-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialclient.go
182 lines (162 loc) · 5.3 KB
/
serialclient.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package modbus
import (
"context"
"errors"
"github.com/aldas/go-modbus-client/packet"
"io"
"os"
"sync"
"time"
)
// SerialClient provides mechanisms to send requests to modbus server over serial port
type SerialClient struct {
// readTimeout is total amount of time reading the response can take before client returns error.
// NB: if you have set long reading timeout on your serial port implementation this timeout will not help you
// as it works for cases when there are multiple read calls.
readTimeout time.Duration
asProtocolErrorFunc func(data []byte) error
parseResponseFunc func(data []byte) (packet.Response, error)
mu sync.RWMutex
isFlusher bool
serialPort io.ReadWriteCloser
hooks ClientHooks
}
// NewSerialClient creates new instance of Modbus SerialClient for Modbus RTU protocol
func NewSerialClient(serialPort io.ReadWriteCloser, opts ...SerialClientOptionFunc) *SerialClient {
_, isFlusher := serialPort.(Flusher)
client := &SerialClient{
readTimeout: defaultReadTimeout,
asProtocolErrorFunc: packet.AsRTUErrorPacket,
parseResponseFunc: packet.ParseRTUResponseWithCRC,
serialPort: serialPort,
hooks: nil,
isFlusher: isFlusher,
}
for _, o := range opts {
o(client)
}
return client
}
// SerialClientOptionFunc is options type for NewSerialClient function
type SerialClientOptionFunc func(c *SerialClient)
// WithSerialHooks is option to set hooks for SerialClient
func WithSerialHooks(hooks ClientHooks) func(c *SerialClient) {
return func(c *SerialClient) {
c.hooks = hooks
}
}
// WithSerialReadTimeout is option to for setting total timeout for reading the whole packet
func WithSerialReadTimeout(readTimeout time.Duration) func(c *SerialClient) {
return func(c *SerialClient) {
c.readTimeout = readTimeout
}
}
// Do sends given Modbus request to modbus server and returns parsed Response.
// ctx is to be used for to cancel connection attempt.
// On modbus exception nil is returned as response and error wraps value of type packet.ErrorResponseRTU
// User errors.Is and errors.As to check if error wraps packet.ErrorResponseRTU
func (c *SerialClient) Do(ctx context.Context, req packet.Request) (packet.Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
if req == nil {
return nil, errors.New("request can not be nil")
}
if c.serialPort == nil {
return nil, errors.New("serial port is not set")
}
resp, err := c.do(ctx, req.Bytes(), req.ExpectedResponseLength())
if err != nil {
return nil, err
}
if c.hooks != nil {
c.hooks.BeforeParse(resp)
}
return c.parseResponseFunc(resp)
}
func (c *SerialClient) do(ctx context.Context, data []byte, expectedLen int) ([]byte, error) {
if c.hooks != nil {
c.hooks.BeforeWrite(data)
}
if _, err := c.serialPort.Write(data); err != nil {
if err := c.flush(); err != nil {
return nil, &ClientError{Err: err}
}
return nil, &ClientError{Err: err}
}
// some serial devices need time between write and reads for device to have enough time to start responding
// in theory we could just start reading and waiting bytes to arrive but this does not seems to work reliably
// sleeping a little before reading seems to solve problems.
time.Sleep(30 * time.Millisecond)
// make buffer a little bit bigger than would be valid to see problems when somehow more bytes are sent
const maxBytes = rtuPacketMaxLen + 10
received := [maxBytes]byte{}
total := 0
readTimeout := time.After(c.readTimeout)
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-readTimeout:
return nil, &ClientError{Err: errors.New("total read timeout exceeded")}
default:
}
n, err := c.serialPort.Read(received[total:maxBytes])
if c.hooks != nil {
c.hooks.AfterEachRead(received[total:total+n], n, err)
}
// on read errors we do not return immediately as for:
// os.ErrDeadlineExceeded - we set new deadline on next iteration
// io.EOF - we check if read + received is enough to form complete packet
if err != nil && !(errors.Is(err, os.ErrDeadlineExceeded) || errors.Is(err, io.EOF)) {
if err := c.flush(); err != nil {
return nil, &ClientError{Err: err}
}
return nil, &ClientError{Err: err}
}
total += n
if total > rtuPacketMaxLen {
if err := c.flush(); err != nil {
return nil, &ClientError{Err: err}
}
return nil, &ErrPacketTooLong
}
// check if we have exactly the error packet. Error packets are shorter than regulars packets
if errPacket := c.asProtocolErrorFunc(received[0:total]); errPacket != nil {
if err := c.flush(); err != nil {
return nil, &ClientError{Err: err}
}
return nil, &ClientError{Err: errPacket}
}
if total >= expectedLen {
if err := c.flush(); err != nil {
return nil, &ClientError{Err: err}
}
break
}
}
if total == 0 {
return nil, &ClientError{Err: errors.New("no bytes received")}
}
result := make([]byte, total)
copy(result, received[:total])
return result, nil
}
// Close closes serial connection to the device
func (c *SerialClient) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.serialPort == nil {
return nil
}
return c.serialPort.Close()
}
func (c *SerialClient) flush() error {
if !c.isFlusher {
return nil
}
return c.serialPort.(Flusher).Flush()
}
// Flusher is interface for flushing unread/unwritten data from serial port buffer
type Flusher interface {
Flush() error
}