forked from simonvetter/modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus.go
104 lines (92 loc) · 3.59 KB
/
modbus.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
package modbus
import (
"fmt"
"errors"
)
type pdu struct {
unitId uint8
functionCode uint8
payload []byte
}
const (
// coils
FC_READ_COILS uint8 = 0x01
FC_WRITE_SINGLE_COIL uint8 = 0x05
FC_WRITE_MULTIPLE_COILS uint8 = 0x0f
// discrete inputs
FC_READ_DISCRETE_INPUTS uint8 = 0x02
// 16-bit input/holding registers
FC_READ_HOLDING_REGISTERS uint8 = 0x03
FC_READ_INPUT_REGISTERS uint8 = 0x04
FC_WRITE_SINGLE_REGISTER uint8 = 0x06
FC_WRITE_MULTIPLE_REGISTERS uint8 = 0x10
FC_MASK_WRITE_REGISTER uint8 = 0x16
FC_READ_WRITE_MULTILE_REGISTERS uint8 = 0x17
FC_READ_FIFO_QUEUE uint8 = 0x18
// file access
FC_READ_FILE_RECORD uint8 = 0x14
FC_WRITE_FILE_RECORD uint8 = 0x15
// exception codes
EX_ILLEGAL_FUNCTION uint8 = 0x01
EX_ILLEGAL_DATA_ADDRESS uint8 = 0x02
EX_ILLEGAL_DATA_VALUE uint8 = 0x03
EX_SERVER_DEVICE_FAILURE uint8 = 0x04
EX_ACKNOWLEDGE uint8 = 0x05
EX_SERVER_DEVICE_BUSY uint8 = 0x06
EX_MEMORY_PARITY_ERROR uint8 = 0x08
EX_GW_PATH_UNAVAILABLE uint8 = 0x0a
EX_GW_TARGET_FAILED_TO_RESPOND uint8 = 0x0b
)
var (
ErrConfigurationError error = errors.New("configuration error")
ErrRequestTimedOut error = errors.New("request timed out")
ErrIllegalFunction error = errors.New("illegal function")
ErrIllegalDataAddress error = errors.New("illegal data address")
ErrIllegalDataValue error = errors.New("illegal data value")
ErrServerDeviceFailure error = errors.New("server device failure")
ErrAcknowledge error = errors.New("request acknowledged")
ErrServerDeviceBusy error = errors.New("server device busy")
ErrMemoryParityError error = errors.New("memory parity error")
ErrGWPathUnavailable error = errors.New("gateway path unavailable")
ErrGWTargetFailedToRespond error = errors.New("gateway target device failed to respond")
ErrBadCRC error = errors.New("bad crc")
ErrShortFrame error = errors.New("short frame")
ErrProtocolError error = errors.New("protocol error")
ErrBadUnitId error = errors.New("bad unit id")
ErrBadTransactionId error = errors.New("bad transaction id")
ErrUnknownProtocolId error = errors.New("unknown protocol identifier")
ErrUnexpectedParameters error = errors.New("unexpected parameters")
)
func mapExceptionCodeToError(exceptionCode uint8) (err error) {
switch exceptionCode {
case EX_ILLEGAL_FUNCTION: err = ErrIllegalFunction
case EX_ILLEGAL_DATA_ADDRESS: err = ErrIllegalDataAddress
case EX_ILLEGAL_DATA_VALUE: err = ErrIllegalDataValue
case EX_SERVER_DEVICE_FAILURE: err = ErrServerDeviceFailure
case EX_ACKNOWLEDGE: err = ErrAcknowledge
case EX_MEMORY_PARITY_ERROR: err = ErrMemoryParityError
case EX_SERVER_DEVICE_BUSY: err = ErrServerDeviceBusy
case EX_GW_PATH_UNAVAILABLE: err = ErrGWPathUnavailable
case EX_GW_TARGET_FAILED_TO_RESPOND: err = ErrGWTargetFailedToRespond
default:
err = fmt.Errorf("unsupported exception code (%v)", exceptionCode)
}
return
}
func mapErrorToExceptionCode(err error) (exceptionCode uint8) {
switch err {
case ErrIllegalFunction: exceptionCode = EX_ILLEGAL_FUNCTION
case ErrIllegalDataAddress: exceptionCode = EX_ILLEGAL_DATA_ADDRESS
case ErrIllegalDataValue: exceptionCode = EX_ILLEGAL_DATA_VALUE
case ErrServerDeviceFailure: exceptionCode = EX_SERVER_DEVICE_FAILURE
case ErrAcknowledge: exceptionCode = EX_ACKNOWLEDGE
case ErrMemoryParityError: exceptionCode = EX_MEMORY_PARITY_ERROR
case ErrServerDeviceBusy: exceptionCode = EX_SERVER_DEVICE_BUSY
case ErrGWPathUnavailable: exceptionCode = EX_GW_PATH_UNAVAILABLE
case ErrGWTargetFailedToRespond:
exceptionCode = EX_GW_TARGET_FAILED_TO_RESPOND
default:
exceptionCode = EX_SERVER_DEVICE_FAILURE
}
return
}