Skip to content

Commit

Permalink
add option to pass serial read timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasilijs Jelcaninovs committed May 23, 2022
1 parent fe4c9e6 commit d1c7323
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion serial/serial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package serial

import (
"github.com/tarm/serial"
"time"
)

// New creates a serial port.
Expand All @@ -18,7 +19,8 @@ func New(options ...Option) (*serial.Port, error) {
for _, option := range options {
option.applyConfig(&cfg)
}
config := serial.Config{Name: cfg.port, Baud: cfg.baud}

config := serial.Config{Name: cfg.port, Baud: cfg.baud, ReadTimeout: cfg.ReadTimeout}
p, err := serial.OpenPort(&config)
if err != nil {
return nil, err
Expand All @@ -36,6 +38,11 @@ func WithPort(p string) Port {
return Port(p)
}

// WithTimeout specifies read timeout the serial port.
func WithTimeout(t time.Duration) ReadTimeout {
return ReadTimeout(t)
}

// Option is a construction option that modifies the behaviour of the serial port.
type Option interface {
applyConfig(*Config)
Expand All @@ -45,6 +52,7 @@ type Option interface {
type Config struct {
port string
baud int
ReadTimeout time.Duration // Total timeout
}

// Baud is the bit rate for the serial line.
Expand All @@ -60,3 +68,9 @@ type Port string
func (p Port) applyConfig(c *Config) {
c.port = string(p)
}

type ReadTimeout time.Duration

func (t ReadTimeout) applyConfig(c *Config) {
c.ReadTimeout = time.Duration(t)
}

0 comments on commit d1c7323

Please sign in to comment.