Reputation: 169
I'm working in c programming language under Linux, trying to create a communication application with serial port. The program is sending data to a serial port and reading received data from a microcontroller. The received data could reach any number of bytes between 10 and 64 but no more and no less. I use the following code to read and write data:
unsigned char send_bytes[] = { 0x1, 0x6, 0x2, 0xAA, 0x2, 0x3, 0xB8, 0x4 };
int w = write(fd, send_bytes, sizeof(send_bytes)); // send
char buffer[64];
int r = read(fd, buffer, sizeof(buffer)); //read data
My problem is that r
never gets more than 8 bytes of data. Does anyone know why is this the case?
Thanks in advance.
Upvotes: 0
Views: 1647
Reputation: 399803
Perhaps your operating system is only providing 8 bytes of serial port buffering, so it "favors" to deliver incoming data in chunks of 8 bytes.
Repeat the read for as long as it has data available. You can use select()
for this on systems where it's available.
Also, since the other end is a microcontroller which might be slower than your workstation by a large margin, perhaps the data simply isn't available yet when you do the read()
. That's another reason to try again, of course.
Upvotes: 1