modbus-rs-wasm 0.14.2 → 0.15.0

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.
@@ -1,710 +1,300 @@
1
- /* tslint:disable */
2
- /* eslint-disable */
3
-
4
- export interface WasmSerialTransportOptions {
5
- mode?: "rtu" | "ascii";
6
- baudRate: number;
7
- dataBits?: 5 | 6 | 7 | 8;
8
- stopBits?: 1 | 2;
9
- parity?: "none" | "even" | "odd";
10
- responseTimeoutMs?: number;
11
- retryAttempts?: number;
12
- tickIntervalMs?: number;
1
+ export interface WasmTcpTransportOptions {
2
+ responseTimeoutMs?: number;
3
+ retryAttempts?: number;
13
4
  }
14
5
 
6
+ export declare class WasmTcpTransport {
7
+ static connect(wsUrl: string, opts?: WasmTcpTransportOptions): Promise<WasmTcpTransport>;
8
+ createClient(opts: CreateClientOptions): WasmModbusClient;
9
+ get pendingRequests(): boolean;
10
+ reconnect(): Promise<void>;
11
+ close(): void;
12
+ }
15
13
 
14
+ export declare class WasmModbusClient {
15
+ readHoldingRegisters(opts: ReadRegistersOptions): Promise<Uint16Array>;
16
+ readInputRegisters(opts: ReadRegistersOptions): Promise<Uint16Array>;
17
+ writeSingleRegister(opts: WriteSingleRegisterOptions): Promise<void>;
18
+ writeMultipleRegisters(opts: WriteMultipleRegistersOptions): Promise<void>;
19
+ readWriteMultipleRegisters(opts: ReadWriteMultipleRegistersOptions): Promise<Uint16Array>;
20
+ readCoils(opts: ReadBitsOptions): Promise<boolean[]>;
21
+ writeSingleCoil(opts: WriteSingleCoilOptions): Promise<void>;
22
+ writeMultipleCoils(opts: WriteMultipleCoilsOptions): Promise<void>;
23
+ readDiscreteInputs(opts: ReadBitsOptions): Promise<boolean[]>;
24
+ readFifoQueue(opts: ReadFifoQueueOptions): Promise<Uint16Array>;
25
+ readFileRecord(opts: ReadFileRecordOptions): Promise<Uint16Array[]>;
26
+ writeFileRecord(opts: WriteFileRecordOptions): Promise<void>;
27
+ readExceptionStatus(): Promise<number>;
28
+ diagnostics(opts: DiagnosticsOptions): Promise<DiagnosticsResponse>;
29
+ readDeviceIdentification(opts: ReadDeviceIdentificationOptions): Promise<DeviceIdentificationResponse>;
30
+ maskWriteRegister(opts: MaskWriteRegisterOptions): Promise<void>;
31
+ }
16
32
 
17
- export interface WasmTcpTransportOptions {
18
- responseTimeoutMs?: number;
19
- retryAttempts?: number;
20
- tickIntervalMs?: number;
33
+ export interface MaskWriteRegisterOptions {
34
+ address: number;
35
+ andMask: number;
36
+ orMask: number;
37
+ signal?: AbortSignal;
21
38
  }
22
- export interface WasmCreateClientOptions {
23
- unitId?: number;
39
+
40
+ // Serial transport
41
+ export interface WasmSerialTransportOptions {
42
+ mode?: "rtu" | "ascii";
43
+ baudRate: number;
44
+ dataBits?: 5 | 6 | 7 | 8;
45
+ stopBits?: 1 | 2;
46
+ parity?: "none" | "even" | "odd";
47
+ responseTimeoutMs?: number;
48
+ retryAttempts?: number;
24
49
  }
25
50
 
51
+ export declare class WasmSerialPortHandle {
52
+ is_valid(): boolean;
53
+ }
26
54
 
55
+ export declare function request_serial_port(): Promise<WasmSerialPortHandle>;
27
56
 
28
- /**
29
- * Browser-facing Modbus client bound to a specific unit ID.
30
- */
31
- export class WasmModbusClient {
32
- private constructor();
33
- free(): void;
34
- [Symbol.dispose](): void;
35
- /**
36
- * Send a Diagnostics request (FC 8).
37
- *
38
- * `sub_function` is one of the `DiagnosticSubFunction` u16 codes.
39
- * Returns a `Promise` resolving with `{ subFunction, data: Uint16Array }` or rejects on error.
40
- */
41
- diagnostics(sub_function: number, data: Uint16Array): Promise<any>;
42
- /**
43
- * Read the communication event counter (FC 11).
44
- *
45
- * Returns a `Promise` resolving with `{ status, eventCount }` or rejects on error.
46
- */
47
- get_comm_event_counter(): Promise<any>;
48
- /**
49
- * Read the communication event log (FC 12).
50
- *
51
- * Returns a `Promise` resolving with `{ status, eventCount, messageCount, events: Uint8Array }`
52
- * or rejects on error.
53
- */
54
- get_comm_event_log(): Promise<any>;
55
- /**
56
- * Returns `true` if there are in-flight Modbus requests.
57
- */
58
- has_pending_requests(): boolean;
59
- /**
60
- * Returns `true` when the underlying WebSocket is open.
61
- */
62
- is_connected(): boolean;
63
- /**
64
- * Apply an AND/OR mask to a holding register at `address` (FC 22).
65
- *
66
- * Result register = (current & and_mask) | (or_mask & !and_mask).
67
- * Returns a `Promise` resolving with `true` or rejects on error.
68
- */
69
- mask_write_register(address: number, and_mask: number, or_mask: number): Promise<any>;
70
- /**
71
- * Read `quantity` coils starting at `address`.
72
- *
73
- * Returns a `Promise` that resolves with a `Uint8Array` (bit-packed coil bytes)
74
- * or rejects with an error string on failure.
75
- */
76
- read_coils(address: number, quantity: number): Promise<any>;
77
- /**
78
- * Read Device Identification (FC 43 / MEI 0x0E).
79
- *
80
- * `read_device_id_code`: 1=Basic, 2=Regular, 3=Extended, 4=Specific.
81
- * `object_id`: 0x00=VendorName, 0x01=ProductCode, 0x02=Revision, 0x03=VendorURL, etc.
82
- * Returns a `Promise` resolving with `{ readDeviceIdCode, conformityLevel, moreFollows, objects }`
83
- * or rejects on error.
84
- */
85
- read_device_identification(read_device_id_code: number, object_id: number): Promise<any>;
86
- /**
87
- * Read `quantity` discrete inputs starting at `address`.
88
- *
89
- * Returns a `Promise` that resolves with a `Uint8Array` (bit-packed)
90
- * or rejects on error.
91
- */
92
- read_discrete_inputs(address: number, quantity: number): Promise<any>;
93
- /**
94
- * Read the exception status (FC 7) — serial-line only on most devices.
95
- *
96
- * Returns a `Promise` resolving with a status `number` or rejects on error.
97
- */
98
- read_exception_status(): Promise<any>;
99
- /**
100
- * Read the FIFO queue pointed to by `address` (FC 24).
101
- *
102
- * Returns a `Promise` resolving with a `Uint16Array` or rejects on error.
103
- */
104
- read_fifo_queue(address: number): Promise<any>;
105
- /**
106
- * Read a file record (FC 20).
107
- *
108
- * Returns a `Promise` resolving with `Array<{ fileNumber, recordNumber, data: Uint16Array }>`
109
- * or rejects on error.
110
- */
111
- read_file_record(file_number: number, record_number: number, record_length: number): Promise<any>;
112
- /**
113
- * Read `quantity` holding registers starting at `address`.
114
- *
115
- * Returns a `Promise` that resolves with a `Uint16Array` (register values)
116
- * or rejects with an error string on failure.
117
- */
118
- read_holding_registers(address: number, quantity: number): Promise<any>;
119
- /**
120
- * Read `quantity` input registers starting at `address`.
121
- *
122
- * Returns a `Promise` that resolves with a `Uint16Array` or rejects on error.
123
- */
124
- read_input_registers(address: number, quantity: number): Promise<any>;
125
- /**
126
- * Read a single coil at `address`.
127
- *
128
- * Returns a `Promise` that resolves with a `boolean` or rejects on error.
129
- */
130
- read_single_coil(address: number): Promise<any>;
131
- /**
132
- * Read a single discrete input at `address`.
133
- *
134
- * Returns a `Promise` resolving with a `boolean` or rejects on error.
135
- */
136
- read_single_discrete_input(address: number): Promise<any>;
137
- /**
138
- * Read a single holding register at `address`.
139
- *
140
- * Returns a `Promise` that resolves with a `number` or rejects on error.
141
- */
142
- read_single_holding_register(address: number): Promise<any>;
143
- /**
144
- * Read a single input register at `address`.
145
- *
146
- * Returns a `Promise` that resolves with a `number` or rejects on error.
147
- */
148
- read_single_input_register(address: number): Promise<any>;
149
- /**
150
- * Perform an atomic read-then-write on holding registers.
151
- *
152
- * Reads `read_quantity` registers from `read_address`, then writes `values` to `write_address`.
153
- * `write_quantity` is ignored — the quantity written is derived from `values.length`.
154
- * Returns a `Promise` resolving with a `Uint16Array` (the values read) or rejects on error.
155
- */
156
- read_write_multiple_registers(read_address: number, read_quantity: number, write_address: number, _write_quantity: number, values: Uint16Array): Promise<any>;
157
- /**
158
- * Report Server ID (FC 17).
159
- *
160
- * Returns a `Promise` resolving with a `Uint8Array` (raw server ID data) or rejects on error.
161
- */
162
- report_server_id(): Promise<any>;
163
- /**
164
- * Write a file record (FC 21).
165
- *
166
- * `values` is a `Uint16Array` of register values to write.
167
- * Returns a `Promise` resolving with `true` or rejects on error.
168
- */
169
- write_file_record(file_number: number, record_number: number, values: Uint16Array): Promise<any>;
170
- /**
171
- * Write multiple coils starting at `address`.
172
- *
173
- * `packed_bytes` is a bit-packed `Uint8Array` (LSB of byte 0 = coil at `address`).
174
- * Returns a `Promise` that resolves with `{ address, quantity }` or rejects on error.
175
- */
176
- write_multiple_coils(address: number, quantity: number, packed_bytes: Uint8Array): Promise<any>;
177
- /**
178
- * Write `values` to multiple consecutive holding registers starting at `address`.
179
- *
180
- * Returns a `Promise` that resolves with `{ address, quantity }` or rejects on error.
181
- */
182
- write_multiple_registers(address: number, quantity: number, values: Uint16Array): Promise<any>;
183
- /**
184
- * Write a single coil at `address` to `value` (true = ON, false = OFF).
185
- *
186
- * Returns a `Promise` that resolves with `{ address, value }` or rejects on error.
187
- */
188
- write_single_coil(address: number, value: boolean): Promise<any>;
189
- /**
190
- * Write `value` to a single holding register at `address`.
191
- *
192
- * Returns a `Promise` that resolves with `{ address, value }` or rejects on error.
193
- */
194
- write_single_register(address: number, value: number): Promise<any>;
57
+ export declare class WasmSerialTransport {
58
+ constructor(portHandle: WasmSerialPortHandle, opts?: WasmSerialTransportOptions);
59
+ createClient(opts: CreateClientOptions): WasmSerialModbusClient;
60
+ get pendingRequests(): boolean;
61
+ close(): void;
195
62
  }
196
63
 
197
- /**
198
- * Browser-facing Modbus client bound to a specific serial unit ID.
199
- */
200
- export class WasmSerialModbusClient {
201
- private constructor();
202
- free(): void;
203
- [Symbol.dispose](): void;
204
- /**
205
- * Send a Diagnostics request (FC 8).
206
- *
207
- * `sub_function` is one of the `DiagnosticSubFunction` u16 codes.
208
- * Returns a `Promise` resolving with `{ subFunction, data: Uint16Array }` or rejects on error.
209
- */
210
- diagnostics(sub_function: number, data: Uint16Array): Promise<any>;
211
- /**
212
- * Read the communication event counter (FC 11).
213
- *
214
- * Returns a `Promise` resolving with `{ status, eventCount }` or rejects on error.
215
- */
216
- get_comm_event_counter(): Promise<any>;
217
- /**
218
- * Read the communication event log (FC 12).
219
- *
220
- * Returns a `Promise` resolving with `{ status, eventCount, messageCount, events: Uint8Array }`
221
- * or rejects on error.
222
- */
223
- get_comm_event_log(): Promise<any>;
224
- /**
225
- * Returns `true` if there are in-flight Modbus requests.
226
- */
227
- has_pending_requests(): boolean;
228
- /**
229
- * Returns `true` when the serial port is open.
230
- */
231
- is_connected(): boolean;
232
- /**
233
- * Apply an AND/OR mask to a holding register at `address` (FC 22).
234
- *
235
- * Result register = (current & and_mask) | (or_mask & !and_mask).
236
- * Returns a `Promise` resolving with `true` or rejects on error.
237
- */
238
- mask_write_register(address: number, and_mask: number, or_mask: number): Promise<any>;
239
- /**
240
- * Read `quantity` coils starting at `address`.
241
- *
242
- * Returns a `Promise` resolving with a `Uint8Array` (bit-packed coil bytes) or rejects on error.
243
- */
244
- read_coils(address: number, quantity: number): Promise<any>;
245
- /**
246
- * Read Device Identification (FC 43 / MEI 0x0E).
247
- *
248
- * `read_device_id_code`: 1=Basic, 2=Regular, 3=Extended, 4=Specific.
249
- * `object_id`: 0x00=VendorName, 0x01=ProductCode, 0x02=Revision, etc.
250
- * Returns a `Promise` resolving with `{ readDeviceIdCode, conformityLevel, moreFollows, objects }`
251
- * or rejects on error.
252
- */
253
- read_device_identification(read_device_id_code: number, object_id: number): Promise<any>;
254
- /**
255
- * Read `quantity` discrete inputs starting at `address`.
256
- *
257
- * Returns a `Promise` resolving with a `Uint8Array` (bit-packed) or rejects on error.
258
- */
259
- read_discrete_inputs(address: number, quantity: number): Promise<any>;
260
- /**
261
- * Read the exception status (FC 7).
262
- *
263
- * Returns a `Promise` resolving with a status `number` or rejects on error.
264
- */
265
- read_exception_status(): Promise<any>;
266
- /**
267
- * Read the FIFO queue pointed to by `address` (FC 24).
268
- *
269
- * Returns a `Promise` resolving with a `Uint16Array` or rejects on error.
270
- */
271
- read_fifo_queue(address: number): Promise<any>;
272
- /**
273
- * Read a file record (FC 20).
274
- *
275
- * Returns a `Promise` resolving with `Array<{ fileNumber, recordNumber, data: Uint16Array }>`
276
- * or rejects on error.
277
- */
278
- read_file_record(file_number: number, record_number: number, record_length: number): Promise<any>;
279
- /**
280
- * Read `quantity` holding registers starting at `address`.
281
- *
282
- * Returns a `Promise` resolving with a `Uint16Array` (register values) or rejects on error.
283
- */
284
- read_holding_registers(address: number, quantity: number): Promise<any>;
285
- /**
286
- * Read `quantity` input registers starting at `address`.
287
- *
288
- * Returns a `Promise` resolving with a `Uint16Array` or rejects on error.
289
- */
290
- read_input_registers(address: number, quantity: number): Promise<any>;
291
- /**
292
- * Read a single coil at `address`.
293
- *
294
- * Returns a `Promise` resolving with a `boolean` or rejects on error.
295
- */
296
- read_single_coil(address: number): Promise<any>;
297
- /**
298
- * Read a single discrete input at `address`.
299
- *
300
- * Returns a `Promise` resolving with a `boolean` or rejects on error.
301
- */
302
- read_single_discrete_input(address: number): Promise<any>;
303
- /**
304
- * Read a single holding register at `address`.
305
- *
306
- * Returns a `Promise` resolving with a `number` or rejects on error.
307
- */
308
- read_single_holding_register(address: number): Promise<any>;
309
- /**
310
- * Read a single input register at `address`.
311
- *
312
- * Returns a `Promise` resolving with a `number` or rejects on error.
313
- */
314
- read_single_input_register(address: number): Promise<any>;
315
- /**
316
- * Perform an atomic read-then-write on holding registers.
317
- *
318
- * Reads `read_quantity` registers from `read_address`, then writes `values` to `write_address`.
319
- * `write_quantity` is ignored — the quantity written is derived from `values.length`.
320
- * Returns a `Promise` resolving with a `Uint16Array` (the values read) or rejects on error.
321
- */
322
- read_write_multiple_registers(read_address: number, read_quantity: number, write_address: number, _write_quantity: number, values: Uint16Array): Promise<any>;
323
- /**
324
- * Report Server ID (FC 17).
325
- *
326
- * Returns a `Promise` resolving with a `Uint8Array` (raw server ID data) or rejects on error.
327
- */
328
- report_server_id(): Promise<any>;
329
- /**
330
- * Write a file record (FC 21).
331
- *
332
- * `values` is a `Uint16Array` of register values to write.
333
- * Returns a `Promise` resolving with `true` or rejects on error.
334
- */
335
- write_file_record(file_number: number, record_number: number, values: Uint16Array): Promise<any>;
336
- /**
337
- * Write multiple coils starting at `address`.
338
- *
339
- * `packed_bytes` is a bit-packed `Uint8Array` (LSB of byte 0 = coil at `address`).
340
- * Returns a `Promise` resolving with `{ address, quantity }` or rejects on error.
341
- */
342
- write_multiple_coils(address: number, quantity: number, packed_bytes: Uint8Array): Promise<any>;
343
- /**
344
- * Write `values` to multiple consecutive holding registers starting at `address`.
345
- *
346
- * Returns a `Promise` resolving with `{ address, quantity }` or rejects on error.
347
- */
348
- write_multiple_registers(address: number, quantity: number, values: Uint16Array): Promise<any>;
349
- /**
350
- * Write a single coil at `address` to `value` (true = ON, false = OFF).
351
- *
352
- * Returns a `Promise` resolving with `{ address, value }` or rejects on error.
353
- */
354
- write_single_coil(address: number, value: boolean): Promise<any>;
355
- /**
356
- * Write `value` to a single holding register at `address`.
357
- *
358
- * Returns a `Promise` resolving with `{ address, value }` or rejects on error.
359
- */
360
- write_single_register(address: number, value: number): Promise<any>;
64
+ export declare class WasmSerialModbusClient {
65
+ readHoldingRegisters(opts: ReadRegistersOptions): Promise<Uint16Array>;
66
+ readInputRegisters(opts: ReadRegistersOptions): Promise<Uint16Array>;
67
+ writeSingleRegister(opts: WriteSingleRegisterOptions): Promise<void>;
68
+ writeMultipleRegisters(opts: WriteMultipleRegistersOptions): Promise<void>;
69
+ readWriteMultipleRegisters(opts: ReadWriteMultipleRegistersOptions): Promise<Uint16Array>;
70
+ readCoils(opts: ReadBitsOptions): Promise<boolean[]>;
71
+ writeSingleCoil(opts: WriteSingleCoilOptions): Promise<void>;
72
+ writeMultipleCoils(opts: WriteMultipleCoilsOptions): Promise<void>;
73
+ readDiscreteInputs(opts: ReadBitsOptions): Promise<boolean[]>;
74
+ readFifoQueue(opts: ReadFifoQueueOptions): Promise<Uint16Array>;
75
+ readFileRecord(opts: ReadFileRecordOptions): Promise<Uint16Array[]>;
76
+ writeFileRecord(opts: WriteFileRecordOptions): Promise<void>;
77
+ readExceptionStatus(): Promise<number>;
78
+ diagnostics(opts: DiagnosticsOptions): Promise<DiagnosticsResponse>;
79
+ readDeviceIdentification(opts: ReadDeviceIdentificationOptions): Promise<DeviceIdentificationResponse>;
80
+ maskWriteRegister(opts: MaskWriteRegisterOptions): Promise<void>;
361
81
  }
362
82
 
363
- /**
364
- * Opaque handle around a browser `SerialPort` object granted by Web Serial.
365
- */
366
- export class WasmSerialPortHandle {
367
- private constructor();
368
- free(): void;
369
- [Symbol.dispose](): void;
370
- /**
371
- * Returns true if the wrapped JS value still looks like a valid SerialPort object.
372
- */
373
- is_valid(): boolean;
83
+ // Shared interfaces
84
+ export interface CreateClientOptions {
85
+ unitId: number;
374
86
  }
375
-
376
- /**
377
- * Browser-facing Modbus server endpoint for Web Serial traffic.
378
- */
379
- export class WasmSerialServer {
380
- free(): void;
381
- [Symbol.dispose](): void;
382
- /**
383
- * Attach browser SerialPort object delegated to mbus-serial transport.
384
- */
385
- attach_serial_port(port: any): void;
386
- /**
387
- * Clears the stored last-error message.
388
- */
389
- clear_last_error(): void;
390
- /**
391
- * Dispatch a request object into JS app handler.
392
- */
393
- dispatch_request(request: any): Promise<any>;
394
- /**
395
- * Whether server lifecycle is currently active.
396
- */
397
- is_running(): boolean;
398
- /**
399
- * Returns the last captured binding-layer error message, if any.
400
- */
401
- last_error_message(): string | undefined;
402
- /**
403
- * Selected serial mode as numeric enum.
404
- */
405
- mode(): WasmServerTransportKind;
406
- /**
407
- * Create a serial server with a JS request handler callback.
408
- *
409
- * `on_request` receives one request object and may return a direct value or Promise.
410
- */
411
- constructor(config: WasmSerialServerConfig, on_request: Function);
412
- /**
413
- * Try receiving one frame from delegated serial transport.
414
- */
415
- recv_frame(): Uint8Array;
416
- /**
417
- * Send one encoded frame through delegated serial transport.
418
- */
419
- send_frame(frame: Uint8Array): void;
420
- /**
421
- * Start server lifecycle.
422
- */
423
- start(): void;
424
- /**
425
- * Returns a point-in-time status snapshot for diagnostics/observability.
426
- */
427
- status_snapshot(): WasmServerStatusSnapshot;
428
- /**
429
- * Stop server lifecycle.
430
- */
431
- stop(): void;
432
- /**
433
- * Whether delegated serial transport currently reports connected.
434
- */
435
- transport_connected(): boolean;
87
+ export interface ReadRegistersOptions {
88
+ address: number;
89
+ quantity: number;
90
+ signal?: AbortSignal;
436
91
  }
437
-
438
- /**
439
- * Configuration for Web Serial server bindings.
440
- */
441
- export class WasmSerialServerConfig {
442
- private constructor();
443
- free(): void;
444
- [Symbol.dispose](): void;
445
- /**
446
- * Create ASCII serial server config.
447
- */
448
- static ascii(): WasmSerialServerConfig;
449
- /**
450
- * Selected serial mode.
451
- */
452
- mode(): WasmServerTransportKind;
453
- /**
454
- * Create RTU serial server config.
455
- */
456
- static rtu(): WasmSerialServerConfig;
92
+ export interface WriteSingleRegisterOptions {
93
+ address: number;
94
+ value: number;
95
+ signal?: AbortSignal;
457
96
  }
458
-
459
- /**
460
- * Connection and polling manager for browser Modbus Serial clients.
461
- */
462
- export class WasmSerialTransport {
463
- free(): void;
464
- [Symbol.dispose](): void;
465
- /**
466
- * Closes the serial port connection and rejects all pending requests.
467
- */
468
- close(): void;
469
- /**
470
- * Creates a device client bound to the specified unit ID.
471
- */
472
- createClient(options?: WasmCreateClientOptions | null): WasmSerialModbusClient;
473
- /**
474
- * Returns `true` if there are in-flight Modbus requests.
475
- */
476
- has_pending_requests(): boolean;
477
- /**
478
- * Returns `true` when the serial port is open and connected.
479
- */
480
- is_connected(): boolean;
481
- /**
482
- * Creates a new Serial (RTU) transport using the provided `port_handle`.
483
- *
484
- * The transport claims the underlying serial stream and manages the
485
- * read/write loop, allowing multiple clients to multiplex requests over it.
486
- */
487
- constructor(port_handle: WasmSerialPortHandle, options?: WasmSerialTransportOptions | null);
488
- /**
489
- * Drop all pending in-flight requests and attempt to reopen the serial port.
490
- */
491
- reconnect(): boolean;
97
+ export interface WriteMultipleRegistersOptions {
98
+ address: number;
99
+ values: number[] | Uint16Array;
100
+ signal?: AbortSignal;
492
101
  }
493
-
494
- /**
495
- * High-level design descriptor for phased server binding rollout.
496
- */
497
- export class WasmServerBindingPlan {
498
- private constructor();
499
- free(): void;
500
- [Symbol.dispose](): void;
501
- /**
502
- * Whether JS app callbacks are required for request dispatch.
503
- */
504
- app_callbacks_required(): boolean;
505
- /**
506
- * Whether lifecycle includes managed start/stop semantics.
507
- */
508
- lifecycle_managed(): boolean;
509
- /**
510
- * Plan for Web Serial ASCII based server bindings.
511
- */
512
- static serial_ascii(): WasmServerBindingPlan;
513
- /**
514
- * Plan for Web Serial RTU based server bindings.
515
- */
516
- static serial_rtu(): WasmServerBindingPlan;
517
- /**
518
- * Whether diagnostics counters are part of the plan.
519
- */
520
- supports_diagnostics_stats(): boolean;
521
- /**
522
- * Whether traffic hook callbacks are part of the plan.
523
- */
524
- supports_traffic_hooks(): boolean;
525
- /**
526
- * Plan for WebSocket-gateway based server bindings.
527
- */
528
- static tcp_gateway(): WasmServerBindingPlan;
529
- /**
530
- * Selected transport family.
531
- */
532
- transport(): WasmServerTransportKind;
102
+ export interface ReadWriteMultipleRegistersOptions {
103
+ readAddress: number;
104
+ readQuantity: number;
105
+ writeAddress: number;
106
+ writeValues: number[] | Uint16Array;
107
+ signal?: AbortSignal;
108
+ }
109
+ export interface ReadBitsOptions {
110
+ address: number;
111
+ quantity: number;
112
+ signal?: AbortSignal;
113
+ }
114
+ export interface WriteSingleCoilOptions {
115
+ address: number;
116
+ value: boolean;
117
+ signal?: AbortSignal;
118
+ }
119
+ export interface WriteMultipleCoilsOptions {
120
+ address: number;
121
+ values: boolean[];
122
+ signal?: AbortSignal;
123
+ }
124
+ export interface ReadFifoQueueOptions {
125
+ address: number;
126
+ signal?: AbortSignal;
127
+ }
128
+ export interface FileRecordReadRequest {
129
+ fileNumber: number;
130
+ recordNumber: number;
131
+ recordLength: number;
132
+ }
133
+ export interface ReadFileRecordOptions {
134
+ requests: FileRecordReadRequest[];
135
+ signal?: AbortSignal;
136
+ }
137
+ export interface FileRecordWriteRequest {
138
+ fileNumber: number;
139
+ recordNumber: number;
140
+ recordData: number[] | Uint16Array;
141
+ }
142
+ export interface WriteFileRecordOptions {
143
+ requests: FileRecordWriteRequest[];
144
+ signal?: AbortSignal;
145
+ }
146
+ export interface DiagnosticsOptions {
147
+ subFunction: number;
148
+ data: number[] | Uint16Array;
149
+ signal?: AbortSignal;
150
+ }
151
+ export interface DiagnosticsResponse {
152
+ subFunction: number;
153
+ data: Uint16Array;
154
+ }
155
+ export interface ReadDeviceIdentificationOptions {
156
+ readDeviceIdCode: number;
157
+ objectId: number;
158
+ signal?: AbortSignal;
159
+ }
160
+ export interface DeviceIdentificationObject {
161
+ id: number;
162
+ value: string;
163
+ }
164
+ export interface DeviceIdentificationResponse {
165
+ conformityLevel: number;
166
+ moreFollows: boolean;
167
+ nextObjectId: number;
168
+ objects: DeviceIdentificationObject[];
533
169
  }
534
170
 
535
- /**
536
- * Lightweight runtime status snapshot for browser-side server observability.
537
- */
538
- export class WasmServerStatusSnapshot {
539
- private constructor();
540
- free(): void;
541
- [Symbol.dispose](): void;
542
- /**
543
- * Number of successful `dispatch_request(...)` calls completed.
544
- */
545
- dispatched_requests(): number;
546
- /**
547
- * Whether a last error message is currently stored.
548
- */
549
- last_error_present(): boolean;
550
- /**
551
- * Number of successful `recv_frame(...)` calls.
552
- */
553
- received_frames(): number;
554
- /**
555
- * Whether server lifecycle is running.
556
- */
557
- running(): boolean;
558
- /**
559
- * Number of successful `send_frame(...)` calls.
560
- */
561
- sent_frames(): number;
562
- /**
563
- * Transport family of this server snapshot.
564
- */
565
- transport(): WasmServerTransportKind;
566
- /**
567
- * Whether delegated transport reports connected.
568
- */
569
- transport_connected(): boolean;
171
+ // WASM Server interfaces and declarations
172
+ export interface ReadCoilsRequest {
173
+ unitId: number;
174
+ address: number;
175
+ quantity: number;
176
+ }
177
+ export interface ReadDiscreteInputsRequest {
178
+ unitId: number;
179
+ address: number;
180
+ quantity: number;
181
+ }
182
+ export interface ReadHoldingRegistersRequest {
183
+ unitId: number;
184
+ address: number;
185
+ quantity: number;
186
+ }
187
+ export interface ReadInputRegistersRequest {
188
+ unitId: number;
189
+ address: number;
190
+ quantity: number;
191
+ }
192
+ export interface WriteSingleCoilRequest {
193
+ unitId: number;
194
+ address: number;
195
+ value: boolean;
196
+ }
197
+ export interface WriteSingleRegisterRequest {
198
+ unitId: number;
199
+ address: number;
200
+ value: number;
201
+ }
202
+ export interface WriteMultipleCoilsRequest {
203
+ unitId: number;
204
+ address: number;
205
+ values: boolean[];
206
+ }
207
+ export interface WriteMultipleRegistersRequest {
208
+ unitId: number;
209
+ address: number;
210
+ values: number[];
211
+ }
212
+ export interface ReadWriteMultipleRegistersRequest {
213
+ unitId: number;
214
+ readAddress: number;
215
+ readQuantity: number;
216
+ writeAddress: number;
217
+ writeValues: number[];
218
+ }
219
+ export interface ReadFifoQueueRequest {
220
+ unitId: number;
221
+ address: number;
222
+ }
223
+ export interface FileRecordReadServerSubRequest {
224
+ fileNumber: number;
225
+ recordNumber: number;
226
+ recordLength: number;
227
+ }
228
+ export interface ReadFileRecordRequest {
229
+ unitId: number;
230
+ requests: FileRecordReadServerSubRequest[];
231
+ }
232
+ export interface FileRecordWriteSubRequest {
233
+ fileNumber: number;
234
+ recordNumber: number;
235
+ recordData: number[];
236
+ }
237
+ export interface WriteFileRecordRequest {
238
+ unitId: number;
239
+ requests: FileRecordWriteSubRequest[];
240
+ }
241
+ export interface ReadExceptionStatusRequest {
242
+ unitId: number;
243
+ }
244
+ export interface DiagnosticsRequest {
245
+ unitId: number;
246
+ subFunction: number;
247
+ data: number[];
248
+ }
249
+ export interface ServerDiagnosticsResponse {
250
+ subFunction: number;
251
+ data: number[];
252
+ }
253
+ export interface ModbusException {
254
+ exception: number;
570
255
  }
571
256
 
572
- /**
573
- * Transport families planned for browser-side server bindings.
574
- */
575
- export enum WasmServerTransportKind {
576
- /**
577
- * Server loop receives requests via a WebSocket gateway.
578
- */
579
- TcpGateway = 0,
580
- /**
581
- * Server loop receives RTU frames via Web Serial.
582
- */
583
- SerialRtu = 1,
584
- /**
585
- * Server loop receives ASCII frames via Web Serial.
586
- */
587
- SerialAscii = 2,
257
+ export interface ServerHandlers {
258
+ onReadCoils?: (req: ReadCoilsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
259
+ onReadDiscreteInputs?: (req: ReadDiscreteInputsRequest) => boolean[] | ModbusException | Promise<boolean[] | ModbusException>;
260
+ onReadHoldingRegisters?: (req: ReadHoldingRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
261
+ onReadInputRegisters?: (req: ReadInputRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
262
+ onWriteSingleCoil?: (req: WriteSingleCoilRequest) => void | ModbusException | Promise<void | ModbusException>;
263
+ onWriteSingleRegister?: (req: WriteSingleRegisterRequest) => void | ModbusException | Promise<void | ModbusException>;
264
+ onReadExceptionStatus?: (req: ReadExceptionStatusRequest) => number | ModbusException | Promise<number | ModbusException>;
265
+ onDiagnostics?: (req: DiagnosticsRequest) => ServerDiagnosticsResponse | ModbusException | Promise<ServerDiagnosticsResponse | ModbusException>;
266
+ onWriteMultipleCoils?: (req: WriteMultipleCoilsRequest) => void | ModbusException | Promise<void | ModbusException>;
267
+ onWriteMultipleRegisters?: (req: WriteMultipleRegistersRequest) => void | ModbusException | Promise<void | ModbusException>;
268
+ onReadFileRecord?: (req: ReadFileRecordRequest) => number[][] | ModbusException | Promise<number[][] | ModbusException>;
269
+ onWriteFileRecord?: (req: WriteFileRecordRequest) => void | ModbusException | Promise<void | ModbusException>;
270
+ onReadWriteMultipleRegisters?: (req: ReadWriteMultipleRegistersRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
271
+ onReadFifoQueue?: (req: ReadFifoQueueRequest) => number[] | ModbusException | Promise<number[] | ModbusException>;
588
272
  }
589
273
 
590
- /**
591
- * Configuration for TCP gateway server bindings.
592
- */
593
- export class WasmTcpGatewayConfig {
594
- free(): void;
595
- [Symbol.dispose](): void;
596
- /**
597
- * Create a new TCP-gateway config from a websocket URL.
598
- */
599
- constructor(ws_url: string);
600
- /**
601
- * WebSocket endpoint URL.
602
- */
603
- ws_url(): string;
274
+ export interface WasmTcpServerOptions {
275
+ wsUrl: string;
276
+ unitId: number;
604
277
  }
605
278
 
606
- /**
607
- * Browser-facing Modbus server endpoint for WebSocket gateway traffic.
608
- */
609
- export class WasmTcpServer {
610
- free(): void;
611
- [Symbol.dispose](): void;
612
- /**
613
- * Clears the stored last-error message.
614
- */
615
- clear_last_error(): void;
616
- /**
617
- * Dispatch a request object into JS app handler.
618
- *
619
- * This is the phase-1 execution path used before transport loops are added.
620
- */
621
- dispatch_request(request: any): Promise<any>;
622
- /**
623
- * Whether server lifecycle is currently active.
624
- */
625
- is_running(): boolean;
626
- /**
627
- * Returns the last captured binding-layer error message, if any.
628
- */
629
- last_error_message(): string | undefined;
630
- /**
631
- * Create a server from gateway config and a JS request handler.
632
- *
633
- * `on_request` receives one request object and may return a direct value or Promise.
634
- */
635
- constructor(config: WasmTcpGatewayConfig, on_request: Function);
636
- /**
637
- * Try receiving one frame from delegated network transport.
638
- */
639
- recv_frame(): Uint8Array;
640
- /**
641
- * Send one encoded response/request frame through delegated network transport.
642
- */
643
- send_frame(frame: Uint8Array): void;
644
- /**
645
- * Start server lifecycle.
646
- */
647
- start(): void;
648
- /**
649
- * Returns a point-in-time status snapshot for diagnostics/observability.
650
- */
651
- status_snapshot(): WasmServerStatusSnapshot;
652
- /**
653
- * Stop server lifecycle.
654
- */
655
- stop(): void;
656
- /**
657
- * Whether the delegated websocket transport is fully open and ready.
658
- */
659
- transport_connected(): boolean;
660
- /**
661
- * Whether the delegated websocket transport handshake is still in progress.
662
- */
663
- transport_connecting(): boolean;
664
- /**
665
- * Gateway URL this server is configured to use.
666
- */
667
- ws_url(): string;
279
+ export interface WasmSerialServerOptions {
280
+ serialPort: WasmSerialPortHandle | any;
281
+ unitId: number;
282
+ baudRate?: number;
283
+ dataBits?: 5 | 6 | 7 | 8;
284
+ stopBits?: 1 | 2;
285
+ parity?: "none" | "even" | "odd";
286
+ responseTimeoutMs?: number;
668
287
  }
669
288
 
670
- /**
671
- * Connection and background polling manager for browser Modbus TCP clients.
672
- */
673
- export class WasmTcpTransport {
674
- free(): void;
675
- [Symbol.dispose](): void;
676
- /**
677
- * Closes the connection and rejects all pending requests.
678
- */
679
- close(): void;
680
- /**
681
- * Creates a device client bound to the specified unit ID.
682
- */
683
- createClient(options?: WasmCreateClientOptions | null): WasmModbusClient;
684
- /**
685
- * Returns `true` if there are in-flight Modbus requests.
686
- */
687
- has_pending_requests(): boolean;
688
- /**
689
- * Returns `true` when the underlying WebSocket is open and connected.
690
- */
691
- is_connected(): boolean;
692
- /**
693
- * Creates a new TCP/WebSocket transport.
694
- *
695
- * This establishes a connection to the provided `ws_url` and handles
696
- * the underlying read/write loops.
697
- */
698
- constructor(ws_url: string, options?: WasmTcpTransportOptions | null);
699
- /**
700
- * Drop all pending in-flight requests and attempt to reconnect.
701
- */
702
- reconnect(): boolean;
289
+ export declare class WasmTcpServer {
290
+ static bind(opts: WasmTcpServerOptions, handlers: ServerHandlers): Promise<WasmTcpServer>;
291
+ serve(): Promise<void>;
292
+ shutdown(): Promise<void>;
703
293
  }
704
294
 
705
- /**
706
- * Requests a browser serial port from `navigator.serial.requestPort()`.
707
- *
708
- * Must be invoked from a user-gesture context (e.g. click handler).
709
- */
710
- export function request_serial_port(): Promise<WasmSerialPortHandle>;
295
+ export declare class WasmSerialServer {
296
+ static bindRtu(opts: WasmSerialServerOptions, handlers: ServerHandlers): Promise<WasmSerialServer>;
297
+ static bindAscii(opts: WasmSerialServerOptions, handlers: ServerHandlers): Promise<WasmSerialServer>;
298
+ serve(): Promise<void>;
299
+ shutdown(): Promise<void>;
300
+ }