-
Notifications
You must be signed in to change notification settings - Fork 2
/
Constants.go
140 lines (125 loc) · 4.53 KB
/
Constants.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
package modbus
import (
"errors"
)
// Mode is the modbus connection mode type.
type Mode byte
// The available modbus connection modes.
const (
ModeTCP Mode = iota
ModeRTU
ModeASCII
)
// ModeNames maps Mode to a string description
var ModeNames = map[Mode]string{
ModeTCP: "TCP",
ModeRTU: "RTU",
ModeASCII: "ASCII",
}
// ModeByName maps ModeNames to their Mode, i.e. the inverse of ModeNames.
var ModeByName = map[string]Mode{}
// MaxRTUSize MaxASCIISize and MaxTCPSize define the maximum allowable number
// of byes in a single Modbus packet.
const (
MaxRTUSize = 512
MaxASCIISize = 512
MaxTCPSize = 260
)
// FunctionCode is the modbus function code type.
type FunctionCode byte
// Modbus Function Codes
const (
FunctionReadCoils FunctionCode = 0x01
FunctionReadDiscreteInputs = 0x02
FunctionReadHoldingRegisters = 0x03
FunctionReadInputRegisters = 0x04
FunctionWriteSingleCoil = 0x05
FunctionWriteSingleRegister = 0x06
FunctionWriteMultipleCoils = 0x0F
FunctionWriteMultipleRegisters = 0x10
FunctionMaskWriteRegister = 0x16
)
// FunctionNames maps function name strings by their Function Code
var FunctionNames = map[FunctionCode]string{
FunctionReadCoils: "ReadCoils",
FunctionReadDiscreteInputs: "ReadDiscreteInputs",
FunctionReadHoldingRegisters: "ReadHoldingRegisters",
FunctionReadInputRegisters: "ReadInputRegisters",
FunctionWriteSingleCoil: "WriteSingleCoil",
FunctionWriteSingleRegister: "WriteSingleRegister",
FunctionWriteMultipleCoils: "WriteMultipleCoils",
FunctionWriteMultipleRegisters: "WriteMultipleRegisters",
FunctionMaskWriteRegister: "MaskWriteRegister",
}
// FunctionCodes maps FunctionCodes by their FunctionName, i.e. the inverse of
// the FunctionNames map
var FunctionCodes = map[string]FunctionCode{}
func init() {
// Initialize FunctionCodes map as the inverse of the FunctionNames map
for b, s := range FunctionNames {
FunctionCodes[s] = b
}
for m, s := range ModeNames {
ModeByName[s] = m
}
}
// exception indexes into the exceptions map
const (
// Official Modbus Exceptions
exceptionUnknown = 0x00
exceptionIllegalFunction = 0x01
exceptionDataAddress = 0x02
exceptionDataValue = 0x03
exceptionSlaveDeviceFailure = 0x04
exceptionAcknowledge = 0x05
exceptionSlaveDeviceBusy = 0x06
exceptionMemoryParityError = 0x08
exceptionGatewayPathUnavailable = 0x0A
exceptionGatewayTargetDeviceFailedToRespond = 0x0B
// Unofficial exceptions
exceptionEmptyResponse = 0xf9
exceptionBadResponseLength = 0xfa
exceptionBadFraming = 0xfb
exceptionSlaveIDMismatch = 0xfc
exceptionWriteDataMismatch = 0xfd
exceptionResponseLengthMismatch = 0xfe
exceptionBadChecksum = 0xff
)
// exceptions contains a map of common exceptions that may be returned by a
// Packager in the course of sending a Query.
var exceptions = map[uint16]error{
exceptionUnknown: errors.New(
"Modbus Error: Unknown"),
exceptionIllegalFunction: errors.New(
"Modbus Error: Illegal Function (0x01)"),
exceptionDataAddress: errors.New(
"Modbus Error: Data Address (0x02)"),
exceptionDataValue: errors.New(
"Modbus Error: Data Value (0x03)"),
exceptionSlaveDeviceFailure: errors.New(
"Modbus Error: Slave Device Failure (0x04)"),
exceptionAcknowledge: errors.New(
"Modbus Error: Acknowledge (0x05)"),
exceptionSlaveDeviceBusy: errors.New(
"Modbus Error: Slave Device Busy (0x06)"),
exceptionMemoryParityError: errors.New(
"Modbus Error: Memory Parity Error (0x08)"),
exceptionGatewayPathUnavailable: errors.New(
"Modbus Error: Gateway Path Unavailable (0x0A)"),
exceptionGatewayTargetDeviceFailedToRespond: errors.New(
"Modbus Error: Gateway Target Device Failed to Respond (0x0B)"),
exceptionEmptyResponse: errors.New(
"Response Error: Empty response"),
exceptionBadResponseLength: errors.New(
"Response Error: Bad response length"),
exceptionBadFraming: errors.New(
"Response Error: Bad Framing"),
exceptionSlaveIDMismatch: errors.New(
"Response Error: SlaveID mismatch"),
exceptionWriteDataMismatch: errors.New(
"Response Error: Write data mismatch"),
exceptionResponseLengthMismatch: errors.New(
"Response Error: Response length mismatch"),
exceptionBadChecksum: errors.New(
"Response Error: Bad Checksum"),
}