This repository has been archived by the owner on Jan 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtcp.go
154 lines (135 loc) · 3.11 KB
/
tcp.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
/**
* Copyright (C) 2014 - 2015, Markus Kohlhase <mail@markus-kohlhase.de>
*/
package modbus
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"strconv"
"time"
)
const (
aduLength = 260
headerLength = 7
tcpProtocolId = 0
)
type header struct {
// Transaction Identifier
transaction uint16
// Protocol Identifier
protocol uint16
// PDU Length
length uint16
// Unit Identifier
unit uint8
}
type adu struct {
header *header
pdu *Pdu
}
func (adu *adu) pack() (bin []byte, err error) {
binPdu, err := adu.pdu.pack()
if err != nil {
return
}
bin = append(adu.header.pack(), binPdu...)
return
}
func (h *header) pack() []byte {
buff := bytes.NewBuffer([]byte{})
binary.Write(buff, binary.BigEndian, h)
return buff.Bytes()
}
func unpackHeader(data []byte) (*header, error) {
if l := len(data); l < headerLength {
return nil, fmt.Errorf("Invalid header length: %d byte", l)
}
return &header{
binary.BigEndian.Uint16(data[0:2]),
binary.BigEndian.Uint16(data[2:4]),
binary.BigEndian.Uint16(data[4:6]),
data[6],
}, nil
}
func unpackAdu(data []byte) (*adu, error) {
if l := len(data); l < 8 {
return nil, fmt.Errorf("Invalid ADU length: %d byte", l)
}
pdu, err := unpackPdu(data[headerLength:])
if err != nil {
return nil, err
}
header, err := unpackHeader(data[0:headerLength])
if err != nil {
return nil, err
}
return &adu{header, pdu}, nil
}
type tcpTransporter struct {
host string
port uint
conn net.Conn
transaction uint16
id uint8
timeout time.Duration
}
func (t *tcpTransporter) Connect() error {
address := t.host + ":" + strconv.Itoa(int(t.port))
if t.timeout > 0 {
conn, err := net.DialTimeout("tcp", address, t.timeout)
t.conn = conn
return err
} else {
conn, err := net.Dial("tcp", address)
t.conn = conn
return err
}
}
func (t *tcpTransporter) Close() (err error) {
if t.conn != nil {
if err = t.conn.Close(); err != nil {
return
}
t.conn = nil
return
}
return errors.New("Not connected")
}
func (t *tcpTransporter) Send(pdu *Pdu) (*Pdu, error) {
if t.conn == nil {
if err := t.Connect(); err != nil {
return nil, err
}
}
t.transaction++
header := &header{t.transaction, tcpProtocolId, uint16(len(pdu.Data) + 2), t.id}
binAdu, err := (&adu{header, pdu}).pack()
if err != nil {
return nil, err
}
if _, err := t.conn.Write(binAdu); err != nil {
return nil, fmt.Errorf("Could not write data: %s", err)
}
buff := make([]byte, aduLength)
l, err := t.conn.Read(buff)
if err != nil {
return nil, fmt.Errorf("Could not receive data: %s", err)
}
res, err := unpackAdu(buff[:l])
if err != nil {
return nil, fmt.Errorf("Could not read PDU: %s", err)
}
if i := res.header.transaction; i != t.transaction {
return nil, fmt.Errorf("Invalid transaction id: %d instead of %d", i, t.transaction)
}
return res.pdu, nil
}
func NewTcpClient(host string, port uint) IoClient {
return &mbClient{&tcpTransporter{host: host, port: port}}
}
func NewTcpClientTimeout(host string, port uint, timeout time.Duration) IoClient {
return &mbClient{&tcpTransporter{host: host, port: port, timeout: timeout}}
}