go-serial icon indicating copy to clipboard operation
go-serial copied to clipboard

Add API to check if port is alive

Open niondir opened this issue 6 years ago • 1 comments

It would be nice to have a function on the Port that allows to check if the port is still alive.

It's already done in the Read() methods.

On Windows:

getCommState(port.handle, params)
if err := setCommState(port.handle, params); err != nil {
	port.Close()
	return 0, err
}

On Linux:

port.closeLock.RLock()
defer port.closeLock.RUnlock()
if !port.opened {
	return 0, &PortError{code: PortClosed}
}

fds := unixutils.NewFDSet(port.handle, port.closeSignal.ReadFD())
res, err := unixutils.Select(fds, nil, fds, -1)
if err != nil {
	return 0, err
}
if res.IsReadable(port.closeSignal.ReadFD()) {
	return 0, &PortError{code: PortClosed}
}

niondir avatar Nov 30 '17 10:11 niondir

I believe checking if a port is "alive" (without trying to read/write) can only really be done if there is some kind of reliable hardware flow control, DTR/DSR. If I'm not mistaken, what's happening here is that portClosed is found out upon a Read attempt. What do you think @cmaglie?

quite avatar Dec 23 '22 14:12 quite