Occasionally the code was reading the result from the FTDI chip before the result was ready, giving the wrong "current state" of the I/O pins.