You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider the following pseudo code for reading from a serial port:
// 1. Open port.port, _:=serial.Open("/dev/pts/1", mode)
// 2. Listen to the "done" channel that emits when user presses Ctrl+C.gofunc() {
<-doneport.Close()
}()
// 3. Read from port.for {
buff:=make([]byte, 1024)
// If Close is called while Read is blocked (because it's waiting on data) // then Read returns and we can exit the application.if_, err=port.Read(buff); err!=nil {
ifisPortClosed(err) {
returnnil
}
returnerr
}
}
In the above example, all works as expected. Calling Close on the port unblocks Read and the application can proceed as desired.
Now consider a similar setup for writing to the serial port:
port, _:=serial.Open("/dev/pts/2", mode)
gofunc() {
<-doneport.Close()
}()
i=: 0for {
// If Close is called while Write is blocked // (because no reader exists on the other side) // then Write does *not* return.if_, err=port.Write([]byte(fmt.Sprintf("%v", i))); err!=nil {
ifisPortClosed(err) {
returnnil
}
returnerr
}
i++time.Sleep(time.Microsecond)
}
In this example, if Write is blocked because no reader exists on the other side, then calling Close on the port does not force Write to return and the application cannot exit cleanly.
The text was updated successfully, but these errors were encountered:
Consider the following pseudo code for reading from a serial port:
In the above example, all works as expected. Calling
Close
on the port unblocksRead
and the application can proceed as desired.Now consider a similar setup for writing to the serial port:
In this example, if
Write
is blocked because no reader exists on the other side, then callingClose
on the port does not forceWrite
to return and the application cannot exit cleanly.The text was updated successfully, but these errors were encountered: