forked from flosse/go-modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
174 lines (123 loc) · 3.28 KB
/
api.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (C) 2014 - 2015, Markus Kohlhase <[email protected]>
*
* Modbus API
*/
package modbus
type Transporter interface {
Connect() error
Close() error
Send(pdu *Pdu) (*Pdu, error)
}
type Client interface {
/* Access to transporter layer */
Transporter() Transporter
/**************
* Bit access *
**************/
/* Physical Discrete Inputs */
// Function Code 2
ReadDiscreteInputs(address, quantity uint16) (inputs []bool, err error)
/* Internal Bits Or Physical Coils */
// Function Code 1
ReadCoils(address, quantity uint16) (coils []bool, err error)
// Function Code 5
WriteSingleCoil(address uint16, coil bool) error
// Function Code 15
WriteMultipleCoils(address uint16, coils []bool) error
/******************
* 16 bits access *
******************/
/* Physical Input Registers */
// Function Code 4
ReadInputRegisters(address, quantity uint16) (readRegisters []uint16, err error)
// Function Code 3
ReadHoldingRegisters(address, quantity uint16) (readRegisters []uint16, err error)
// Function Code 6
WriteSingleRegister(address, value uint16) error
// Function Code 16
WriteMultipleRegisters(address uint16, values []uint16) error
// Function Code 23
ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress uint16, values []uint16) (readRegisters []uint16, err error)
// Function Code 22
MaskWriteRegister(address, andMask, orMask uint16) error
// Function Code 24
ReadFifoQueue(address uint16) (values []uint16, err error)
/**********************
* File record access *
**********************/
// TODO: specify methods
}
type SerialClient interface {
// Embed general client API
Client
/***************
* Diagnostics *
**************/
// Function Code 7
ReadExceptionStatus() (states []bool, err error)
// Function Code 8
Diagnostics(subfunction uint16, data []uint16) (response []uint16, err error)
// Function Code 11
GetCommEventCounter() (status bool, count uint16, err error)
// TODO: specify method
// Function Code 12
// GetCommEventLog
// Function Code 17
ReportServerId() (response []byte, err error)
// TODO: specify method
// Function Code 43
// ReadDeviceIdentification
}
type IoClient interface {
/********************
* Abstract Objects *
********************/
// Embed general client API
Client
// Discrete input
DiscreteInput(address uint16) DiscreteInput
// Coil
Coil(address uint16) Coil
// Input Register
InputRegister(address uint16) InputRegister
// Input Registers
InputRegisters(address, count uint16) InputRegisters
// Holding Register
HoldingRegister(address uint16) HoldingRegister
// Holding Registers
HoldingRegisters(address, count uint16) HoldingRegisters
}
type DiscreteInput interface {
Test() (bool, error)
}
type Coil interface {
DiscreteInput
Set() error
Clear() error
Toggle() error
}
type InputRegister interface {
Read() (uint16, error)
}
type InputRegisters interface {
Read() ([]uint16, error)
ReadString() (string, error)
}
type HoldingRegister interface {
InputRegister
Write(uint16) error
}
type HoldingRegisters interface {
InputRegisters
Write([]uint16) error
WriteString(s string) error
}
type Handler interface {
Handle(req *Pdu) (res *Pdu)
}
type Server interface {
SetHandler(h *Handler)
Start() error
Stop() error
}