jacobsa / go-serial

A Go library for dealing with serial ports.
Apache License 2.0
631 stars 121 forks source link

Unable to properly reopen serial device after it disappears #51

Open jasongaunt opened 3 years ago

jasongaunt commented 3 years ago

Hi all, I'm using this library on Linux to configure and maintain connectivity of a USB LTE modem. One of the steps results in the modem "disappearing" as it restarts. My modems TTY device (/dev/ttyACM0 in my case but this would also apply to /dev/ttyUSBx and similar) disappears and reappears about 5 seconds later.

I put in code to handle that by checking for the existence of /dev/ttyACM0 before reading / writing to it and to execute serial.Close() and then (after a small delay) serial.Open() again. There are no errors re-opening the device, but the moment I try and call serial.Read() or serial.Write() the Go program errors out with the following;

Error reading from serial port: read /dev/ttyACM0: file already closed or Error writing to serial port: write /dev/ttyACM0: file already closed

How can I handle this without restarting the Go program? To me it seems like the Close() event isn't fully doing its task or some state in the serial library is persisting.