Tested on Mac OS, Linux and Windows with UART emulated on USB.
I wrote general BSD code, because it should work, but I am not able to test
it.
On Mac OS I am quite sure that drain behaves as expected because it fixed
an issue with buffer overflow (which OS does not report but fails).
For Windows it seems that drain is actually part of `Write`, because
`Write` was long and `Drain` was fast. But I have not found any mention in
Win32 docs about buffering and asynchronous write, so I put there flushing
code to be sure, and this code did not break anything.
For Linux `Drain` is also taking more time than writing so it looks working
too.
We observer errors getting the IOCalloutDevice string, this is a
tentative fix based on the hypotesis that the enumerator is
called too early during the USB enumeration phase.
A very small port.readTimeout may lead to a negative timeout if the
elapsed time between:
deadline = time.Now().Add(port.readTimeout)
and
timeout = time.Until(deadline)
is longer than port.readTimeout.
Fix#134
See https://stackoverflow.com/a/34945814/1655275
> pselect signals that file descriptor is ready and
> ioctl(fd, FIONREAD, &len) returns zero len.
Failure to detect this condition has been revealed by
the testsuite:
=== RUN TestDisconnectingPortDetection
2017/07/27 13:56:37 PR - Connecting to Probe
2017/07/27 13:56:37 > Searching for port 2341:8037
2017/07/27 13:56:37 Detected port '/dev/ttyACM0' 2341:8037
2017/07/27 13:56:37 Using '/dev/ttyACM0'
2017/07/27 13:56:37 Starting test (timeout 20s)
2017/07/27 13:56:37 PR - Turn ON target
2017/07/27 13:56:37 TR - Connecting to Target
2017/07/27 13:56:37 > Searching for port 2341:8036
2017/07/27 13:56:37 Detected port '/dev/ttyACM0' 2341:8037
[...cut...]
2017/07/27 13:56:46 > Searching for port 2341:8036
2017/07/27 13:56:46 Detected port '/dev/ttyACM0' 2341:8037
2017/07/27 13:56:46 Detected port '/dev/ttyACM1' 2341:8036
2017/07/27 13:56:46 Using '/dev/ttyACM1'
2017/07/27 13:56:46 T2 - Make a Read call
2017/07/27 13:56:46 T1 - Delay 200ms before disconnecting target
2017/07/27 13:56:46 T1 - Disconnect target
2017/07/27 13:56:46 PR - Turn OFF target
2017/07/27 13:56:46 T2 - Read returned: n=0 err=nil
--- FAIL: TestDisconnectingPortDetection (9.18s)
Error Trace: serial_test.go:100
Error: An error is expected but got nil. %s
Messages: Read returned no errors
this commit fix the problem above.