I have an app that expects to do I/O over a full-duplex serial line. That is:
char buf; write(fd, "ping", 4); read(fd, buf, 4);
... expects to end up with whatever four bytes the remote device transmitted in response to the "ping" string.
But I'm running on a half-duplex RS485 line, so every byte that gets transmitted on the serial line is also received on the on the serial line (because they are the same line). So the code snippet above always reads "ping" into the buffer before the remote device transmits anything.
Obviously the host code isn't expecting this.
The best solution I've come up with is to always to a read following a write and verify that the received characters match those that were sent, and then to ignore them.
But is there a better way? Is there a reliable Unix idiom for inhibiting the reception of characters during the time that characters are being transmitted?
(I appreciate that there are lots of subtleties and twisty little mazes in my question. For example, does the UART have a fifo? Is the receive process running in the same thread? Etc. If it was easy, I wouldn't be asking unix.stackexchange! :)
I implemented a simple routine that I call after each
read() an equivalent # of bytes and then uses
strncmp() to verify that they match. It appears to be robust, but I'm still interested to know if there's a driver level approach that might do this better, or at least differently.