-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathserial.go
62 lines (51 loc) · 1.36 KB
/
serial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <warthog618@gmail.com>.
// Package serial provides a serial port, which provides the io.ReadWriter interface,
// that provides the connection between the at or gsm packages and the physical modem.
package serial
import (
"github.com/tarm/serial"
)
// New creates a serial port.
//
// This is currently a simple wrapper around tarm serial.
func New(options ...Option) (*serial.Port, error) {
cfg := defaultConfig
for _, option := range options {
option.applyConfig(&cfg)
}
config := serial.Config{Name: cfg.port, Baud: cfg.baud}
p, err := serial.OpenPort(&config)
if err != nil {
return nil, err
}
return p, nil
}
// WithBaud sets the baud rate for the serial port.
func WithBaud(b int) Baud {
return Baud(b)
}
// WithPort specifies the port for the serial port.
func WithPort(p string) Port {
return Port(p)
}
// Option is a construction option that modifies the behaviour of the serial port.
type Option interface {
applyConfig(*Config)
}
// Config contains the configuration parameters of the serial port.
type Config struct {
port string
baud int
}
// Baud is the bit rate for the serial line.
type Baud int
func (b Baud) applyConfig(c *Config) {
c.baud = int(b)
}
// Port identifies the serial port on the plaform.
type Port string
func (p Port) applyConfig(c *Config) {
c.port = string(p)
}