0
votes

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.

1
int read, Use different name for function and variable. Don't mix them. - Don't You Worry Child
Sorry they are not named the same. I wrote it here like that, it's fixed now. - user2081328
Try adding subsequent read functions and see what they return. - Don't You Worry Child
You probably need to show your code for the open() and initialization of the serial port. Also, performing syscalls in the variable declarations in not good practice. - sawdust

1 Answers

1
votes

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.