njs-modbus 2.1.0 → 3.0.2
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 +264 -154
- package/README.zh-CN.md +314 -0
- package/dist/index.cjs +1887 -1066
- package/dist/index.d.ts +368 -215
- package/dist/index.mjs +1884 -1065
- package/dist/src/error-code.d.ts +2 -24
- package/dist/src/layers/application/abstract-application-layer.d.ts +12 -7
- package/dist/src/layers/application/ascii-application-layer.d.ts +8 -9
- package/dist/src/layers/application/rtu-application-layer.d.ts +21 -44
- package/dist/src/layers/application/tcp-application-layer.d.ts +8 -6
- package/dist/src/layers/physical/abstract-physical-layer.d.ts +40 -12
- package/dist/src/layers/physical/index.d.ts +9 -3
- package/dist/src/layers/physical/serial-physical-layer.d.ts +43 -13
- package/dist/src/layers/physical/tcp-client-physical-layer.d.ts +12 -10
- package/dist/src/layers/physical/tcp-physical-connection.d.ts +17 -0
- package/dist/src/layers/physical/tcp-server-physical-layer.d.ts +23 -12
- package/dist/src/layers/physical/udp-client-physical-layer.d.ts +35 -0
- package/dist/src/layers/physical/udp-server-physical-layer.d.ts +54 -0
- package/dist/src/layers/physical/utils.d.ts +39 -0
- package/dist/src/layers/physical/vars.d.ts +11 -0
- package/dist/src/master/master.d.ts +45 -17
- package/dist/src/slave/slave.d.ts +57 -33
- package/dist/src/types.d.ts +2 -2
- package/dist/src/utils/index.d.ts +4 -2
- package/dist/src/utils/rtu-timing.d.ts +49 -0
- package/dist/src/utils/whitelist.d.ts +11 -0
- package/package.json +8 -8
- package/dist/src/layers/physical/udp-physical-layer.d.ts +0 -42
- package/dist/src/utils/genConnectionId.d.ts +0 -2
- package/dist/test/adu-buffer.test.d.ts +0 -1
- package/dist/test/ascii-hex-sentry.test.d.ts +0 -1
- package/dist/test/ascii-hex-validation.test.d.ts +0 -1
- package/dist/test/ascii-tcp-fragmentation.test.d.ts +0 -1
- package/dist/test/check-range.test.d.ts +0 -1
- package/dist/test/fallback-atomic.test.d.ts +0 -1
- package/dist/test/fallback-serial.test.d.ts +0 -1
- package/dist/test/fc17-serverid-validation.test.d.ts +0 -1
- package/dist/test/fc43-conformity.test.d.ts +0 -1
- package/dist/test/fc43-utf8-objects.test.d.ts +0 -1
- package/dist/test/gen-connection-id.test.d.ts +0 -1
- package/dist/test/helpers/raw-tcp.d.ts +0 -38
- package/dist/test/master-concurrent.test.d.ts +0 -1
- package/dist/test/modbus-error.test.d.ts +0 -1
- package/dist/test/physical-lifecycle.test.d.ts +0 -1
- package/dist/test/predict-rtu.test.d.ts +0 -1
- package/dist/test/rtu-custom-fc.test.d.ts +0 -1
- package/dist/test/rtu-pool-overflow.test.d.ts +0 -1
- package/dist/test/rtu-t15-timing.test.d.ts +0 -1
- package/dist/test/rtu-t35-default.test.d.ts +0 -1
- package/dist/test/rtu-t35-strict.test.d.ts +0 -1
- package/dist/test/rtu-tcp-fragmentation.test.d.ts +0 -1
- package/dist/test/serial-e2e.test.d.ts +0 -1
- package/dist/test/slave-multi-connection.test.d.ts +0 -1
- package/dist/test/slave.test.d.ts +0 -1
- package/dist/test/tcp-fragmentation.test.d.ts +0 -1
- package/dist/test/udp-multi-client.test.d.ts +0 -1
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { AbstractPhysicalLayer } from '../layers/physical';
|
|
1
|
+
import type { AsciiApplicationLayerOptions } from '../layers/application';
|
|
2
|
+
import type { AbstractPhysicalLayer, AbstractPhysicalLayerEvents, OpenArgs, PhysicalConfig } from '../layers/physical';
|
|
3
3
|
import type { CustomFunctionCode, DeviceIdentification, ServerId } from '../types';
|
|
4
|
+
import type { RtuProtocolOptions } from '../utils';
|
|
4
5
|
import EventEmitter from 'node:events';
|
|
5
|
-
|
|
6
|
-
error: [error: Error];
|
|
7
|
-
close: [];
|
|
8
|
-
}
|
|
6
|
+
import { PhysicalState } from '../layers/physical';
|
|
9
7
|
interface ReturnValue<T> {
|
|
10
8
|
transaction?: number;
|
|
11
9
|
unit: number;
|
|
@@ -22,20 +20,34 @@ export interface ModbusMasterOptions {
|
|
|
22
20
|
* Default false (FIFO queue, requests are serialized).
|
|
23
21
|
*/
|
|
24
22
|
concurrent?: boolean;
|
|
23
|
+
physical: PhysicalConfig;
|
|
24
|
+
protocol: {
|
|
25
|
+
type: 'RTU';
|
|
26
|
+
opts?: RtuProtocolOptions;
|
|
27
|
+
} | {
|
|
28
|
+
type: 'TCP';
|
|
29
|
+
} | {
|
|
30
|
+
type: 'ASCII';
|
|
31
|
+
opts?: AsciiApplicationLayerOptions;
|
|
32
|
+
};
|
|
25
33
|
}
|
|
26
|
-
export declare class ModbusMaster<
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
export declare class ModbusMaster<T extends ModbusMasterOptions = ModbusMasterOptions> extends EventEmitter<AbstractPhysicalLayerEvents> {
|
|
35
|
+
readonly timeout: number;
|
|
36
|
+
readonly concurrent: boolean;
|
|
29
37
|
private _masterSession;
|
|
38
|
+
private _physicalLayer;
|
|
39
|
+
private _protocol;
|
|
40
|
+
private _appLayer?;
|
|
41
|
+
private _customFunctionCodes;
|
|
30
42
|
private _queue;
|
|
31
43
|
private _draining;
|
|
32
44
|
private _nextTid;
|
|
33
|
-
private
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
get
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
private _cleanupFns;
|
|
46
|
+
private _closePromise;
|
|
47
|
+
get state(): PhysicalState;
|
|
48
|
+
get physicalLayer(): AbstractPhysicalLayer;
|
|
49
|
+
constructor(options: T);
|
|
50
|
+
private _createAppLayer;
|
|
39
51
|
private send;
|
|
40
52
|
private _drain;
|
|
41
53
|
private _exchange;
|
|
@@ -97,8 +109,24 @@ export declare class ModbusMaster<A extends AbstractApplicationLayer, P extends
|
|
|
97
109
|
sendCustomFC(unit: 0, fc: number, data: Buffer | number[], timeout?: number): Promise<void>;
|
|
98
110
|
sendCustomFC(unit: number, fc: number, data: Buffer | number[], timeout?: number): Promise<Buffer>;
|
|
99
111
|
private _clean;
|
|
100
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Open the underlying physical layer and begin accepting connections.
|
|
114
|
+
*
|
|
115
|
+
* A `ModbusMaster` instance can only be opened once. Once {@link close}
|
|
116
|
+
* is called — explicitly or because the physical layer disconnected —
|
|
117
|
+
* the instance is permanently closed and cannot be reopened.
|
|
118
|
+
* Create a new `ModbusMaster` if a new connection is required.
|
|
119
|
+
*/
|
|
120
|
+
open(...args: OpenArgs<T['physical']>): Promise<void>;
|
|
121
|
+
private _close;
|
|
122
|
+
/**
|
|
123
|
+
* Permanently close the master and release all resources.
|
|
124
|
+
*
|
|
125
|
+
* After calling this method the instance is considered dead:
|
|
126
|
+
* - No further requests can be sent.
|
|
127
|
+
* - The instance cannot be reopened via {@link open}.
|
|
128
|
+
* - All event listeners registered on this master are removed.
|
|
129
|
+
*/
|
|
101
130
|
close(): Promise<void>;
|
|
102
|
-
destroy(): Promise<void>;
|
|
103
131
|
}
|
|
104
132
|
export {};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { AbstractPhysicalLayer } from '../layers/physical';
|
|
3
|
-
import type { CustomFunctionCode,
|
|
1
|
+
import type { AsciiApplicationLayerOptions } from '../layers/application';
|
|
2
|
+
import type { AbstractPhysicalLayer, AbstractPhysicalLayerEvents, OpenArgs, PhysicalConfig } from '../layers/physical';
|
|
3
|
+
import type { CustomFunctionCode, MaybeAsyncFunction, ServerId } from '../types';
|
|
4
|
+
import type { RtuProtocolOptions } from '../utils';
|
|
4
5
|
import EventEmitter from 'node:events';
|
|
6
|
+
import { PhysicalState } from '../layers/physical';
|
|
5
7
|
export interface ModbusSlaveModel {
|
|
6
8
|
unit?: number;
|
|
7
9
|
/**
|
|
@@ -10,27 +12,27 @@ export interface ModbusSlaveModel {
|
|
|
10
12
|
* If provide the return value, use this value as data of `PDU` to respond.
|
|
11
13
|
* Otherwise keep the default read and write behavior.
|
|
12
14
|
*/
|
|
13
|
-
interceptor?:
|
|
14
|
-
readDiscreteInputs?:
|
|
15
|
-
readCoils?:
|
|
16
|
-
writeSingleCoil?:
|
|
15
|
+
interceptor?: MaybeAsyncFunction<(fc: number, data: Buffer) => Buffer | undefined>;
|
|
16
|
+
readDiscreteInputs?: MaybeAsyncFunction<(address: number, length: number) => boolean[]>;
|
|
17
|
+
readCoils?: MaybeAsyncFunction<(address: number, length: number) => boolean[]>;
|
|
18
|
+
writeSingleCoil?: MaybeAsyncFunction<(address: number, value: boolean) => void>;
|
|
17
19
|
/**
|
|
18
20
|
* If omitted, defaults to loop and call `writeSingleCoil`.
|
|
19
21
|
*/
|
|
20
|
-
writeMultipleCoils?:
|
|
21
|
-
readInputRegisters?:
|
|
22
|
-
readHoldingRegisters?:
|
|
23
|
-
writeSingleRegister?:
|
|
22
|
+
writeMultipleCoils?: MaybeAsyncFunction<(address: number, value: boolean[]) => void>;
|
|
23
|
+
readInputRegisters?: MaybeAsyncFunction<(address: number, length: number) => number[]>;
|
|
24
|
+
readHoldingRegisters?: MaybeAsyncFunction<(address: number, length: number) => number[]>;
|
|
25
|
+
writeSingleRegister?: MaybeAsyncFunction<(address: number, value: number) => void>;
|
|
24
26
|
/**
|
|
25
27
|
* If omitted, defaults to loop and call `writeSingleRegister`.
|
|
26
28
|
*/
|
|
27
|
-
writeMultipleRegisters?:
|
|
29
|
+
writeMultipleRegisters?: MaybeAsyncFunction<(address: number, value: number[]) => void>;
|
|
28
30
|
/**
|
|
29
31
|
* If omitted, defaults to call `readHoldingRegisters` and `writeSingleRegister`.
|
|
30
32
|
*/
|
|
31
|
-
maskWriteRegister?:
|
|
32
|
-
reportServerId?:
|
|
33
|
-
readDeviceIdentification?:
|
|
33
|
+
maskWriteRegister?: MaybeAsyncFunction<(address: number, andMask: number, orMask: number) => void>;
|
|
34
|
+
reportServerId?: MaybeAsyncFunction<() => ServerId>;
|
|
35
|
+
readDeviceIdentification?: MaybeAsyncFunction<() => {
|
|
34
36
|
[index: number]: string;
|
|
35
37
|
}>;
|
|
36
38
|
getAddressRange?: () => {
|
|
@@ -40,10 +42,6 @@ export interface ModbusSlaveModel {
|
|
|
40
42
|
holdingRegisters?: [number, number] | [number, number][];
|
|
41
43
|
};
|
|
42
44
|
}
|
|
43
|
-
interface ModbusSlaveEvents {
|
|
44
|
-
error: [error: Error];
|
|
45
|
-
close: [];
|
|
46
|
-
}
|
|
47
45
|
export interface ModbusSlaveOptions {
|
|
48
46
|
/**
|
|
49
47
|
* Pipelined concurrent processing of requests within one connection.
|
|
@@ -51,19 +49,31 @@ export interface ModbusSlaveOptions {
|
|
|
51
49
|
* Default false — per-connection FIFO. RTU/ASCII + concurrent=true throws.
|
|
52
50
|
*/
|
|
53
51
|
concurrent?: boolean;
|
|
52
|
+
physical: PhysicalConfig;
|
|
53
|
+
protocol: {
|
|
54
|
+
type: 'RTU';
|
|
55
|
+
opts?: RtuProtocolOptions;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'TCP';
|
|
58
|
+
} | {
|
|
59
|
+
type: 'ASCII';
|
|
60
|
+
opts?: AsciiApplicationLayerOptions;
|
|
61
|
+
};
|
|
54
62
|
}
|
|
55
|
-
export declare class ModbusSlave<
|
|
56
|
-
|
|
57
|
-
private physicalLayer;
|
|
58
|
-
models: Map<number, ModbusSlaveModel>;
|
|
63
|
+
export declare class ModbusSlave<T extends ModbusSlaveOptions = ModbusSlaveOptions> extends EventEmitter<AbstractPhysicalLayerEvents> {
|
|
64
|
+
readonly models: Map<number, ModbusSlaveModel>;
|
|
59
65
|
readonly concurrent: boolean;
|
|
60
|
-
private
|
|
66
|
+
private _physicalLayer;
|
|
67
|
+
private _protocol;
|
|
68
|
+
private _appLayers;
|
|
61
69
|
private _customFunctionCodes;
|
|
62
70
|
private _locks;
|
|
63
|
-
private
|
|
64
|
-
|
|
65
|
-
get
|
|
66
|
-
|
|
71
|
+
private _cleanupFns;
|
|
72
|
+
private _closePromise;
|
|
73
|
+
get state(): PhysicalState;
|
|
74
|
+
get physicalLayer(): AbstractPhysicalLayer;
|
|
75
|
+
constructor(options: T);
|
|
76
|
+
private _createAppLayer;
|
|
67
77
|
private handleFC1;
|
|
68
78
|
private handleFC2;
|
|
69
79
|
private handleFC3;
|
|
@@ -80,15 +90,29 @@ export declare class ModbusSlave<A extends AbstractApplicationLayer, P extends A
|
|
|
80
90
|
private _drain;
|
|
81
91
|
private _processFrame;
|
|
82
92
|
private _intercept;
|
|
83
|
-
private
|
|
93
|
+
private _withAddressLock;
|
|
84
94
|
private _handleFC;
|
|
85
95
|
add(model: ModbusSlaveModel): void;
|
|
86
96
|
remove(unit: number): void;
|
|
87
97
|
addCustomFunctionCode(cfc: CustomFunctionCode): void;
|
|
88
98
|
removeCustomFunctionCode(fc: number): void;
|
|
89
|
-
|
|
90
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Open the underlying physical layer and begin accepting connections.
|
|
101
|
+
*
|
|
102
|
+
* A `ModbusSlave` instance can only be opened once. Once {@link close}
|
|
103
|
+
* is called — explicitly or because the physical layer disconnected —
|
|
104
|
+
* the instance is permanently closed and cannot be reopened.
|
|
105
|
+
* Create a new `ModbusSlave` if a new server is required.
|
|
106
|
+
*/
|
|
107
|
+
open(...args: OpenArgs<T['physical']>): Promise<void>;
|
|
108
|
+
private _close;
|
|
109
|
+
/**
|
|
110
|
+
* Permanently close the slave and release all resources.
|
|
111
|
+
*
|
|
112
|
+
* After calling this method the instance is considered dead:
|
|
113
|
+
* - No further requests can be processed.
|
|
114
|
+
* - The instance cannot be reopened via {@link open}.
|
|
115
|
+
* - All event listeners registered on this slave are removed.
|
|
116
|
+
*/
|
|
91
117
|
close(): Promise<void>;
|
|
92
|
-
destroy(): Promise<void>;
|
|
93
118
|
}
|
|
94
|
-
export {};
|
package/dist/src/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type Callback<T> = (error: Error | null, value: T) => void;
|
|
2
|
-
export type
|
|
2
|
+
export type MaybeAsyncFunction<F extends (...args: any) => any> = F extends (...args: infer A) => infer R ? ((...args: A) => Promise<R>) | ((...args: A) => R) : never;
|
|
3
3
|
export interface ApplicationDataUnit {
|
|
4
4
|
transaction?: number;
|
|
5
5
|
unit: number;
|
|
@@ -50,5 +50,5 @@ export interface CustomFunctionCode {
|
|
|
50
50
|
* Throwing inside `handle` is turned into a Modbus exception response by the slave.
|
|
51
51
|
* If `handle` is omitted, the slave returns an ILLEGAL_FUNCTION exception for this FC.
|
|
52
52
|
*/
|
|
53
|
-
handle?:
|
|
53
|
+
handle?: MaybeAsyncFunction<(data: Buffer, unit: number) => Buffer>;
|
|
54
54
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
export type { PredictResult } from './predictRtuFrameLength';
|
|
2
|
+
export type { RtuProtocolOptions, ResolvedRtuTiming } from './rtu-timing';
|
|
1
3
|
export { checkRange } from './checkRange';
|
|
2
4
|
export { crc } from './crc';
|
|
3
|
-
export { genConnectionId } from './genConnectionId';
|
|
4
5
|
export { bitsToMs } from './bitsToMs';
|
|
5
6
|
export { isUint8 } from './isUint8';
|
|
6
7
|
export { lrc } from './lrc';
|
|
7
8
|
export { predictRtuFrameLength } from './predictRtuFrameLength';
|
|
8
|
-
export
|
|
9
|
+
export { resolveRtuTiming } from './rtu-timing';
|
|
10
|
+
export { isWhitelisted } from './whitelist';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** User-facing RTU protocol options (supports both bit and ms units). */
|
|
2
|
+
export interface RtuProtocolOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Inter-frame silence (Modbus RTU t3.5).
|
|
5
|
+
* - `{ unit: 'bit', value: 38.5 }` — spec bit-time approximation (default when `baudRate` is provided)
|
|
6
|
+
* - `{ unit: 'ms', value: 20 }` — explicit milliseconds
|
|
7
|
+
*
|
|
8
|
+
* Per Modbus V1.02 §2.5.1.1, at baud rates > 19200 a fixed 1.75 ms is used
|
|
9
|
+
* regardless of the bit value.
|
|
10
|
+
*/
|
|
11
|
+
intervalBetweenFrames?: {
|
|
12
|
+
unit: 'bit' | 'ms';
|
|
13
|
+
value: number;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Inter-character timeout (Modbus RTU t1.5). Opt-in; **disabled** by default.
|
|
17
|
+
* - `{ unit: 'bit', value: 21 }` — bit-time approximation (~1.5 char times)
|
|
18
|
+
* - `{ unit: 'ms', value: 1 }` — explicit milliseconds
|
|
19
|
+
*
|
|
20
|
+
* Per Modbus V1.02 §2.5.1.1, at baud rates > 19200 a fixed 0.75 ms is used
|
|
21
|
+
* regardless of the bit value.
|
|
22
|
+
*/
|
|
23
|
+
interCharTimeout?: {
|
|
24
|
+
unit: 'bit' | 'ms';
|
|
25
|
+
value: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Buffer pool size per connection (bytes). Defaults to `MAX_FRAME_LENGTH * 2`
|
|
29
|
+
* (512 bytes). Increase this if you expect frames larger than 256 bytes or
|
|
30
|
+
* heavy pipelining on a single connection.
|
|
31
|
+
*/
|
|
32
|
+
poolSize?: number;
|
|
33
|
+
}
|
|
34
|
+
/** Resolved RTU timing values in milliseconds. */
|
|
35
|
+
export interface ResolvedRtuTiming {
|
|
36
|
+
intervalBetweenFrames: number;
|
|
37
|
+
interCharTimeout: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolve Modbus RTU timing parameters from user options into milliseconds.
|
|
41
|
+
*
|
|
42
|
+
* - `intervalBetweenFrames` (t3.5): if omitted and `baudRate` is present,
|
|
43
|
+
* defaults to 38.5 bits per Modbus V1.02 §2.5.1.1.
|
|
44
|
+
* - `interCharTimeout` (t1.5): opt-in; only resolved when explicitly provided.
|
|
45
|
+
*
|
|
46
|
+
* Per the spec, at baud rates > 19200 fixed values are used
|
|
47
|
+
* (1.75 ms for t3.5, 0.75 ms for t1.5) regardless of the bit value.
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveRtuTiming(opts?: RtuProtocolOptions, baudRate?: number): ResolvedRtuTiming;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize an IP address by stripping the IPv4-mapped IPv6 prefix.
|
|
3
|
+
* This ensures consistent comparison of addresses like `::ffff:192.168.1.1`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function normalizeAddress(address?: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Check whether a remote address is allowed by the given whitelist.
|
|
8
|
+
* IPv4-mapped IPv6 addresses are normalized before comparison.
|
|
9
|
+
* Returns `true` when whitelist is absent or empty.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isWhitelisted(address: string | undefined, whitelist: string[] | undefined): boolean;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "njs-modbus",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A pure JavaScript
|
|
3
|
+
"version": "3.0.2",
|
|
4
|
+
"description": "A pure JavaScript implementation of Modbus for Node.js.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"modbus",
|
|
7
7
|
"rtu",
|
|
@@ -10,17 +10,16 @@
|
|
|
10
10
|
"tcp",
|
|
11
11
|
"udp"
|
|
12
12
|
],
|
|
13
|
-
"homepage": "https://github.com/xiejay97/njs-modbus/tree/main
|
|
13
|
+
"homepage": "https://github.com/xiejay97/njs-modbus/tree/main#readme",
|
|
14
14
|
"bugs": {
|
|
15
15
|
"url": "https://github.com/xiejay97/njs-modbus/issues"
|
|
16
16
|
},
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/xiejay97/njs-modbus.git"
|
|
19
|
+
"url": "git+https://github.com/xiejay97/njs-modbus.git"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "Xie Jay <xiejay97@gmail.com>",
|
|
23
|
-
"packageManager": "pnpm@10.10.0",
|
|
24
23
|
"sideEffects": false,
|
|
25
24
|
"type": "module",
|
|
26
25
|
"main": "dist/index.cjs",
|
|
@@ -32,8 +31,8 @@
|
|
|
32
31
|
"*.d.ts"
|
|
33
32
|
],
|
|
34
33
|
"scripts": {
|
|
35
|
-
"build": "rollup -c",
|
|
36
|
-
"test": "tsx --test test
|
|
34
|
+
"build": "node -e \"fs.rmSync('dist', {recursive: true, force: true})\" && rollup -c",
|
|
35
|
+
"test": "tsx --test test/**/*.test.ts",
|
|
37
36
|
"util:sort-package-json": "sort-package-json"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
@@ -62,7 +61,8 @@
|
|
|
62
61
|
"tslib": "^2.8.1",
|
|
63
62
|
"tsx": "^4.21.0",
|
|
64
63
|
"typescript": "~5.8.3",
|
|
65
|
-
"typescript-eslint": "^8.34.0"
|
|
64
|
+
"typescript-eslint": "^8.34.0",
|
|
65
|
+
"why-is-node-running": "^3.2.2"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"serialport": ">=12.0.0"
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { BindOptions, SocketOptions } from 'node:dgram';
|
|
2
|
-
import { AbstractPhysicalLayer } from './abstract-physical-layer';
|
|
3
|
-
export interface UdpPhysicalLayerOptions {
|
|
4
|
-
/** dgram Socket creation options. */
|
|
5
|
-
socket?: Partial<SocketOptions>;
|
|
6
|
-
/**
|
|
7
|
-
* Provided → client mode (send to this single remote, filter inbound).
|
|
8
|
-
* Omitted → server mode (bind locally, accept from any rinfo).
|
|
9
|
-
*/
|
|
10
|
-
remote?: {
|
|
11
|
-
port?: number;
|
|
12
|
-
address?: string;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Server mode only. Each unique rinfo (`address:port`) gets its own
|
|
16
|
-
* `PhysicalConnection`; if no datagram arrives within this many ms, the
|
|
17
|
-
* connection is evicted (`connection-close` emitted, upper-layer framing
|
|
18
|
-
* state released). Default 30000. Set 0 to disable eviction.
|
|
19
|
-
*/
|
|
20
|
-
idleTimeout?: number;
|
|
21
|
-
}
|
|
22
|
-
export declare class UdpPhysicalLayer extends AbstractPhysicalLayer {
|
|
23
|
-
TYPE: 'SERIAL' | 'NET';
|
|
24
|
-
private _socket;
|
|
25
|
-
private _connections;
|
|
26
|
-
private _isOpen;
|
|
27
|
-
private _isOpening;
|
|
28
|
-
private _destroyed;
|
|
29
|
-
private _socketOptions?;
|
|
30
|
-
private _port;
|
|
31
|
-
private _address?;
|
|
32
|
-
private _idleTimeout;
|
|
33
|
-
isServer: boolean;
|
|
34
|
-
get isOpen(): boolean;
|
|
35
|
-
get destroyed(): boolean;
|
|
36
|
-
constructor(options?: UdpPhysicalLayerOptions);
|
|
37
|
-
open(options?: BindOptions): Promise<void>;
|
|
38
|
-
private _handleMessage;
|
|
39
|
-
write(data: Buffer): Promise<void>;
|
|
40
|
-
close(): Promise<void>;
|
|
41
|
-
destroy(): Promise<void>;
|
|
42
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { Server } from 'node:net';
|
|
2
|
-
import { Socket } from 'node:net';
|
|
3
|
-
export interface RawClient {
|
|
4
|
-
socket: Socket;
|
|
5
|
-
write: (data: Buffer) => Promise<void>;
|
|
6
|
-
end: () => Promise<void>;
|
|
7
|
-
onData: (callback: (chunk: Buffer) => void) => void;
|
|
8
|
-
collected: () => Buffer;
|
|
9
|
-
}
|
|
10
|
-
export declare function connectRawClient(port: number, host?: string): Promise<RawClient>;
|
|
11
|
-
export interface RawServer {
|
|
12
|
-
server: Server;
|
|
13
|
-
port: number;
|
|
14
|
-
/** Resolves with the first accepted socket. */
|
|
15
|
-
firstSocket: Promise<Socket>;
|
|
16
|
-
close: () => Promise<void>;
|
|
17
|
-
}
|
|
18
|
-
export declare function startRawServer(port?: number, host?: string): Promise<RawServer>;
|
|
19
|
-
export declare function sleep(ms: number): Promise<void>;
|
|
20
|
-
/** Build a Modbus TCP MBAP request frame. */
|
|
21
|
-
export declare function buildMbapRequest(opts: {
|
|
22
|
-
transaction: number;
|
|
23
|
-
unit: number;
|
|
24
|
-
fc: number;
|
|
25
|
-
data: Buffer | number[];
|
|
26
|
-
}): Buffer;
|
|
27
|
-
/** Build a Modbus RTU frame (PDU + 2-byte CRC, little-endian). */
|
|
28
|
-
export declare function buildRtuFrame(opts: {
|
|
29
|
-
unit: number;
|
|
30
|
-
fc: number;
|
|
31
|
-
data: Buffer | number[];
|
|
32
|
-
}): Buffer;
|
|
33
|
-
/** Build a Modbus ASCII frame: `:` + hex(unit + fc + data + LRC) + CRLF. */
|
|
34
|
-
export declare function buildAsciiFrame(opts: {
|
|
35
|
-
unit: number;
|
|
36
|
-
fc: number;
|
|
37
|
-
data: Buffer | number[];
|
|
38
|
-
}): Buffer;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|