

At first I suspected random bit flips, but the data modification was deterministic.

When trying this on a second computer, however, I had a nasty surprise waiting for me: reading from the serial port would return wrong data in some cases. On the first computer (where I wrote the code) everything worked well, and the code worked as expected. What I didn't do, however, was to closely look at all of the input flags available. I was working on implementing a binary protocol over a serial port (the details are not important), and I tried to setup the serial port with the proper settings: make sure the baud rate is right, configure the number of data bits, whether parity is used, the number of stop bits, make sure the terminal is in noncanonical mode (for raw access), make sure flow control is configured properly, and configure the right timeouts. What I found out the hard way is that one actually needs to carefully look at all available flags, otherwise legacy settings can come back to haunt you. Sweet's Serial Programming Guide for POSIX Operating Systems is an invaluable resource. Programming serial ports on Linux (and other POSIX-compatible systems) can be tricky, because a lot of legacy is involved here.
