-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SetDeadline implementation #117
Comments
A quick and dirty implementation may be : func (port *unixPort) SetDeadline(t time.Time) error {
return port.SetReadTimeout(time.Until(t))
} but it would be also interesting to have timeouts/deadlines on |
Hello, Either the native timeout or the context approach can be used in my case, but I need both read and write timeouts 😃 |
I need this as well. I would like to use |
How about the same interface as net.conn?
which could be set using for example If implementing the same interface as net.Conn one could keep much code between networked and serial connections without much change. |
I second this! I had to implement serial port handling in software that was already using type portWithDeadline struct {
serial.Port
writeDeadline *time.Time
}
func (p portWithDeadline) SetDeadline(t time.Time) error {
p.SetWriteDeadline(t)
return p.SetReadDeadline(t)
}
func (p portWithDeadline) SetReadDeadline(t time.Time) error {
return p.SetReadTimeout(time.Until(t))
}
func (p portWithDeadline) SetWriteDeadline(t time.Time) error {
p.writeDeadline = &t
return nil
}
func (p portWithDeadline) Write(b []byte) (n int, err error) {
if p.writeDeadline == nil || p.writeDeadline.IsZero() {
return p.Port.Write(b)
}
finishedCh := make(chan struct{})
timeoutCh := time.After(time.Until(*p.writeDeadline))
go func() {
defer close(finishedCh)
n, err = p.Port.Write(b)
}()
select {
case <-timeoutCh:
return 0, errors.New("write operation timed out")
case <-finishedCh:
return n, err
}
} |
Hello,
Thanks for PR #109 (timeouts).
I am using your lib (go 1.16.7 on linux/amd64) to drive Ingenico payement hardware that has timeouts defined in its communication protocol. But the timeouts are not the same between protocol states. For example : when I write a ENQ byte, there is a response timeout of 2s; when I ended sending something, I have to listen 2s before I can start sending a new item; when there is a communication conflict, the timeout is 5s before retry...
My first driving implementation was based on your lib, and did not fully respected all the timeouts. My second implementation use directly the
/dev/ttyACM0
file (using *os.File), and respects the timeouts, using the*os.File.SetDeadline
func.So I would like if it is possible to add a
SetDeadline
implementation that can be called when needed, and that may have the same behavior that the*os.File
. 🙂The text was updated successfully, but these errors were encountered: