bindings-cpp icon indicating copy to clipboard operation
bindings-cpp copied to clipboard

Fix single character reads on windows

Open markw65 opened this issue 1 year ago • 0 comments

I have a project using @serialport/bindings-cpp that works without issues on MacOS and Linux, but on windows every read fails with

[Error: Reading from COM port (ReadFile): Unknown error code 1784]

The device I'm talking to has a binary protocol, and all exchanges begin with a single character write to the device, a single character response from the device, and then further, longer exchanges. The single character read always fails.

This is because ReadThread does a single character read using ReadFileEx, and then, unconditionally reads "the rest of the data" using ReadFile. But in the case of a single character read there is no "rest of the data". This pull request fixes it by checking for no more data before issuing the ReadFile.

I don't know how to write a test that would fail without the fix.

markw65 avatar Dec 18 '23 18:12 markw65