bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
619 stars 189 forks source link

Add API to check if port is alive #44

Open niondir opened 6 years ago

niondir commented 6 years ago

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}
}
quite commented 1 year ago

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?