bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
657 stars 199 forks source link

Timeout doesn't work properly on windows #130

Closed vinxzhu closed 1 year ago

vinxzhu commented 2 years ago

The ReadIntervalTimeout on windows platform is set to -1, which causes the timeout function to work improperly. I tried to set it to 1 in serial_windows.go, then the function worked well. image

kroppt commented 2 years ago

The Windows COMMTIMEOUTS documentation for defines the structure:

typedef struct _COMMTIMEOUTS {
  DWORD ReadIntervalTimeout;
  DWORD ReadTotalTimeoutMultiplier;
  DWORD ReadTotalTimeoutConstant;
  DWORD WriteTotalTimeoutMultiplier;
  DWORD WriteTotalTimeoutConstant;
} COMMTIMEOUTS, *LPCOMMTIMEOUTS;

Again, to clarify the data type, the data types documentation defines:

typedef unsigned long DWORD;

This is a uint32, which is how it is defined in the Go code as well.

Thus, the value of ReadIntervalTimeout cannot be -1.

And finally, the COMMTIMEOUTS documentation remarks:

If an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called:

  • If there are any bytes in the input buffer, ReadFile returns immediately with the bytes in the buffer.
  • If there are no bytes in the input buffer, ReadFile waits until a byte arrives and then returns immediately.
  • If no bytes arrive within the time specified by ReadTotalTimeoutConstant, ReadFile times out.

In short, the code currently configures ReadIntervalTimeout and ReadTotalTimeoutMultiplier correctly, because that is the behavior we want.

I'm not saying there isn't a bug per se, but the explanation you are offering for why appears incorrect. Do you have a concise code sample that reproduces the timeout problem? What is the behavior you are seeing that makes you think it is working improperly?

cmaglie commented 1 year ago

Closing for lack of feedback, please reopen and provide the requested info if you want to continue the discussion.