LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I read all the data from a serial port? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-read-all-the-data-from-a-serial-port-4175550098/)

rpbulls 08-06-2015 10:25 PM

How do I read all the data from a serial port?
 
All,
I'm trying to talk to a modem through a usb2serial port interface. I managed to open a port, send commands to the modem and read back data.
Currently, i'm doing a simple read of responses using

read (int fd, char *response, 8)

Essentially, i'm reading 8 bytes at a time (note that the modem init was done at 1152000 8n1). Also, note that i'm monitoring for responses from the serial port using "select".

However, the response from the modem is of varying length. And, a fixed number of byte reads seems to be either missing some of the bytes or adding garbage to the "response" string.

How can i effectively read the exact number of bytes (no more no less) from the modem?

Appreciate help!

jefro 08-07-2015 03:10 PM

Generally you issue command AT (attention) to a modem assuming you have the correct serial speeds. That speed you posted seems very wrong.

michaelk 08-07-2015 03:28 PM

I assume it is a typo i.e 115200. Its been awhile since I've played with a MODEM. You can look at the return value to see the number or characters that was actually read. Depending on how it is configured does it return an OK or other character(s) when the command completes?

rpbulls 08-08-2015 08:24 AM

Thanks for the responses. Sorry about the typo in the serial port config (115200, it shudda been). Quite silly of me to not have realized that read() returns the actual byte count. Worked like a charm!


All times are GMT -5. The time now is 07:24 PM.