jacobsa / go-serial

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

Linux and communication errors #38

Open min4er opened 6 years ago

min4er commented 6 years ago

Hello! In Linux, when we try to read from port and wait fo data, we have a io.EOF error, and can hande it. Code (simplified):

var n int = 0
var err error
res:=make([byte, 1024])

for n==0{
n, err =port.Read(res)
if err!=nil && err!=io.EOF{
log.Printf("port.Read: %v", err)
}
log.Printf("Read: %v bytes", n)

But, when connection lost occurs, the error is also io.EOF ! How can I distinguish disconnection and waiting for data case? Now, when I get io.EOF error, I check if /dev/tty* file phisically exists on disk, to recognize a cable disconnection. But, this method is quite ugly.

And, in addition, can you advise me, what is the best way to handle IO errors , using your package? In Windows, when cable is re-connected, I can continue work without closing and re-opening port, but in Linux, I have "bad descriptor" error. So I can not use "defer port.Close()", And, additionally,manual closing of port causes crash of the stack of all functions , which used the descriptor at the moment. Is there a way to "renew" descriptor or some another practice?

wybiral commented 6 years ago

I'm having this same issue on Linux but on Windows it seems to block like normal.