-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmodbus.go
More file actions
146 lines (128 loc) · 4.7 KB
/
modbus.go
File metadata and controls
146 lines (128 loc) · 4.7 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
// Copyright 2014 Quoc-Viet Nguyen. 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 provides a client for MODBUS TCP and RTU/ASCII.
*/
package modbus
import (
"context"
"fmt"
)
const (
// FuncCodeReadDiscreteInputs for bit wise access
FuncCodeReadDiscreteInputs = 2
// FuncCodeReadCoils for bit wise access
FuncCodeReadCoils = 1
// FuncCodeWriteSingleCoil for bit wise access
FuncCodeWriteSingleCoil = 5
// FuncCodeWriteMultipleCoils for bit wise access
FuncCodeWriteMultipleCoils = 15
// FuncCodeReadInputRegisters 16-bit wise access
FuncCodeReadInputRegisters = 4
// FuncCodeReadHoldingRegisters 16-bit wise access
FuncCodeReadHoldingRegisters = 3
// FuncCodeWriteSingleRegister 16-bit wise access
FuncCodeWriteSingleRegister = 6
// FuncCodeWriteMultipleRegisters 16-bit wise access
FuncCodeWriteMultipleRegisters = 16
// FuncCodeReadWriteMultipleRegisters 16-bit wise access
FuncCodeReadWriteMultipleRegisters = 23
// FuncCodeMaskWriteRegister 16-bit wise access
FuncCodeMaskWriteRegister = 22
// FuncCodeReadFIFOQueue 16-bit wise access
FuncCodeReadFIFOQueue = 24
// FuncCodeReadDeviceIdentification for byte wise access
FuncCodeReadDeviceIdentification = 43
)
// meiType specifies a MEI Type as defined in https://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf#page=44
type meiType byte
const (
// meiTypeReadDeviceIdentification is used together with FuncCodeReadDeviceIdentification
meiTypeReadDeviceIdentification meiType = 14
)
// ReadDeviceIDCode specifies a Read Device ID Code as defined in https://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf#page=45
type ReadDeviceIDCode byte
const (
// ReadDeviceIDCodeBasic queries for VendorName, ProductCode, and MajorMinorRevision.
ReadDeviceIDCodeBasic ReadDeviceIDCode = iota + 1
// ReadDeviceIDCodeRegular queries for VendorURL, ProductName, ModelName, and UserApplicationName.
ReadDeviceIDCodeRegular
// ReadDeviceIDCodeExtended queries for regular and private (custom) objects.
ReadDeviceIDCodeExtended
// ReadDeviceIDCodeSpecific queries for specific objects.
ReadDeviceIDCodeSpecific
)
const (
// ExceptionCodeIllegalFunction error code
ExceptionCodeIllegalFunction = 1
// ExceptionCodeIllegalDataAddress error code
ExceptionCodeIllegalDataAddress = 2
// ExceptionCodeIllegalDataValue error code
ExceptionCodeIllegalDataValue = 3
// ExceptionCodeServerDeviceFailure error code
ExceptionCodeServerDeviceFailure = 4
// ExceptionCodeAcknowledge error code
ExceptionCodeAcknowledge = 5
// ExceptionCodeServerDeviceBusy error code
ExceptionCodeServerDeviceBusy = 6
// ExceptionCodeMemoryParityError error code
ExceptionCodeMemoryParityError = 8
// ExceptionCodeGatewayPathUnavailable error code
ExceptionCodeGatewayPathUnavailable = 10
// ExceptionCodeGatewayTargetDeviceFailedToRespond error code
ExceptionCodeGatewayTargetDeviceFailedToRespond = 11
)
// Error implements error interface.
type Error struct {
FunctionCode byte
ExceptionCode byte
}
// Error converts known modbus exception code to error message.
func (e *Error) Error() string {
var name string
switch e.ExceptionCode {
case ExceptionCodeIllegalFunction:
name = "illegal function"
case ExceptionCodeIllegalDataAddress:
name = "illegal data address"
case ExceptionCodeIllegalDataValue:
name = "illegal data value"
case ExceptionCodeServerDeviceFailure:
name = "server device failure"
case ExceptionCodeAcknowledge:
name = "acknowledge"
case ExceptionCodeServerDeviceBusy:
name = "server device busy"
case ExceptionCodeMemoryParityError:
name = "memory parity error"
case ExceptionCodeGatewayPathUnavailable:
name = "gateway path unavailable"
case ExceptionCodeGatewayTargetDeviceFailedToRespond:
name = "gateway target device failed to respond"
default:
name = "unknown"
}
return fmt.Sprintf("modbus: exception '%v' (%s), function '%v'", e.ExceptionCode, name, e.FunctionCode&0x7F)
}
// ProtocolDataUnit (PDU) is independent of underlying communication layers.
type ProtocolDataUnit struct {
FunctionCode byte
Data []byte
}
// Packager specifies the communication layer.
type Packager interface {
SetSlave(slaveID byte)
Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
Verify(aduRequest []byte, aduResponse []byte) (err error)
}
// Transporter specifies the transport layer.
type Transporter interface {
Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
}
// Connector exposes the underlying handler capability for open/connect and close the transport channel.
type Connector interface {
Connect(ctx context.Context) error
Close() error
}