modbus-rs 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -33
- package/index.d.ts +743 -129
- package/index.js +88 -52
- package/package.json +8 -8
package/index.d.ts
CHANGED
|
@@ -1,82 +1,260 @@
|
|
|
1
1
|
/* auto-generated by NAPI-RS */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Physical serial port in ASCII mode. Factory for device clients.
|
|
5
|
+
* Manages a physical serial port connection in ASCII mode.
|
|
6
|
+
* This class is a factory for creating lightweight `AsyncSerialModbusClient` instances
|
|
7
|
+
* that are bound to a specific unit ID and share the underlying serial connection.
|
|
8
|
+
*
|
|
9
|
+
* Use the static `open` method to establish a connection.
|
|
10
|
+
*/
|
|
4
11
|
export declare class AsyncAsciiTransport {
|
|
5
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* Opens the serial port in ASCII mode.
|
|
14
|
+
* Opens a serial port and establishes a connection in ASCII mode.
|
|
15
|
+
* @param opts Options for the serial ASCII transport.
|
|
16
|
+
* @param opts.portPath The path to the serial port (e.g., '/dev/ttyUSB0', 'COM3').
|
|
17
|
+
* @param opts.baudRate The baud rate for the serial communication.
|
|
18
|
+
* @returns A `Promise` that resolves to an `AsyncAsciiTransport` instance.
|
|
19
|
+
*/
|
|
6
20
|
static open(opts: AsciiTransportOptions): Promise<AsyncAsciiTransport>
|
|
7
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Creates a device client bound to the specified unit ID.
|
|
23
|
+
* Creates a lightweight device client that communicates through this transport.
|
|
24
|
+
* The client is bound to a specific Modbus unit ID.
|
|
25
|
+
* @param opts Options specifying the unit ID.
|
|
26
|
+
*/
|
|
8
27
|
createClient(opts: CreateClientOptions): AsyncSerialModbusClient
|
|
9
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Sets the per-request timeout in milliseconds.
|
|
30
|
+
* Sets the timeout for individual Modbus requests made through this transport.
|
|
31
|
+
* @param timeout_ms The timeout duration in milliseconds.
|
|
32
|
+
*/
|
|
10
33
|
setRequestTimeout(timeoutMs: number): void
|
|
11
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Clears the per-request timeout.
|
|
36
|
+
* Clears any previously set per-request timeout.
|
|
37
|
+
*/
|
|
12
38
|
clearRequestTimeout(): void
|
|
13
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Returns whether there are pending requests.
|
|
41
|
+
* Checks if there are any Modbus requests currently in-flight on this transport.
|
|
42
|
+
*/
|
|
14
43
|
get pendingRequests(): boolean
|
|
15
|
-
/**
|
|
44
|
+
/**
|
|
45
|
+
* Closes the transport.
|
|
46
|
+
* Closes the underlying serial port connection.
|
|
47
|
+
*/
|
|
16
48
|
close(): Promise<void>
|
|
17
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* Reconnects the transport after a disconnect.
|
|
51
|
+
* Attempts to re-establish the serial connection if it has been closed.
|
|
52
|
+
*/
|
|
18
53
|
reconnect(): Promise<void>
|
|
19
54
|
}
|
|
20
55
|
|
|
21
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Physical serial port in RTU mode. Factory for device clients.
|
|
58
|
+
* Manages a physical serial port connection in RTU mode.
|
|
59
|
+
* This class is a factory for creating lightweight `AsyncSerialModbusClient` instances
|
|
60
|
+
* that are bound to a specific unit ID and share the underlying serial connection.
|
|
61
|
+
*
|
|
62
|
+
* Use the static `open` method to establish a connection.
|
|
63
|
+
*/
|
|
22
64
|
export declare class AsyncRtuTransport {
|
|
23
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Opens the serial port in RTU mode.
|
|
67
|
+
* Opens a serial port and establishes a connection in RTU mode.
|
|
68
|
+
* @param opts Options for the serial RTU transport.
|
|
69
|
+
* @param opts.portPath The path to the serial port (e.g., '/dev/ttyUSB0', 'COM3').
|
|
70
|
+
* @param opts.baudRate The baud rate for the serial communication.
|
|
71
|
+
* @returns A `Promise` that resolves to an `AsyncRtuTransport` instance.
|
|
72
|
+
*/
|
|
24
73
|
static open(opts: RtuTransportOptions): Promise<AsyncRtuTransport>
|
|
25
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* Creates a device client bound to the specified unit ID.
|
|
76
|
+
* Creates a lightweight device client that communicates through this transport.
|
|
77
|
+
* The client is bound to a specific Modbus unit ID.
|
|
78
|
+
* @param opts Options specifying the unit ID.
|
|
79
|
+
*/
|
|
26
80
|
createClient(opts: CreateClientOptions): AsyncSerialModbusClient
|
|
27
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* Sets the per-request timeout in milliseconds.
|
|
83
|
+
* Sets the timeout for individual Modbus requests made through this transport.
|
|
84
|
+
* @param timeout_ms The timeout duration in milliseconds.
|
|
85
|
+
*/
|
|
28
86
|
setRequestTimeout(timeoutMs: number): void
|
|
29
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* Clears the per-request timeout.
|
|
89
|
+
* Clears any previously set per-request timeout.
|
|
90
|
+
*/
|
|
30
91
|
clearRequestTimeout(): void
|
|
31
|
-
/**
|
|
92
|
+
/**
|
|
93
|
+
* Returns whether there are pending requests.
|
|
94
|
+
* Checks if there are any Modbus requests currently in-flight on this transport.
|
|
95
|
+
*/
|
|
32
96
|
get pendingRequests(): boolean
|
|
33
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Closes the transport.
|
|
99
|
+
* Closes the underlying serial port connection.
|
|
100
|
+
*/
|
|
34
101
|
close(): Promise<void>
|
|
35
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* Reconnects the transport after a disconnect.
|
|
104
|
+
* Attempts to re-establish the serial connection if it has been closed.
|
|
105
|
+
*/
|
|
36
106
|
reconnect(): Promise<void>
|
|
37
107
|
}
|
|
38
108
|
|
|
39
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* Lightweight device client sharing the serial transport.
|
|
111
|
+
* A lightweight Modbus client that sends requests for a specific unit ID
|
|
112
|
+
* over a shared `AsyncRtuTransport` or `AsyncAsciiTransport`.
|
|
113
|
+
*/
|
|
40
114
|
export declare class AsyncSerialModbusClient {
|
|
41
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Reads holding registers (FC03).
|
|
117
|
+
* Reads one or more holding registers (Function Code 03).
|
|
118
|
+
* @param opts Options for reading registers.
|
|
119
|
+
* @param opts.address The starting register address.
|
|
120
|
+
* @param opts.quantity The number of registers to read.
|
|
121
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
122
|
+
*/
|
|
42
123
|
readHoldingRegisters(opts: ReadRegistersOptions): Promise<number[]>
|
|
43
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* Reads input registers (FC04).
|
|
126
|
+
* Reads one or more input registers (Function Code 04).
|
|
127
|
+
* @param opts Options for reading registers.
|
|
128
|
+
* @param opts.address The starting register address.
|
|
129
|
+
* @param opts.quantity The number of registers to read.
|
|
130
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
131
|
+
*/
|
|
44
132
|
readInputRegisters(opts: ReadRegistersOptions): Promise<number[]>
|
|
45
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* Writes a single register (FC06).
|
|
135
|
+
* Writes a single holding register (Function Code 06).
|
|
136
|
+
* @param opts Options for writing a single register.
|
|
137
|
+
* @param opts.address The register address.
|
|
138
|
+
* @param opts.value The 16-bit value to write.
|
|
139
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
140
|
+
*/
|
|
46
141
|
writeSingleRegister(opts: WriteSingleRegisterOptions): Promise<void>
|
|
47
|
-
/**
|
|
142
|
+
/**
|
|
143
|
+
* Writes multiple registers (FC16).
|
|
144
|
+
* Writes multiple consecutive holding registers (Function Code 16).
|
|
145
|
+
* @param opts Options for writing multiple registers.
|
|
146
|
+
* @param opts.address The starting register address.
|
|
147
|
+
* @param opts.values An array of 16-bit values to write.
|
|
148
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
149
|
+
*/
|
|
48
150
|
writeMultipleRegisters(opts: WriteMultipleRegistersOptions): Promise<void>
|
|
49
|
-
/**
|
|
151
|
+
/**
|
|
152
|
+
* Reads and writes multiple registers atomically (FC23).
|
|
153
|
+
* Performs an atomic read/write operation on multiple registers (Function Code 23).
|
|
154
|
+
* @param opts Options for the read/write operation.
|
|
155
|
+
* @param opts.read_address The starting address for the read operation.
|
|
156
|
+
* @param opts.read_quantity The number of registers to read.
|
|
157
|
+
* @param opts.write_address The starting address for the write operation.
|
|
158
|
+
* @param opts.write_values An array of 16-bit values to write.
|
|
159
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
160
|
+
*/
|
|
50
161
|
readWriteMultipleRegisters(opts: ReadWriteMultipleRegistersOptions): Promise<number[]>
|
|
51
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Reads coils (FC01).
|
|
164
|
+
* Reads the status of one or more coils (Function Code 01).
|
|
165
|
+
* @param opts Options for reading bits.
|
|
166
|
+
* @param opts.address The starting coil address.
|
|
167
|
+
* @param opts.quantity The number of coils to read.
|
|
168
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
169
|
+
*/
|
|
52
170
|
readCoils(opts: ReadBitsOptions): Promise<boolean[]>
|
|
53
|
-
/**
|
|
171
|
+
/**
|
|
172
|
+
* Writes a single coil (FC05).
|
|
173
|
+
* Writes a single coil (Function Code 05).
|
|
174
|
+
* @param opts Options for writing a single coil.
|
|
175
|
+
* @param opts.address The coil address.
|
|
176
|
+
* @param opts.value The boolean value to write (true for ON, false for OFF).
|
|
177
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
178
|
+
*/
|
|
54
179
|
writeSingleCoil(opts: WriteSingleCoilOptions): Promise<void>
|
|
55
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* Writes multiple coils (FC15).
|
|
182
|
+
* Writes multiple consecutive coils (Function Code 15).
|
|
183
|
+
* @param opts Options for writing multiple coils.
|
|
184
|
+
* @param opts.address The starting coil address.
|
|
185
|
+
* @param opts.values An array of boolean values to write.
|
|
186
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
187
|
+
*/
|
|
56
188
|
writeMultipleCoils(opts: WriteMultipleCoilsOptions): Promise<void>
|
|
57
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* Reads discrete inputs (FC02).
|
|
191
|
+
* Reads the status of one or more discrete inputs (Function Code 02).
|
|
192
|
+
* @param opts Options for reading bits.
|
|
193
|
+
* @param opts.address The starting discrete input address.
|
|
194
|
+
* @param opts.quantity The number of discrete inputs to read.
|
|
195
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
196
|
+
*/
|
|
58
197
|
readDiscreteInputs(opts: ReadBitsOptions): Promise<boolean[]>
|
|
59
|
-
/**
|
|
198
|
+
/**
|
|
199
|
+
* Reads FIFO queue (FC24).
|
|
200
|
+
* Reads from a FIFO queue register (Function Code 24).
|
|
201
|
+
* @param opts Options for reading the FIFO queue.
|
|
202
|
+
* @param opts.address The FIFO pointer address.
|
|
203
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
204
|
+
*/
|
|
60
205
|
readFifoQueue(opts: ReadFifoQueueOptions): Promise<FifoQueueResponse>
|
|
61
|
-
/**
|
|
206
|
+
/**
|
|
207
|
+
* Reads file records (FC20).
|
|
208
|
+
* Reads one or more file records (Function Code 20).
|
|
209
|
+
* @param opts Options for reading file records.
|
|
210
|
+
* @param opts.requests An array of file record read sub-requests.
|
|
211
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
212
|
+
*/
|
|
62
213
|
readFileRecord(opts: ReadFileRecordOptions): Promise<number[][]>
|
|
63
|
-
/**
|
|
214
|
+
/**
|
|
215
|
+
* Writes file records (FC21).
|
|
216
|
+
* Writes one or more file records (Function Code 21).
|
|
217
|
+
* @param opts Options for writing file records.
|
|
218
|
+
* @param opts.requests An array of file record write sub-requests.
|
|
219
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
220
|
+
*/
|
|
64
221
|
writeFileRecord(opts: WriteFileRecordOptions): Promise<void>
|
|
65
|
-
/**
|
|
222
|
+
/**
|
|
223
|
+
* Reads exception status (FC07).
|
|
224
|
+
* Reads the device's exception status byte (Function Code 07).
|
|
225
|
+
*/
|
|
66
226
|
readExceptionStatus(): Promise<number>
|
|
67
|
-
/**
|
|
227
|
+
/**
|
|
228
|
+
* Sends a diagnostics request (FC08).
|
|
229
|
+
* Executes a diagnostic function on the device (Function Code 08).
|
|
230
|
+
* @param opts Options for the diagnostics request.
|
|
231
|
+
* @param opts.sub_function The diagnostic sub-function code.
|
|
232
|
+
* @param opts.data Data to be sent with the request.
|
|
233
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
234
|
+
*/
|
|
68
235
|
diagnostics(opts: DiagnosticsOptions): Promise<DiagnosticsResponse>
|
|
69
|
-
/**
|
|
236
|
+
/**
|
|
237
|
+
* Reads device identification (FC43/MEI14).
|
|
238
|
+
* Reads device identification information (Function Code 43, MEI 14).
|
|
239
|
+
* @param opts Options for reading device identification.
|
|
240
|
+
* @param opts.read_device_id_code The category of objects to read (e.g., basic, regular).
|
|
241
|
+
* @param opts.object_id The ID of the first object to read.
|
|
242
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
243
|
+
*/
|
|
70
244
|
readDeviceIdentification(opts: ReadDeviceIdentificationOptions): Promise<DeviceIdentificationResponse>
|
|
71
245
|
}
|
|
72
246
|
|
|
73
|
-
/**
|
|
247
|
+
/**
|
|
248
|
+
* Async Modbus Serial server supporting RTU and ASCII transports.
|
|
249
|
+
* An asynchronous Modbus server that listens on a serial port for incoming requests.
|
|
250
|
+
* It supports both RTU and ASCII transport modes.
|
|
251
|
+
* Use the static `bindRtu` or `bindAscii` methods to create and start a server instance.
|
|
252
|
+
*/
|
|
74
253
|
export declare class AsyncSerialModbusServer {
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/** Stops the server. */
|
|
254
|
+
/**
|
|
255
|
+
* Stops the server.
|
|
256
|
+
* Stops the server and closes the serial port.
|
|
257
|
+
*/
|
|
80
258
|
shutdown(): Promise<void>
|
|
81
259
|
}
|
|
82
260
|
|
|
@@ -98,73 +276,222 @@ export declare class AsyncTcpGateway {
|
|
|
98
276
|
shutdown(): Promise<void>
|
|
99
277
|
}
|
|
100
278
|
|
|
101
|
-
/**
|
|
279
|
+
/**
|
|
280
|
+
* Lightweight device client sharing the TCP transport.
|
|
281
|
+
* A lightweight Modbus client that sends requests for a specific unit ID
|
|
282
|
+
* over a shared `AsyncTcpTransport`.
|
|
283
|
+
*/
|
|
102
284
|
export declare class AsyncTcpModbusClient {
|
|
103
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* Reads holding registers (FC03).
|
|
287
|
+
* Reads one or more holding registers (Function Code 03).
|
|
288
|
+
* @param opts Options for reading registers.
|
|
289
|
+
* @param opts.address The starting register address.
|
|
290
|
+
* @param opts.quantity The number of registers to read.
|
|
291
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
292
|
+
*/
|
|
104
293
|
readHoldingRegisters(opts: ReadRegistersOptions): Promise<number[]>
|
|
105
|
-
/**
|
|
294
|
+
/**
|
|
295
|
+
* Reads input registers (FC04).
|
|
296
|
+
* Reads one or more input registers (Function Code 04).
|
|
297
|
+
* @param opts Options for reading registers.
|
|
298
|
+
* @param opts.address The starting register address.
|
|
299
|
+
* @param opts.quantity The number of registers to read.
|
|
300
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
301
|
+
*/
|
|
106
302
|
readInputRegisters(opts: ReadRegistersOptions): Promise<number[]>
|
|
107
|
-
/**
|
|
303
|
+
/**
|
|
304
|
+
* Writes a single register (FC06).
|
|
305
|
+
* Writes a single holding register (Function Code 06).
|
|
306
|
+
* @param opts Options for writing a single register.
|
|
307
|
+
* @param opts.address The register address.
|
|
308
|
+
* @param opts.value The 16-bit value to write.
|
|
309
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
310
|
+
*/
|
|
108
311
|
writeSingleRegister(opts: WriteSingleRegisterOptions): Promise<void>
|
|
109
|
-
/**
|
|
312
|
+
/**
|
|
313
|
+
* Writes multiple registers (FC16).
|
|
314
|
+
* Writes multiple consecutive holding registers (Function Code 16).
|
|
315
|
+
* @param opts Options for writing multiple registers.
|
|
316
|
+
* @param opts.address The starting register address.
|
|
317
|
+
* @param opts.values An array of 16-bit values to write.
|
|
318
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
319
|
+
*/
|
|
110
320
|
writeMultipleRegisters(opts: WriteMultipleRegistersOptions): Promise<void>
|
|
111
|
-
/**
|
|
321
|
+
/**
|
|
322
|
+
* Reads and writes multiple registers atomically (FC23).
|
|
323
|
+
* Performs an atomic read/write operation on multiple registers (Function Code 23).
|
|
324
|
+
* @param opts Options for the read/write operation.
|
|
325
|
+
* @param opts.read_address The starting address for the read operation.
|
|
326
|
+
* @param opts.read_quantity The number of registers to read.
|
|
327
|
+
* @param opts.write_address The starting address for the write operation.
|
|
328
|
+
* @param opts.write_values An array of 16-bit values to write.
|
|
329
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
330
|
+
*/
|
|
112
331
|
readWriteMultipleRegisters(opts: ReadWriteMultipleRegistersOptions): Promise<number[]>
|
|
113
|
-
/**
|
|
332
|
+
/**
|
|
333
|
+
* Reads coils (FC01).
|
|
334
|
+
* Reads the status of one or more coils (Function Code 01).
|
|
335
|
+
* @param opts Options for reading bits.
|
|
336
|
+
* @param opts.address The starting coil address.
|
|
337
|
+
* @param opts.quantity The number of coils to read.
|
|
338
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
339
|
+
*/
|
|
114
340
|
readCoils(opts: ReadBitsOptions): Promise<boolean[]>
|
|
115
|
-
/**
|
|
341
|
+
/**
|
|
342
|
+
* Writes a single coil (FC05).
|
|
343
|
+
* Writes a single coil (Function Code 05).
|
|
344
|
+
* @param opts Options for writing a single coil.
|
|
345
|
+
* @param opts.address The coil address.
|
|
346
|
+
* @param opts.value The boolean value to write (true for ON, false for OFF).
|
|
347
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
348
|
+
*/
|
|
116
349
|
writeSingleCoil(opts: WriteSingleCoilOptions): Promise<void>
|
|
117
|
-
/**
|
|
350
|
+
/**
|
|
351
|
+
* Writes multiple coils (FC15).
|
|
352
|
+
* Writes multiple consecutive coils (Function Code 15).
|
|
353
|
+
* @param opts Options for writing multiple coils.
|
|
354
|
+
* @param opts.address The starting coil address.
|
|
355
|
+
* @param opts.values An array of boolean values to write.
|
|
356
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
357
|
+
*/
|
|
118
358
|
writeMultipleCoils(opts: WriteMultipleCoilsOptions): Promise<void>
|
|
119
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* Reads discrete inputs (FC02).
|
|
361
|
+
* Reads the status of one or more discrete inputs (Function Code 02).
|
|
362
|
+
* @param opts Options for reading bits.
|
|
363
|
+
* @param opts.address The starting discrete input address.
|
|
364
|
+
* @param opts.quantity The number of discrete inputs to read.
|
|
365
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
366
|
+
*/
|
|
120
367
|
readDiscreteInputs(opts: ReadBitsOptions): Promise<boolean[]>
|
|
121
|
-
/**
|
|
368
|
+
/**
|
|
369
|
+
* Reads FIFO queue (FC24).
|
|
370
|
+
* Reads from a FIFO queue register (Function Code 24).
|
|
371
|
+
* @param opts Options for reading the FIFO queue.
|
|
372
|
+
* @param opts.address The FIFO pointer address.
|
|
373
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
374
|
+
*/
|
|
122
375
|
readFifoQueue(opts: ReadFifoQueueOptions): Promise<FifoQueueResponse>
|
|
123
|
-
/**
|
|
376
|
+
/**
|
|
377
|
+
* Reads file records (FC20).
|
|
378
|
+
* Reads one or more file records (Function Code 20).
|
|
379
|
+
* @param opts Options for reading file records.
|
|
380
|
+
* @param opts.requests An array of file record read sub-requests.
|
|
381
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
382
|
+
*/
|
|
124
383
|
readFileRecord(opts: ReadFileRecordOptions): Promise<number[][]>
|
|
125
|
-
/**
|
|
384
|
+
/**
|
|
385
|
+
* Writes file records (FC21).
|
|
386
|
+
* Writes one or more file records (Function Code 21).
|
|
387
|
+
* @param opts Options for writing file records.
|
|
388
|
+
* @param opts.requests An array of file record write sub-requests.
|
|
389
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
390
|
+
*/
|
|
126
391
|
writeFileRecord(opts: WriteFileRecordOptions): Promise<void>
|
|
127
|
-
/**
|
|
392
|
+
/**
|
|
393
|
+
* Reads exception status (FC07).
|
|
394
|
+
* Reads the device's exception status byte (Function Code 07).
|
|
395
|
+
*/
|
|
128
396
|
readExceptionStatus(): Promise<number>
|
|
129
|
-
/**
|
|
397
|
+
/**
|
|
398
|
+
* Sends a diagnostics request (FC08).
|
|
399
|
+
* Executes a diagnostic function on the device (Function Code 08).
|
|
400
|
+
* @param opts Options for the diagnostics request.
|
|
401
|
+
* @param opts.sub_function The diagnostic sub-function code.
|
|
402
|
+
* @param opts.data Data to be sent with the request.
|
|
403
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
404
|
+
*/
|
|
130
405
|
diagnostics(opts: DiagnosticsOptions): Promise<DiagnosticsResponse>
|
|
131
|
-
/**
|
|
406
|
+
/**
|
|
407
|
+
* Reads device identification (FC43/MEI14).
|
|
408
|
+
* Reads device identification information (Function Code 43, MEI 14).
|
|
409
|
+
* @param opts Options for reading device identification.
|
|
410
|
+
* @param opts.read_device_id_code The category of objects to read (e.g., basic, regular).
|
|
411
|
+
* @param opts.object_id The ID of the first object to read.
|
|
412
|
+
* @param opts.signal An optional `AbortSignal` to cancel the operation.
|
|
413
|
+
*/
|
|
132
414
|
readDeviceIdentification(opts: ReadDeviceIdentificationOptions): Promise<DeviceIdentificationResponse>
|
|
133
415
|
}
|
|
134
416
|
|
|
135
417
|
/**
|
|
136
|
-
*
|
|
418
|
+
* Async Modbus TCP server.
|
|
137
419
|
*
|
|
138
|
-
*
|
|
420
|
+
* Binds to a TCP port and handles incoming Modbus requests using JS callbacks.
|
|
421
|
+
* An asynchronous Modbus TCP server that listens for incoming client connections.
|
|
139
422
|
*/
|
|
140
423
|
export declare class AsyncTcpModbusServer {
|
|
141
424
|
/**
|
|
142
|
-
*
|
|
425
|
+
* Creates and starts a new TCP server.
|
|
426
|
+
* Creates and starts a new Modbus TCP server.
|
|
143
427
|
*
|
|
144
|
-
* @param opts
|
|
145
|
-
* @param
|
|
146
|
-
* @
|
|
428
|
+
* @param opts Server bind options.
|
|
429
|
+
* @param opts.host The host address to bind to (e.g., '0.0.0.0').
|
|
430
|
+
* @param opts.port The TCP port to listen on.
|
|
431
|
+
* @param opts.unitId The Modbus unit ID the server will respond to.
|
|
432
|
+
* @param handlers An object containing callback functions to handle Modbus requests (matches the `ServerHandlers` interface in TypeScript).
|
|
433
|
+
* @returns A `Promise` that resolves to a running `AsyncTcpModbusServer` instance.
|
|
434
|
+
*/
|
|
435
|
+
static bind(opts: TcpServerOptions, handlers: ServerHandlers): Promise<AsyncTcpModbusServer>
|
|
436
|
+
/**
|
|
437
|
+
* Stops the server.
|
|
438
|
+
* Stops the server and closes all active connections.
|
|
147
439
|
*/
|
|
148
|
-
static bind(opts: TcpServerOptions, handlers: object): AsyncTcpModbusServer
|
|
149
|
-
/** Stops the server. */
|
|
150
440
|
shutdown(): Promise<void>
|
|
151
441
|
}
|
|
152
442
|
|
|
153
|
-
/**
|
|
443
|
+
/**
|
|
444
|
+
* Physical TCP socket connection to a Modbus device or gateway.
|
|
445
|
+
* Manages a physical TCP socket connection to a Modbus device or gateway.
|
|
446
|
+
* This class is a factory for creating lightweight `AsyncTcpModbusClient` instances
|
|
447
|
+
* that are bound to a specific unit ID and share the underlying TCP connection.
|
|
448
|
+
*
|
|
449
|
+
* Use the static `connect` method to establish a connection.
|
|
450
|
+
*/
|
|
154
451
|
export declare class AsyncTcpTransport {
|
|
155
|
-
/**
|
|
452
|
+
/**
|
|
453
|
+
* Connects to a Modbus TCP device or gateway.
|
|
454
|
+
* Establishes a connection to a Modbus TCP device or gateway.
|
|
455
|
+
*
|
|
456
|
+
* @param opts - Connection options including host, port, and timeout settings.
|
|
457
|
+
* @param opts.host - Target host address (IP or hostname).
|
|
458
|
+
* @param opts.port - Target TCP port (typically 502).
|
|
459
|
+
*/
|
|
156
460
|
static connect(opts: TcpTransportOptions): Promise<AsyncTcpTransport>
|
|
157
|
-
/**
|
|
158
|
-
|
|
159
|
-
|
|
461
|
+
/**
|
|
462
|
+
* Creates a device client bound to the specified unit ID.
|
|
463
|
+
* Creates a lightweight device client that communicates through this transport.
|
|
464
|
+
* The client is bound to a specific Modbus unit ID.
|
|
465
|
+
*
|
|
466
|
+
* @param opts - Options specifying the unit ID.
|
|
467
|
+
*/
|
|
468
|
+
createClient(opts: CreateClientOptions): AsyncTcpModbusClient
|
|
469
|
+
/**
|
|
470
|
+
* Sets the per-request timeout in milliseconds.
|
|
471
|
+
* Sets the timeout for individual Modbus requests made through this transport.
|
|
472
|
+
*
|
|
473
|
+
* @param timeout_ms - The timeout duration in milliseconds.
|
|
474
|
+
*/
|
|
160
475
|
setRequestTimeout(timeoutMs: number): void
|
|
161
|
-
/**
|
|
476
|
+
/**
|
|
477
|
+
* Clears the per-request timeout.
|
|
478
|
+
* Clears any previously set per-request timeout, reverting to the default behavior.
|
|
479
|
+
*/
|
|
162
480
|
clearRequestTimeout(): void
|
|
163
|
-
/**
|
|
481
|
+
/**
|
|
482
|
+
* Returns whether there are pending requests.
|
|
483
|
+
* Checks if there are any Modbus requests currently in-flight on this transport.
|
|
484
|
+
*/
|
|
164
485
|
get pendingRequests(): boolean
|
|
165
|
-
/**
|
|
486
|
+
/**
|
|
487
|
+
* Closes the connection.
|
|
488
|
+
* Closes the underlying TCP connection and shuts down the transport.
|
|
489
|
+
*/
|
|
166
490
|
close(): Promise<void>
|
|
167
|
-
/**
|
|
491
|
+
/**
|
|
492
|
+
* Reconnects the transport after a disconnect.
|
|
493
|
+
* Attempts to re-establish the TCP connection if it has been closed.
|
|
494
|
+
*/
|
|
168
495
|
reconnect(): Promise<void>
|
|
169
496
|
}
|
|
170
497
|
|
|
@@ -184,31 +511,37 @@ export interface AsciiTransportOptions {
|
|
|
184
511
|
responseTimeoutMs?: number
|
|
185
512
|
/** Per-request timeout in milliseconds. */
|
|
186
513
|
requestTimeoutMs?: number
|
|
514
|
+
/** Number of retry attempts on failure (0 = none). Default: 0. */
|
|
515
|
+
retryAttempts?: number
|
|
516
|
+
/** Delay between retry attempts in milliseconds. */
|
|
517
|
+
retryDelayMs?: number
|
|
518
|
+
/** Backoff strategy: "immediate", "fixed", or "exponential". Default: "immediate". */
|
|
519
|
+
retryBackoffStrategy?: string
|
|
187
520
|
}
|
|
188
521
|
|
|
189
522
|
/** Options for creating a device client. */
|
|
190
523
|
export interface CreateClientOptions {
|
|
191
|
-
/** Modbus unit ID (1
|
|
192
|
-
unitId
|
|
524
|
+
/** Modbus unit ID (slave address) from 1 to 247. */
|
|
525
|
+
unitId: number
|
|
193
526
|
}
|
|
194
527
|
|
|
195
528
|
/** Device identification object. */
|
|
196
529
|
export interface DeviceIdentificationObject {
|
|
197
|
-
/**
|
|
530
|
+
/** The object ID. */
|
|
198
531
|
id: number
|
|
199
|
-
/**
|
|
532
|
+
/** The value of the object, represented as a string. */
|
|
200
533
|
value: string
|
|
201
534
|
}
|
|
202
535
|
|
|
203
536
|
/** Response from device identification request. */
|
|
204
537
|
export interface DeviceIdentificationResponse {
|
|
205
|
-
/**
|
|
538
|
+
/** The conformity level of the device. */
|
|
206
539
|
conformityLevel: number
|
|
207
|
-
/**
|
|
540
|
+
/** Indicates if more objects are available to be read. */
|
|
208
541
|
moreFollows: boolean
|
|
209
|
-
/**
|
|
542
|
+
/** The ID of the next object to read if `moreFollows` is true. */
|
|
210
543
|
nextObjectId: number
|
|
211
|
-
/**
|
|
544
|
+
/** An array of device identification objects. */
|
|
212
545
|
objects: Array<DeviceIdentificationObject>
|
|
213
546
|
}
|
|
214
547
|
|
|
@@ -216,24 +549,27 @@ export interface DeviceIdentificationResponse {
|
|
|
216
549
|
export interface DiagnosticsOptions {
|
|
217
550
|
/** Diagnostic sub-function code. */
|
|
218
551
|
subFunction: number
|
|
219
|
-
/** Data
|
|
552
|
+
/** Data to be sent with the diagnostics request. */
|
|
220
553
|
data: Array<number>
|
|
221
|
-
/**
|
|
222
|
-
signal?:
|
|
554
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
555
|
+
signal?: AbortSignal
|
|
223
556
|
}
|
|
224
557
|
|
|
225
558
|
/** Handler request for diagnostics. */
|
|
226
559
|
export interface DiagnosticsRequest {
|
|
560
|
+
/** The unit ID of the device that sent the request. */
|
|
227
561
|
unitId: number
|
|
562
|
+
/** The diagnostic sub-function code to execute. */
|
|
228
563
|
subFunction: number
|
|
564
|
+
/** Data sent with the diagnostics request. */
|
|
229
565
|
data: Array<number>
|
|
230
566
|
}
|
|
231
567
|
|
|
232
568
|
/** Response from diagnostics request. */
|
|
233
569
|
export interface DiagnosticsResponse {
|
|
234
|
-
/**
|
|
570
|
+
/** The sub-function code from the request. */
|
|
235
571
|
subFunction: number
|
|
236
|
-
/**
|
|
572
|
+
/** The data returned by the diagnostics function. */
|
|
237
573
|
data: Array<number>
|
|
238
574
|
}
|
|
239
575
|
|
|
@@ -247,29 +583,47 @@ export interface DownstreamConfig {
|
|
|
247
583
|
|
|
248
584
|
/** Response from reading FIFO queue. */
|
|
249
585
|
export interface FifoQueueResponse {
|
|
250
|
-
/** Number of values in the queue. */
|
|
251
|
-
count: number
|
|
252
586
|
/** Queue values. */
|
|
253
587
|
values: Array<number>
|
|
254
588
|
}
|
|
255
589
|
|
|
256
590
|
/** A single file record read sub-request. */
|
|
257
591
|
export interface FileRecordReadRequest {
|
|
258
|
-
/**
|
|
592
|
+
/** The file number (1-65535). */
|
|
259
593
|
fileNumber: number
|
|
260
|
-
/**
|
|
594
|
+
/** The starting record number within the file. */
|
|
261
595
|
recordNumber: number
|
|
262
|
-
/**
|
|
596
|
+
/** The number of records to read. */
|
|
597
|
+
recordLength: number
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** A single file record read sub-request on the server side. */
|
|
601
|
+
export interface FileRecordReadServerSubRequest {
|
|
602
|
+
/** The file number (1-65535). */
|
|
603
|
+
fileNumber: number
|
|
604
|
+
/** The starting record number within the file. */
|
|
605
|
+
recordNumber: number
|
|
606
|
+
/** The number of records to read. */
|
|
263
607
|
recordLength: number
|
|
264
608
|
}
|
|
265
609
|
|
|
266
610
|
/** A single file record write sub-request. */
|
|
267
611
|
export interface FileRecordWriteRequest {
|
|
268
|
-
/**
|
|
612
|
+
/** The file number (1-65535). */
|
|
613
|
+
fileNumber: number
|
|
614
|
+
/** The starting record number within the file. */
|
|
615
|
+
recordNumber: number
|
|
616
|
+
/** The record data to write, as an array of 16-bit values. */
|
|
617
|
+
recordData: Array<number>
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** A single file record write sub-request on the server side. */
|
|
621
|
+
export interface FileRecordWriteSubRequest {
|
|
622
|
+
/** The file number (1-65535). */
|
|
269
623
|
fileNumber: number
|
|
270
|
-
/**
|
|
624
|
+
/** The starting record number within the file. */
|
|
271
625
|
recordNumber: number
|
|
272
|
-
/**
|
|
626
|
+
/** The record data to write, as an array of 16-bit values. */
|
|
273
627
|
recordData: Array<number>
|
|
274
628
|
}
|
|
275
629
|
|
|
@@ -289,20 +643,41 @@ export interface GatewayConfig {
|
|
|
289
643
|
routes: Array<RouteEntry>
|
|
290
644
|
}
|
|
291
645
|
|
|
646
|
+
/** Stable error code: The connection was closed. */
|
|
647
|
+
export const MODBUS_ERROR_CODE_CONNECTION_CLOSED: string
|
|
648
|
+
|
|
649
|
+
/** Stable error code: Modbus protocol exception received. */
|
|
650
|
+
export const MODBUS_ERROR_CODE_EXCEPTION: string
|
|
651
|
+
|
|
652
|
+
/** Stable error code: Internal library error. */
|
|
653
|
+
export const MODBUS_ERROR_CODE_INTERNAL: string
|
|
654
|
+
|
|
655
|
+
/** Stable error code: Invalid argument passed to the API. */
|
|
656
|
+
export const MODBUS_ERROR_CODE_INVALID_ARGUMENT: string
|
|
657
|
+
|
|
658
|
+
/** Stable error code: Request timed out. */
|
|
659
|
+
export const MODBUS_ERROR_CODE_TIMEOUT: string
|
|
660
|
+
|
|
661
|
+
/** Stable error code: Transport/framing error. */
|
|
662
|
+
export const MODBUS_ERROR_CODE_TRANSPORT: string
|
|
663
|
+
|
|
292
664
|
/** Options for reading coils or discrete inputs. */
|
|
293
665
|
export interface ReadBitsOptions {
|
|
294
666
|
/** Starting address. */
|
|
295
667
|
address: number
|
|
296
|
-
/** Number of bits to read. */
|
|
668
|
+
/** Number of bits (coils or discrete inputs) to read. */
|
|
297
669
|
quantity: number
|
|
298
|
-
/**
|
|
299
|
-
signal?:
|
|
670
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
671
|
+
signal?: AbortSignal
|
|
300
672
|
}
|
|
301
673
|
|
|
302
674
|
/** Handler request for reading coils. */
|
|
303
675
|
export interface ReadCoilsRequest {
|
|
676
|
+
/** The unit ID of the device that sent the request. */
|
|
304
677
|
unitId: number
|
|
678
|
+
/** The starting address of the coils to read. */
|
|
305
679
|
address: number
|
|
680
|
+
/** The number of coils to read. */
|
|
306
681
|
quantity: number
|
|
307
682
|
}
|
|
308
683
|
|
|
@@ -310,52 +685,77 @@ export interface ReadCoilsRequest {
|
|
|
310
685
|
export interface ReadDeviceIdentificationOptions {
|
|
311
686
|
/** Read device ID code (1=basic, 2=regular, 3=extended, 4=individual). */
|
|
312
687
|
readDeviceIdCode: number
|
|
313
|
-
/**
|
|
688
|
+
/** The ID of the first object to read. */
|
|
314
689
|
objectId: number
|
|
315
|
-
/**
|
|
316
|
-
signal?:
|
|
690
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
691
|
+
signal?: AbortSignal
|
|
317
692
|
}
|
|
318
693
|
|
|
319
694
|
/** Handler request for reading discrete inputs. */
|
|
320
695
|
export interface ReadDiscreteInputsRequest {
|
|
696
|
+
/** The unit ID of the device that sent the request. */
|
|
321
697
|
unitId: number
|
|
698
|
+
/** The starting address of the discrete inputs to read. */
|
|
322
699
|
address: number
|
|
700
|
+
/** The number of discrete inputs to read. */
|
|
323
701
|
quantity: number
|
|
324
702
|
}
|
|
325
703
|
|
|
704
|
+
/** Handler request for reading exception status. */
|
|
705
|
+
export interface ReadExceptionStatusRequest {
|
|
706
|
+
/** The unit ID of the device that sent the request. */
|
|
707
|
+
unitId: number
|
|
708
|
+
}
|
|
709
|
+
|
|
326
710
|
/** Options for reading FIFO queue. */
|
|
327
711
|
export interface ReadFifoQueueOptions {
|
|
328
712
|
/** FIFO pointer address. */
|
|
329
713
|
address: number
|
|
330
|
-
/**
|
|
331
|
-
signal?:
|
|
714
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
715
|
+
signal?: AbortSignal
|
|
332
716
|
}
|
|
333
717
|
|
|
334
718
|
/** Handler request for reading FIFO queue. */
|
|
335
719
|
export interface ReadFifoQueueRequest {
|
|
720
|
+
/** The unit ID of the device that sent the request. */
|
|
336
721
|
unitId: number
|
|
722
|
+
/** The address of the FIFO queue pointer register. */
|
|
337
723
|
address: number
|
|
338
724
|
}
|
|
339
725
|
|
|
340
726
|
/** Options for reading file records. */
|
|
341
727
|
export interface ReadFileRecordOptions {
|
|
342
|
-
/**
|
|
728
|
+
/** An array of file record read sub-requests. */
|
|
343
729
|
requests: Array<FileRecordReadRequest>
|
|
344
|
-
/**
|
|
345
|
-
signal?:
|
|
730
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
731
|
+
signal?: AbortSignal
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** Handler request for reading file records. */
|
|
735
|
+
export interface ReadFileRecordRequest {
|
|
736
|
+
/** The unit ID of the device that sent the request. */
|
|
737
|
+
unitId: number
|
|
738
|
+
/** An array of file record read sub-requests. */
|
|
739
|
+
requests: Array<FileRecordReadServerSubRequest>
|
|
346
740
|
}
|
|
347
741
|
|
|
348
742
|
/** Handler request for reading holding registers. */
|
|
349
743
|
export interface ReadHoldingRegistersRequest {
|
|
744
|
+
/** The unit ID of the device that sent the request. */
|
|
350
745
|
unitId: number
|
|
746
|
+
/** The starting address of the holding registers to read. */
|
|
351
747
|
address: number
|
|
748
|
+
/** The number of holding registers to read. */
|
|
352
749
|
quantity: number
|
|
353
750
|
}
|
|
354
751
|
|
|
355
752
|
/** Handler request for reading input registers. */
|
|
356
753
|
export interface ReadInputRegistersRequest {
|
|
754
|
+
/** The unit ID of the device that sent the request. */
|
|
357
755
|
unitId: number
|
|
756
|
+
/** The starting address of the input registers to read. */
|
|
358
757
|
address: number
|
|
758
|
+
/** The number of input registers to read. */
|
|
359
759
|
quantity: number
|
|
360
760
|
}
|
|
361
761
|
|
|
@@ -363,24 +763,38 @@ export interface ReadInputRegistersRequest {
|
|
|
363
763
|
export interface ReadRegistersOptions {
|
|
364
764
|
/** Starting register address. */
|
|
365
765
|
address: number
|
|
366
|
-
/** Number of registers to read. */
|
|
766
|
+
/** Number of registers to read (quantity). */
|
|
367
767
|
quantity: number
|
|
368
|
-
/**
|
|
369
|
-
signal?:
|
|
768
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
769
|
+
signal?: AbortSignal
|
|
370
770
|
}
|
|
371
771
|
|
|
372
772
|
/** Options for read/write multiple registers (FC23). */
|
|
373
773
|
export interface ReadWriteMultipleRegistersOptions {
|
|
374
774
|
/** Starting address for read operation. */
|
|
375
775
|
readAddress: number
|
|
376
|
-
/** Number of registers to read. */
|
|
776
|
+
/** Number of registers to read (quantity). */
|
|
377
777
|
readQuantity: number
|
|
378
778
|
/** Starting address for write operation. */
|
|
379
779
|
writeAddress: number
|
|
380
|
-
/**
|
|
780
|
+
/** An array of 16-bit values to write. */
|
|
781
|
+
writeValues: Array<number>
|
|
782
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
783
|
+
signal?: AbortSignal
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/** Handler request for read/write multiple registers (FC23). */
|
|
787
|
+
export interface ReadWriteMultipleRegistersRequest {
|
|
788
|
+
/** The unit ID of the device that sent the request. */
|
|
789
|
+
unitId: number
|
|
790
|
+
/** The starting address for the read operation. */
|
|
791
|
+
readAddress: number
|
|
792
|
+
/** The number of registers to read. */
|
|
793
|
+
readQuantity: number
|
|
794
|
+
/** The starting address for the write operation. */
|
|
795
|
+
writeAddress: number
|
|
796
|
+
/** The array of 16-bit values to write. */
|
|
381
797
|
writeValues: Array<number>
|
|
382
|
-
/** Optional abort signal to cancel the request. */
|
|
383
|
-
signal?: object
|
|
384
798
|
}
|
|
385
799
|
|
|
386
800
|
/** Route entry mapping unit ID to a downstream channel. */
|
|
@@ -407,6 +821,12 @@ export interface RtuTransportOptions {
|
|
|
407
821
|
responseTimeoutMs?: number
|
|
408
822
|
/** Per-request timeout in milliseconds. */
|
|
409
823
|
requestTimeoutMs?: number
|
|
824
|
+
/** Number of retry attempts on failure (0 = none). Default: 0. */
|
|
825
|
+
retryAttempts?: number
|
|
826
|
+
/** Delay between retry attempts in milliseconds. */
|
|
827
|
+
retryDelayMs?: number
|
|
828
|
+
/** Backoff strategy: "immediate", "fixed", or "exponential". Default: "immediate". */
|
|
829
|
+
retryBackoffStrategy?: string
|
|
410
830
|
}
|
|
411
831
|
|
|
412
832
|
/** Server bind options for serial port. */
|
|
@@ -421,7 +841,7 @@ export interface SerialServerOptions {
|
|
|
421
841
|
parity?: string
|
|
422
842
|
/** Stop bits (1 or 2). */
|
|
423
843
|
stopBits?: number
|
|
424
|
-
/** Modbus unit ID (1-247). */
|
|
844
|
+
/** Modbus unit ID (1-247) for the server to respond to. */
|
|
425
845
|
unitId: number
|
|
426
846
|
/** Response timeout in milliseconds. */
|
|
427
847
|
responseTimeoutMs?: number
|
|
@@ -429,13 +849,18 @@ export interface SerialServerOptions {
|
|
|
429
849
|
|
|
430
850
|
/** Handler response for diagnostics. */
|
|
431
851
|
export interface ServerDiagnosticsResponse {
|
|
852
|
+
/** The sub-function code from the request. */
|
|
432
853
|
subFunction: number
|
|
854
|
+
/** The data to be returned by the diagnostics function. */
|
|
433
855
|
data: Array<number>
|
|
434
856
|
}
|
|
435
857
|
|
|
436
858
|
/** Response that may include an exception code. */
|
|
437
859
|
export interface ServerExceptionResponse {
|
|
438
|
-
/**
|
|
860
|
+
/**
|
|
861
|
+
* The Modbus exception code (e.g., 1 for Illegal Function).
|
|
862
|
+
* Modbus exception code if this is an error response.
|
|
863
|
+
*/
|
|
439
864
|
exception?: number
|
|
440
865
|
}
|
|
441
866
|
|
|
@@ -455,32 +880,49 @@ export interface TcpTransportOptions {
|
|
|
455
880
|
host: string
|
|
456
881
|
/** Target TCP port (typically 502). */
|
|
457
882
|
port: number
|
|
458
|
-
/** Per-request timeout in milliseconds
|
|
459
|
-
|
|
883
|
+
/** Per-request timeout in milliseconds. */
|
|
884
|
+
requestTimeoutMs?: number
|
|
885
|
+
/** Number of retry attempts on failure. Default: 0 (no retries). */
|
|
886
|
+
retryAttempts?: number
|
|
887
|
+
/** Delay between retry attempts in milliseconds. */
|
|
888
|
+
retryDelayMs?: number
|
|
889
|
+
/** Backoff strategy for retries: "immediate", "fixed", or "exponential". Default: "immediate". */
|
|
890
|
+
retryBackoffStrategy?: string
|
|
460
891
|
}
|
|
461
892
|
|
|
462
893
|
/** Options for writing file records. */
|
|
463
894
|
export interface WriteFileRecordOptions {
|
|
464
|
-
/**
|
|
895
|
+
/** An array of file record write sub-requests. */
|
|
465
896
|
requests: Array<FileRecordWriteRequest>
|
|
466
|
-
/**
|
|
467
|
-
signal?:
|
|
897
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
898
|
+
signal?: AbortSignal
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** Handler request for writing file records. */
|
|
902
|
+
export interface WriteFileRecordRequest {
|
|
903
|
+
/** The unit ID of the device that sent the request. */
|
|
904
|
+
unitId: number
|
|
905
|
+
/** An array of file record write sub-requests. */
|
|
906
|
+
requests: Array<FileRecordWriteSubRequest>
|
|
468
907
|
}
|
|
469
908
|
|
|
470
909
|
/** Options for writing multiple coils. */
|
|
471
910
|
export interface WriteMultipleCoilsOptions {
|
|
472
911
|
/** Starting coil address. */
|
|
473
912
|
address: number
|
|
474
|
-
/**
|
|
913
|
+
/** An array of boolean values to write. */
|
|
475
914
|
values: Array<boolean>
|
|
476
|
-
/**
|
|
477
|
-
signal?:
|
|
915
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
916
|
+
signal?: AbortSignal
|
|
478
917
|
}
|
|
479
918
|
|
|
480
919
|
/** Handler request for writing multiple coils. */
|
|
481
920
|
export interface WriteMultipleCoilsRequest {
|
|
921
|
+
/** The unit ID of the device that sent the request. */
|
|
482
922
|
unitId: number
|
|
923
|
+
/** The starting address of the coils to write to. */
|
|
483
924
|
address: number
|
|
925
|
+
/** The array of boolean values to write. */
|
|
484
926
|
values: Array<boolean>
|
|
485
927
|
}
|
|
486
928
|
|
|
@@ -488,16 +930,19 @@ export interface WriteMultipleCoilsRequest {
|
|
|
488
930
|
export interface WriteMultipleRegistersOptions {
|
|
489
931
|
/** Starting register address. */
|
|
490
932
|
address: number
|
|
491
|
-
/**
|
|
933
|
+
/** An array of 16-bit values to write. */
|
|
492
934
|
values: Array<number>
|
|
493
|
-
/**
|
|
494
|
-
signal?:
|
|
935
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
936
|
+
signal?: AbortSignal
|
|
495
937
|
}
|
|
496
938
|
|
|
497
939
|
/** Handler request for writing multiple registers. */
|
|
498
940
|
export interface WriteMultipleRegistersRequest {
|
|
941
|
+
/** The unit ID of the device that sent the request. */
|
|
499
942
|
unitId: number
|
|
943
|
+
/** The starting address of the registers to write to. */
|
|
500
944
|
address: number
|
|
945
|
+
/** The array of 16-bit values to write. */
|
|
501
946
|
values: Array<number>
|
|
502
947
|
}
|
|
503
948
|
|
|
@@ -505,16 +950,19 @@ export interface WriteMultipleRegistersRequest {
|
|
|
505
950
|
export interface WriteSingleCoilOptions {
|
|
506
951
|
/** Coil address. */
|
|
507
952
|
address: number
|
|
508
|
-
/**
|
|
953
|
+
/** The boolean value to write (true for ON, false for OFF). */
|
|
509
954
|
value: boolean
|
|
510
|
-
/**
|
|
511
|
-
signal?:
|
|
955
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
956
|
+
signal?: AbortSignal
|
|
512
957
|
}
|
|
513
958
|
|
|
514
959
|
/** Handler request for writing a single coil. */
|
|
515
960
|
export interface WriteSingleCoilRequest {
|
|
961
|
+
/** The unit ID of the device that sent the request. */
|
|
516
962
|
unitId: number
|
|
963
|
+
/** The address of the coil to write to. */
|
|
517
964
|
address: number
|
|
965
|
+
/** The value to write (true for ON, false for OFF). */
|
|
518
966
|
value: boolean
|
|
519
967
|
}
|
|
520
968
|
|
|
@@ -522,15 +970,181 @@ export interface WriteSingleCoilRequest {
|
|
|
522
970
|
export interface WriteSingleRegisterOptions {
|
|
523
971
|
/** Register address. */
|
|
524
972
|
address: number
|
|
525
|
-
/**
|
|
973
|
+
/** The 16-bit value to write to the register. */
|
|
526
974
|
value: number
|
|
527
|
-
/**
|
|
528
|
-
signal?:
|
|
975
|
+
/** An optional `AbortSignal` to cancel the asynchronous operation. */
|
|
976
|
+
signal?: AbortSignal
|
|
529
977
|
}
|
|
530
978
|
|
|
531
979
|
/** Handler request for writing a single register. */
|
|
532
980
|
export interface WriteSingleRegisterRequest {
|
|
981
|
+
/** The unit ID of the device that sent the request. */
|
|
533
982
|
unitId: number
|
|
983
|
+
/** The address of the register to write to. */
|
|
534
984
|
address: number
|
|
985
|
+
/** The 16-bit value to write. */
|
|
535
986
|
value: number
|
|
536
987
|
}
|
|
988
|
+
|
|
989
|
+
export interface ModbusException {
|
|
990
|
+
exception: number;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/**
|
|
994
|
+
* Callback functions to handle Modbus server requests.
|
|
995
|
+
*
|
|
996
|
+
* Each handler corresponds to a specific Modbus function code. If a handler is not provided,
|
|
997
|
+
* the server will respond with an "Illegal Function" exception (0x01).
|
|
998
|
+
*
|
|
999
|
+
* Handlers can return the expected data directly or return a Promise for async operations.
|
|
1000
|
+
* To return a Modbus exception, return an object matching the `ModbusException` interface.
|
|
1001
|
+
*/
|
|
1002
|
+
export interface ServerHandlers {
|
|
1003
|
+
/**
|
|
1004
|
+
* Handle Read Coils (FC 01).
|
|
1005
|
+
* @param req The request object.
|
|
1006
|
+
* @param req.address The starting coil address (0x0000 to 0xFFFF).
|
|
1007
|
+
* @param req.quantity The number of coils to read (1 to 2000).
|
|
1008
|
+
* @returns An array of booleans representing the coil states, or a ModbusException.
|
|
1009
|
+
*/
|
|
1010
|
+
onReadCoils?: (req: ReadCoilsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* Handle Read Discrete Inputs (FC 02).
|
|
1014
|
+
* @param req The request object.
|
|
1015
|
+
* @param req.address The starting discrete input address (0x0000 to 0xFFFF).
|
|
1016
|
+
* @param req.quantity The number of inputs to read (1 to 2000).
|
|
1017
|
+
* @returns An array of booleans representing the input states, or a ModbusException.
|
|
1018
|
+
*/
|
|
1019
|
+
onReadDiscreteInputs?: (req: ReadDiscreteInputsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Handle Read Holding Registers (FC 03).
|
|
1023
|
+
* @param req The request object.
|
|
1024
|
+
* @param req.address The starting holding register address (0x0000 to 0xFFFF).
|
|
1025
|
+
* @param req.quantity The number of registers to read (1 to 125).
|
|
1026
|
+
* @returns An array of 16-bit numbers representing the registers, or a ModbusException.
|
|
1027
|
+
*/
|
|
1028
|
+
onReadHoldingRegisters?: (req: ReadHoldingRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
|
|
1029
|
+
|
|
1030
|
+
/**
|
|
1031
|
+
* Handle Read Input Registers (FC 04).
|
|
1032
|
+
* @param req The request object.
|
|
1033
|
+
* @param req.address The starting input register address (0x0000 to 0xFFFF).
|
|
1034
|
+
* @param req.quantity The number of registers to read (1 to 125).
|
|
1035
|
+
* @returns An array of 16-bit numbers representing the registers, or a ModbusException.
|
|
1036
|
+
*/
|
|
1037
|
+
onReadInputRegisters?: (req: ReadInputRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
* Handle Write Single Coil (FC 05).
|
|
1041
|
+
* @param req The request object.
|
|
1042
|
+
* @param req.address The address of the coil to write (0x0000 to 0xFFFF).
|
|
1043
|
+
* @param req.value The boolean value to write (true for ON, false for OFF).
|
|
1044
|
+
* @returns void on success, or a ModbusException.
|
|
1045
|
+
*/
|
|
1046
|
+
onWriteSingleCoil?: (req: WriteSingleCoilRequest) => void | ModbusException | Promise<void | ModbusException>;
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Handle Write Single Register (FC 06).
|
|
1050
|
+
* @param req The request object.
|
|
1051
|
+
* @param req.address The address of the register to write (0x0000 to 0xFFFF).
|
|
1052
|
+
* @param req.value The 16-bit value to write.
|
|
1053
|
+
* @returns void on success, or a ModbusException.
|
|
1054
|
+
*/
|
|
1055
|
+
onWriteSingleRegister?: (req: WriteSingleRegisterRequest) => void | ModbusException | Promise<void | ModbusException>;
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Handle Read Exception Status (FC 07).
|
|
1059
|
+
* @param req The read exception status request object (empty).
|
|
1060
|
+
* @returns An 8-bit exception status byte, or a ModbusException.
|
|
1061
|
+
*/
|
|
1062
|
+
onReadExceptionStatus?: (req: ReadExceptionStatusRequest) => number | ModbusException | Promise<number | ModbusException>;
|
|
1063
|
+
|
|
1064
|
+
/**
|
|
1065
|
+
* Handle Diagnostics (FC 08).
|
|
1066
|
+
* @param req The diagnostics request object.
|
|
1067
|
+
* @param req.subFunction The 16-bit sub-function code.
|
|
1068
|
+
* @param req.data The 16-bit data payload for the sub-function.
|
|
1069
|
+
* @returns A response containing the sub-function and data, or a ModbusException.
|
|
1070
|
+
*/
|
|
1071
|
+
onDiagnostics?: (req: DiagnosticsRequest) => ServerDiagnosticsResponse | ModbusException | Promise<ServerDiagnosticsResponse | ModbusException>;
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* Handle Write Multiple Coils (FC 15).
|
|
1075
|
+
* @param req The request object.
|
|
1076
|
+
* @param req.address The starting address of the coils to write.
|
|
1077
|
+
* @param req.values An array of booleans to write.
|
|
1078
|
+
* @returns void on success, or a ModbusException.
|
|
1079
|
+
*/
|
|
1080
|
+
onWriteMultipleCoils?: (req: WriteMultipleCoilsRequest) => void | ModbusException | Promise<void | ModbusException>;
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* Handle Write Multiple Registers (FC 16).
|
|
1084
|
+
* @param req The request object.
|
|
1085
|
+
* @param req.address The starting address of the registers to write.
|
|
1086
|
+
* @param req.values An array of 16-bit numbers to write.
|
|
1087
|
+
* @returns void on success, or a ModbusException.
|
|
1088
|
+
*/
|
|
1089
|
+
onWriteMultipleRegisters?: (req: WriteMultipleRegistersRequest) => void | ModbusException | Promise<void | ModbusException>;
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Handle Read File Record (FC 20).
|
|
1093
|
+
* @param req The request object.
|
|
1094
|
+
* @param req.subRequests An array of sub-requests, each with `fileNumber`, `recordNumber`, and `recordLength`.
|
|
1095
|
+
* @returns An array of register arrays for each sub-request, or a ModbusException.
|
|
1096
|
+
*/
|
|
1097
|
+
onReadFileRecord?: (req: ReadFileRecordRequest) => number[][] | ModbusException | Promise<number[][] | ModbusException>;
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Handle Write File Record (FC 21).
|
|
1101
|
+
* @param req The request object.
|
|
1102
|
+
* @param req.subRequests An array of sub-requests, each with `fileNumber`, `recordNumber`, and `recordData` (a number array).
|
|
1103
|
+
* @returns void on success, or a ModbusException.
|
|
1104
|
+
*/
|
|
1105
|
+
onWriteFileRecord?: (req: WriteFileRecordRequest) => void | ModbusException | Promise<void | ModbusException>;
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* Handle Read/Write Multiple Registers (FC 23).
|
|
1109
|
+
* @param req The request containing addresses and values to read and write.
|
|
1110
|
+
* @param req.readAddress The starting address for the read operation.
|
|
1111
|
+
* @param req.readQuantity The number of registers to read.
|
|
1112
|
+
* @param req.writeAddress The starting address for the write operation.
|
|
1113
|
+
* @param req.values An array of 16-bit numbers to write.
|
|
1114
|
+
* @returns An array of 16-bit numbers read, or a ModbusException.
|
|
1115
|
+
*/
|
|
1116
|
+
onReadWriteMultipleRegisters?: (req: ReadWriteMultipleRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
|
|
1117
|
+
|
|
1118
|
+
/**
|
|
1119
|
+
* Handle Read FIFO Queue (FC 24).
|
|
1120
|
+
* @param req The request object containing the FIFO pointer `address`.
|
|
1121
|
+
* @returns An array of 16-bit numbers from the queue, or a ModbusException.
|
|
1122
|
+
*/
|
|
1123
|
+
onReadFifoQueue?: (req: ReadFifoQueueRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/**
|
|
1127
|
+
* Stable error codes for identifying Modbus-related errors.
|
|
1128
|
+
* These can be used with the `getModbusErrorCode` helper to check for specific error types.
|
|
1129
|
+
*/
|
|
1130
|
+
export declare const ModbusErrorCode: {
|
|
1131
|
+
/** A Modbus exception response was received from the server (e.g., illegal function). */
|
|
1132
|
+
readonly EXCEPTION: 'MODBUS_EXCEPTION';
|
|
1133
|
+
/** The request timed out waiting for a response. */
|
|
1134
|
+
readonly TIMEOUT: 'MODBUS_TIMEOUT';
|
|
1135
|
+
/** A transport-level error occurred (e.g., framing error, checksum mismatch). */
|
|
1136
|
+
readonly TRANSPORT: 'MODBUS_TRANSPORT';
|
|
1137
|
+
/** An invalid argument was provided to a client or server function. */
|
|
1138
|
+
readonly INVALID_ARGUMENT: 'MODBUS_INVALID_ARGUMENT';
|
|
1139
|
+
/** The underlying connection was closed. */
|
|
1140
|
+
readonly CONNECTION_CLOSED: 'MODBUS_CONNECTION_CLOSED';
|
|
1141
|
+
/** An unexpected internal error occurred within the library. */
|
|
1142
|
+
readonly INTERNAL: 'MODBUS_INTERNAL';
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Extracts a stable error code from a Modbus error object.
|
|
1147
|
+
* @param err The error object.
|
|
1148
|
+
* @returns The corresponding code from `ModbusErrorCode`, or undefined if not a Modbus error.
|
|
1149
|
+
*/
|
|
1150
|
+
export declare function getModbusErrorCode(err: Error): string | undefined;
|