forked from simonvetter/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.go
109 lines (91 loc) · 2.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package modbus
import (
"time"
"go.bug.st/serial"
)
// serialPortWrapper wraps a serial.Port (i.e. physical port) to
// 1) satisfy the rtuLink interface and
// 2) add Read() deadline/timeout support.
type serialPortWrapper struct {
conf *serialPortConfig
port serial.Port
deadline time.Time
}
type serialPortConfig struct {
Device string
Speed int
DataBits int
Parity serial.Parity
StopBits serial.StopBits
Timeout time.Duration
}
func newSerialPortWrapper(conf *serialPortConfig) (spw *serialPortWrapper) {
spw = &serialPortWrapper{
conf: conf,
}
return
}
func (spw *serialPortWrapper) Open() (err error) {
spw.port, err = serial.Open(spw.conf.Device, &serial.Mode{
BaudRate: spw.conf.Speed,
DataBits: spw.conf.DataBits,
Parity: spw.conf.Parity,
StopBits: spw.conf.StopBits,
})
if err == nil {
spw.port.SetReadTimeout(spw.conf.Timeout)
}
return
}
// Closes the serial port.
func (spw *serialPortWrapper) Close() (err error) {
err = spw.port.Close()
return
}
// Reset the input and output buffer
func (spw *serialPortWrapper) Reset() (err error) {
if err := spw.port.ResetInputBuffer(); err != nil {
return err
}
if err := spw.port.ResetOutputBuffer(); err != nil {
return err
}
return nil
}
// Reads bytes from the underlying serial port.
// If Read() is called after the deadline, a timeout error is returned without
// attempting to read from the serial port.
// If Read() is called before the deadline, a read attempt to the serial port
// is made. At this point, one of two things can happen:
// - the serial port's receive buffer has one or more bytes and port.Read()
// returns immediately (partial or full read),
// - the serial port's receive buffer is empty: port.Read() blocks for
// up to 10ms and returns serial.ErrTimeout. The serial timeout error is
// masked and Read() returns with no data.
//
// As the higher-level methods use io.ReadFull(), Read() will be called
// as many times as necessary until either enough bytes have been read or an
// error is returned (ErrRequestTimedOut or any other i/o error).
func (spw *serialPortWrapper) Read(rxbuf []byte) (cnt int, err error) {
// return a timeout error if the deadline has passed
if time.Now().After(spw.deadline) {
err = ErrRequestTimedOut
return
}
cnt, err = spw.port.Read(rxbuf)
// mask serial.ErrTimeout errors from the serial port
if err != nil {
err = nil
}
return
}
// Sends the bytes over the wire.
func (spw *serialPortWrapper) Write(txbuf []byte) (cnt int, err error) {
cnt, err = spw.port.Write(txbuf)
return
}
// Saves the i/o deadline (only used by Read).
func (spw *serialPortWrapper) SetDeadline(deadline time.Time) (err error) {
spw.deadline = deadline
return
}