njs-modbus 1.1.0 → 1.3.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.
- package/README.md +2 -2
- package/dist/index.cjs +29 -5
- package/dist/index.d.ts +10 -4
- package/dist/index.mjs +29 -5
- package/dist/src/layers/application/rtu-application-layer.d.ts +7 -4
- package/dist/src/layers/physical/abstract-physical-layer.d.ts +1 -0
- package/dist/src/layers/physical/serial-physical-layer.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -69,7 +69,7 @@ npm install njs-modbus
|
|
|
69
69
|
import { SerialPhysicalLayer, RtuApplicationLayer, ModbusMaster } from 'njs-modbus';
|
|
70
70
|
|
|
71
71
|
const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
|
|
72
|
-
const applicationLayer = new RtuApplicationLayer(physicalLayer
|
|
72
|
+
const applicationLayer = new RtuApplicationLayer(physicalLayer);
|
|
73
73
|
|
|
74
74
|
const modbusMaster = new ModbusMaster(applicationLayer, physicalLayer);
|
|
75
75
|
|
|
@@ -99,7 +99,7 @@ const MB_SERVER = {
|
|
|
99
99
|
};
|
|
100
100
|
|
|
101
101
|
const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
|
|
102
|
-
const applicationLayer = new RtuApplicationLayer(physicalLayer
|
|
102
|
+
const applicationLayer = new RtuApplicationLayer(physicalLayer);
|
|
103
103
|
|
|
104
104
|
const modbusSlave = new ModbusSlave(
|
|
105
105
|
{
|
package/dist/index.cjs
CHANGED
|
@@ -38,6 +38,9 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
38
38
|
get destroyed() {
|
|
39
39
|
return this._destroyed;
|
|
40
40
|
}
|
|
41
|
+
get baudRate() {
|
|
42
|
+
return this._baudRate;
|
|
43
|
+
}
|
|
41
44
|
constructor(options) {
|
|
42
45
|
super();
|
|
43
46
|
Object.defineProperty(this, "TYPE", {
|
|
@@ -58,7 +61,14 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
58
61
|
writable: true,
|
|
59
62
|
value: false
|
|
60
63
|
});
|
|
64
|
+
Object.defineProperty(this, "_baudRate", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
configurable: true,
|
|
67
|
+
writable: true,
|
|
68
|
+
value: void 0
|
|
69
|
+
});
|
|
61
70
|
this._serialport = new serialport.SerialPort(Object.assign(Object.assign({}, options), { autoOpen: false }));
|
|
71
|
+
this._baudRate = options.baudRate;
|
|
62
72
|
}
|
|
63
73
|
open() {
|
|
64
74
|
if (this.destroyed) {
|
|
@@ -93,6 +103,7 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
93
103
|
reject(error);
|
|
94
104
|
}
|
|
95
105
|
else {
|
|
106
|
+
this.emit('write', data);
|
|
96
107
|
resolve();
|
|
97
108
|
}
|
|
98
109
|
});
|
|
@@ -189,6 +200,7 @@ class TcpClientPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
189
200
|
reject(error);
|
|
190
201
|
}
|
|
191
202
|
else {
|
|
203
|
+
this.emit('write', data);
|
|
192
204
|
resolve();
|
|
193
205
|
}
|
|
194
206
|
});
|
|
@@ -443,6 +455,7 @@ class UdpPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
443
455
|
reject(error);
|
|
444
456
|
}
|
|
445
457
|
else {
|
|
458
|
+
this.emit('write', data);
|
|
446
459
|
resolve();
|
|
447
460
|
}
|
|
448
461
|
});
|
|
@@ -533,7 +546,13 @@ function lrc(data) {
|
|
|
533
546
|
}
|
|
534
547
|
|
|
535
548
|
class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
536
|
-
constructor(physicalLayer,
|
|
549
|
+
constructor(physicalLayer,
|
|
550
|
+
/**
|
|
551
|
+
* The time interval between two frames, support two formats:
|
|
552
|
+
* - bit: `48bit` as default
|
|
553
|
+
* - millisecond: `20ms`
|
|
554
|
+
*/
|
|
555
|
+
intervalBetweenFrames) {
|
|
537
556
|
super();
|
|
538
557
|
Object.defineProperty(this, "_timerThreePointFive", {
|
|
539
558
|
enumerable: true,
|
|
@@ -554,10 +573,15 @@ class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
|
554
573
|
value: []
|
|
555
574
|
});
|
|
556
575
|
let threePointFiveT = 0;
|
|
557
|
-
if (physicalLayer.TYPE === 'SERIAL'
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
576
|
+
if (physicalLayer.TYPE === 'SERIAL') {
|
|
577
|
+
if (intervalBetweenFrames && intervalBetweenFrames.endsWith('ms')) {
|
|
578
|
+
threePointFiveT = Number(intervalBetweenFrames.slice(0, -2));
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
threePointFiveT = Math.ceil(physicalLayer.baudRate > 19200
|
|
582
|
+
? 1.8
|
|
583
|
+
: getThreePointFiveT(physicalLayer.baudRate, intervalBetweenFrames ? Number(intervalBetweenFrames.slice(0, -3)) : 48));
|
|
584
|
+
}
|
|
561
585
|
}
|
|
562
586
|
const handleData = (data, response) => {
|
|
563
587
|
this._bufferRx = Buffer.concat([this._bufferRx, data]);
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ declare function getCodeByError(err: Error): ErrorCode;
|
|
|
18
18
|
|
|
19
19
|
interface AbstractPhysicalLayerEvents {
|
|
20
20
|
data: [data: Buffer, response: (data: Buffer) => Promise<void>];
|
|
21
|
+
write: [data: Buffer];
|
|
21
22
|
error: [error: Error];
|
|
22
23
|
close: [];
|
|
23
24
|
}
|
|
@@ -60,8 +61,10 @@ declare class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
60
61
|
TYPE: 'SERIAL' | 'NET';
|
|
61
62
|
private _serialport;
|
|
62
63
|
private _destroyed;
|
|
64
|
+
private _baudRate;
|
|
63
65
|
get isOpen(): boolean;
|
|
64
66
|
get destroyed(): boolean;
|
|
67
|
+
get baudRate(): number;
|
|
65
68
|
constructor(options: SerialPhysicalLayerOptions);
|
|
66
69
|
open(): Promise<void>;
|
|
67
70
|
write(data: Buffer): Promise<void>;
|
|
@@ -161,10 +164,13 @@ declare class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
|
161
164
|
private _timerThreePointFive?;
|
|
162
165
|
private _bufferRx;
|
|
163
166
|
private _removeAllListeners;
|
|
164
|
-
constructor(physicalLayer: SerialPhysicalLayer | TcpServerPhysicalLayer | TcpClientPhysicalLayer | UdpPhysicalLayer,
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
constructor(physicalLayer: SerialPhysicalLayer | TcpServerPhysicalLayer | TcpClientPhysicalLayer | UdpPhysicalLayer,
|
|
168
|
+
/**
|
|
169
|
+
* The time interval between two frames, support two formats:
|
|
170
|
+
* - bit: `48bit` as default
|
|
171
|
+
* - millisecond: `20ms`
|
|
172
|
+
*/
|
|
173
|
+
intervalBetweenFrames?: `${number}bit` | `${number}ms`);
|
|
168
174
|
private framing;
|
|
169
175
|
encode(data: ApplicationDataUnit): Buffer;
|
|
170
176
|
destroy(): void;
|
package/dist/index.mjs
CHANGED
|
@@ -36,6 +36,9 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
36
36
|
get destroyed() {
|
|
37
37
|
return this._destroyed;
|
|
38
38
|
}
|
|
39
|
+
get baudRate() {
|
|
40
|
+
return this._baudRate;
|
|
41
|
+
}
|
|
39
42
|
constructor(options) {
|
|
40
43
|
super();
|
|
41
44
|
Object.defineProperty(this, "TYPE", {
|
|
@@ -56,7 +59,14 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
56
59
|
writable: true,
|
|
57
60
|
value: false
|
|
58
61
|
});
|
|
62
|
+
Object.defineProperty(this, "_baudRate", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
59
68
|
this._serialport = new SerialPort(Object.assign(Object.assign({}, options), { autoOpen: false }));
|
|
69
|
+
this._baudRate = options.baudRate;
|
|
60
70
|
}
|
|
61
71
|
open() {
|
|
62
72
|
if (this.destroyed) {
|
|
@@ -91,6 +101,7 @@ class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
91
101
|
reject(error);
|
|
92
102
|
}
|
|
93
103
|
else {
|
|
104
|
+
this.emit('write', data);
|
|
94
105
|
resolve();
|
|
95
106
|
}
|
|
96
107
|
});
|
|
@@ -187,6 +198,7 @@ class TcpClientPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
187
198
|
reject(error);
|
|
188
199
|
}
|
|
189
200
|
else {
|
|
201
|
+
this.emit('write', data);
|
|
190
202
|
resolve();
|
|
191
203
|
}
|
|
192
204
|
});
|
|
@@ -441,6 +453,7 @@ class UdpPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
441
453
|
reject(error);
|
|
442
454
|
}
|
|
443
455
|
else {
|
|
456
|
+
this.emit('write', data);
|
|
444
457
|
resolve();
|
|
445
458
|
}
|
|
446
459
|
});
|
|
@@ -531,7 +544,13 @@ function lrc(data) {
|
|
|
531
544
|
}
|
|
532
545
|
|
|
533
546
|
class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
534
|
-
constructor(physicalLayer,
|
|
547
|
+
constructor(physicalLayer,
|
|
548
|
+
/**
|
|
549
|
+
* The time interval between two frames, support two formats:
|
|
550
|
+
* - bit: `48bit` as default
|
|
551
|
+
* - millisecond: `20ms`
|
|
552
|
+
*/
|
|
553
|
+
intervalBetweenFrames) {
|
|
535
554
|
super();
|
|
536
555
|
Object.defineProperty(this, "_timerThreePointFive", {
|
|
537
556
|
enumerable: true,
|
|
@@ -552,10 +571,15 @@ class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
|
552
571
|
value: []
|
|
553
572
|
});
|
|
554
573
|
let threePointFiveT = 0;
|
|
555
|
-
if (physicalLayer.TYPE === 'SERIAL'
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
574
|
+
if (physicalLayer.TYPE === 'SERIAL') {
|
|
575
|
+
if (intervalBetweenFrames && intervalBetweenFrames.endsWith('ms')) {
|
|
576
|
+
threePointFiveT = Number(intervalBetweenFrames.slice(0, -2));
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
threePointFiveT = Math.ceil(physicalLayer.baudRate > 19200
|
|
580
|
+
? 1.8
|
|
581
|
+
: getThreePointFiveT(physicalLayer.baudRate, intervalBetweenFrames ? Number(intervalBetweenFrames.slice(0, -3)) : 48));
|
|
582
|
+
}
|
|
559
583
|
}
|
|
560
584
|
const handleData = (data, response) => {
|
|
561
585
|
this._bufferRx = Buffer.concat([this._bufferRx, data]);
|
|
@@ -6,10 +6,13 @@ export declare class RtuApplicationLayer extends AbstractApplicationLayer {
|
|
|
6
6
|
private _timerThreePointFive?;
|
|
7
7
|
private _bufferRx;
|
|
8
8
|
private _removeAllListeners;
|
|
9
|
-
constructor(physicalLayer: SerialPhysicalLayer | TcpServerPhysicalLayer | TcpClientPhysicalLayer | UdpPhysicalLayer,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
constructor(physicalLayer: SerialPhysicalLayer | TcpServerPhysicalLayer | TcpClientPhysicalLayer | UdpPhysicalLayer,
|
|
10
|
+
/**
|
|
11
|
+
* The time interval between two frames, support two formats:
|
|
12
|
+
* - bit: `48bit` as default
|
|
13
|
+
* - millisecond: `20ms`
|
|
14
|
+
*/
|
|
15
|
+
intervalBetweenFrames?: `${number}bit` | `${number}ms`);
|
|
13
16
|
private framing;
|
|
14
17
|
encode(data: ApplicationDataUnit): Buffer;
|
|
15
18
|
destroy(): void;
|
|
@@ -28,8 +28,10 @@ export declare class SerialPhysicalLayer extends AbstractPhysicalLayer {
|
|
|
28
28
|
TYPE: 'SERIAL' | 'NET';
|
|
29
29
|
private _serialport;
|
|
30
30
|
private _destroyed;
|
|
31
|
+
private _baudRate;
|
|
31
32
|
get isOpen(): boolean;
|
|
32
33
|
get destroyed(): boolean;
|
|
34
|
+
get baudRate(): number;
|
|
33
35
|
constructor(options: SerialPhysicalLayerOptions);
|
|
34
36
|
open(): Promise<void>;
|
|
35
37
|
write(data: Buffer): Promise<void>;
|