Got a bug in some legacy code which communicates with a payment terminal.
Just before a new payment is started, the code attempts to clear the internal read buffer o
maybe adding a test on port's 'bytes to read' may help steer clear of badly coded drivers:
length = (_serialPort.BytesToRead > 0) ? _serialPort.Read(buffer, 0, 512) : 0;
better still, use
_serialPort.DiscardInBuffer();
instead!