forked from goburrow/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtu_over_tcp_client.go
101 lines (90 loc) · 2.63 KB
/
rtu_over_tcp_client.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
// Copyright 2018 xft. All rights reserved.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
package modbus
import (
"io"
"time"
)
// RTUOverTCPClientHandler implements Packager and Transporter interface.
type RTUOverTCPClientHandler struct {
rtuPackager
rtuTCPTransporter
}
// NewRTUOverTCPClientHandler allocates and initializes a RTUOverTCPClientHandler.
func NewRTUOverTCPClientHandler(address string) *RTUOverTCPClientHandler {
handler := &RTUOverTCPClientHandler{}
handler.Address = address
handler.Timeout = tcpTimeout
handler.IdleTimeout = tcpIdleTimeout
return handler
}
// RTUOverTCPClient creates RTU over TCP client with default handler and given connect string.
func RTUOverTCPClient(address string) Client {
handler := NewRTUOverTCPClientHandler(address)
return NewClient(handler)
}
// rtuTCPTransporter implements Transporter interface.
type rtuTCPTransporter struct {
tcpTransporter
}
func (mb *rtuTCPTransporter) Send(aduRequest []byte) (aduResponse []byte, err error) {
mb.tcpTransporter.mu.Lock()
defer mb.tcpTransporter.mu.Unlock()
// Establish a new connection if not connected
if err = mb.tcpTransporter.connect(); err != nil {
return
}
// Set timer to close when idle
mb.tcpTransporter.lastActivity = time.Now()
mb.tcpTransporter.startCloseTimer()
// Set write and read timeout
var timeout time.Time
if mb.Timeout > 0 {
timeout = mb.lastActivity.Add(mb.Timeout)
}
if err = mb.conn.SetDeadline(timeout); err != nil {
return
}
// Send the request
mb.tcpTransporter.logf("modbus: sending % x\n", aduRequest)
if _, err = mb.conn.Write(aduRequest); err != nil {
return
}
function := aduRequest[1]
functionFail := aduRequest[1] & 0x80
bytesToRead := calculateResponseLength(aduRequest)
var n int
var n1 int
var data [rtuMaxSize]byte
//We first read the minimum length and then read either the full package
//or the error package, depending on the error status (byte 2 of the response)
n, err = io.ReadAtLeast(mb.conn, data[:], rtuMinSize)
if err != nil {
return
}
//if the function is correct
if data[1] == function {
//we read the rest of the bytes
if n < bytesToRead {
if bytesToRead > rtuMinSize && bytesToRead <= rtuMaxSize {
if bytesToRead > n {
n1, err = io.ReadFull(mb.conn, data[n:bytesToRead])
n += n1
}
}
}
} else if data[1] == functionFail {
//for error we need to read 5 bytes
if n < rtuExceptionSize {
n1, err = io.ReadFull(mb.conn, data[n:rtuExceptionSize])
}
n += n1
}
if err != nil {
return
}
aduResponse = data[:n]
mb.logf("modbus: received % x\n", aduResponse)
return
}