I have a serial device (RS232) from which I am requesting (binary) data. This interface answers with a sequence of 5 bytes, or 17 bytes when it has some new data.
I figured out, that I have to turn the RTS pin of before sending, and turn it on to receive. But the problem is, that in 1 of around 30 cases I receive only 16 instead of 17 bytes.
I tried a lot to get it to the current state, but I haven't any more ideas to debug. Here is the content of my read function, which takes byte for byte from the port and print in as HEX:
setRTS(fd,1);
int i = 0;
unsigned char arr[length];
while (i < length){
// char chout[1];
int err = read(fd, &arr[i], 1);
if (err != -1) {
//printf("%i %02x\n", i, arr[i]);
printf("%02x", arr[i]);
}
usleep(50000);
i++;
}
Other solution with no sleep in the loop or a larger number of bytes to read haven't worked, and I received no data at all. The full class can be found here.
My Question is, has anybody a better solution or an advice to read data properly from a serial port?