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:


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]);

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?


closed as off-topic by Ethan Bierlein, Jamal Jul 24 '15 at 23:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Ethan Bierlein, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ did you try post this in signal processing ? \$\endgroup\$ – Abr001am Jul 24 '15 at 23:05
  • \$\begingroup\$ no, this might be a good place. I will try this after testing the cfmakeraw() function. \$\endgroup\$ – firebug Jul 24 '15 at 23:16

You should use cfmakeraw() to init the termios structure. If you forget to set an option, like ignore certain keycodes, binary data might get interpreted by the lower layer as special command and therefore get removed from the data.

  • \$\begingroup\$ thanks, I will try this tomorrow, when I have access to the device again. \$\endgroup\$ – firebug Jul 24 '15 at 23:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.