-4
\$\begingroup\$

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?

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

1 Answer 1

1
\$\begingroup\$

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.

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

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