-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodbus.go
More file actions
149 lines (131 loc) · 3.48 KB
/
modbus.go
File metadata and controls
149 lines (131 loc) · 3.48 KB
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
package modbus
import (
"encoding/hex"
)
const (
//Bit 操作
// FuncCodeReadDiscreteInputs 功能码:读离散量输入
FuncCodeReadDiscreteInputs = 2
// FuncCodeReadCoils 功能码:读线圈
FuncCodeReadCoils = 1
// FuncCodeWriteSingleCoil 功能码:写单个线圈
FuncCodeWriteSingleCoil = 5
// FuncCodeWriteMultipleCoils 功能码:写多个线圈
FuncCodeWriteMultipleCoils = 15
//16-bit 操作
// FuncCodeReadInputRegisters 功能码:读输入寄存器
FuncCodeReadInputRegisters = 4
// FuncCodeReadHoldingRegisters 功能码:读保持寄存器
FuncCodeReadHoldingRegisters = 3
// FuncCodeWriteSingleRegister 功能码:写单个寄存器
FuncCodeWriteSingleRegister = 6
// FuncCodeWriteMultipleRegisters 功能码:写多个寄存器
FuncCodeWriteMultipleRegisters = 16
// FuncCodeReadWriteMultipleRegisters 功能码:读/写多个寄存器
FuncCodeReadWriteMultipleRegisters = 23
)
const (
TCP = ModbusMode("TCP")
RTU = ModbusMode("RTU")
ASCII = ModbusMode("ASCII")
)
var (
faults = map[byte]string{
1: "01:illegal function code",
2: "02:illegal data address",
3: "03:illegal data value",
4: "04:slave station equipment is faulty",
5: "05:confirm",
6: "06:slave device busy",
8: "08:store parity errors",
10: "0A:gateway path is unavailable",
11: "0B:gateway target device fails to respond",
}
)
type ModbusMode string
// ProtocolDataUnit 协议数据单元
type ProtocolDataUnit interface {
GetFunctionCode() (f byte)
Length() (l int)
GetData() (data []byte)
ToHex() (h string)
}
// 协议数据单元
type protocolDataUnit struct {
functionCode byte
data []byte
length int
}
func (pdu protocolDataUnit) GetFunctionCode() (f byte) {
return pdu.functionCode
}
func (pdu protocolDataUnit) Length() (l int) {
return pdu.length
}
func (pdu protocolDataUnit) GetData() (data []byte) {
return pdu.data
}
func (pdu protocolDataUnit) ToHex() (h string) {
return hex.EncodeToString(pdu.data)
}
// ApplicationDataUnit 应用数据单元
type ApplicationDataUnit interface {
GetSlaveId() byte
GetFunctionCode() byte
GetPDU() ProtocolDataUnit
ToHex() string
GetData() []byte
Length() int
GetCheckSum() uint16
GetCheckSumByte() []byte
GetMode() ModbusMode
}
type applicationDataUnit struct {
slaveID byte
pdu ProtocolDataUnit
data []byte
length int
checkSum uint16
checkSumByte []byte
mode ModbusMode
}
func (u applicationDataUnit) GetSlaveId() byte {
return u.slaveID
}
func (u applicationDataUnit) GetFunctionCode() byte {
return u.pdu.GetFunctionCode()
}
func (u applicationDataUnit) GetPDU() ProtocolDataUnit {
return u.pdu
}
func (u applicationDataUnit) Length() int {
return u.length
}
func (u applicationDataUnit) ToHex() string {
return hex.EncodeToString(u.data)
}
func (u applicationDataUnit) GetData() []byte {
return u.data
}
func (u applicationDataUnit) GetCheckSum() uint16 {
return u.checkSum
}
func (u applicationDataUnit) GetCheckSumByte() []byte {
return u.checkSumByte
}
func (u applicationDataUnit) GetMode() ModbusMode {
return u.mode
}
// Packager 包解析器
type Packager interface {
Encode(pdu protocolDataUnit) (adu ApplicationDataUnit, err error)
Decode(results []byte) (adu ApplicationDataUnit, err error)
Verify(aduRequest ApplicationDataUnit, aduResponse ApplicationDataUnit) (err error)
}
// Transporter 数据传输器
type Transporter interface {
Open() error
Connected() bool
Send(aduRequest ApplicationDataUnit) (aduResponse []byte, err error)
Close() error
}