If the SerialPort.Write() is a blocking operation( or is it not?), what would be the need for the BytesToWrite() method. It would always evaluate to zero, cause the last Write operation either succeeded in writing all data or failed, in either case the bytes to be written would be come zero.
Perhaps, there is more to it then what I have described.
SerialPort.Writeis a blocking operation, yes. However, there are two buffers to be considered: The serial device, and theSerialPortbuffers.If the
SerialPortobject is configured to buffer, the write only blocks if there isn’t enough room in that buffer. It will block for as long as it takes the buffer to empty enough to fit the new data. Otherwise it fills the buffer and returns.If the
SerialPortobject does not buffer, the Write operation blocks only for as long as it takes to transfer the data to the serial device. That device has its own buffer(*), so the block may take far less time than the time it will take to send the data.SerialPort.BytesToWriteincludes both data in the device’s buffer and data in theSerialPortobject’s buffer.(*) Older UARTs did not have buffers and newer ones do but can be configured to not buffer.