serialport / bindings-cpp

The C++ bindings for the node serialport project.
MIT License
19 stars 39 forks source link

Fix single character reads on windows #165

Open markw65 opened 9 months ago

markw65 commented 9 months ago

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.