njs-modbus 1.3.0 → 1.3.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/dist/index.mjs DELETED
@@ -1,1896 +0,0 @@
1
- import EventEmitter from 'node:events';
2
- import { SerialPort } from 'serialport';
3
- import { Socket, createServer } from 'node:net';
4
- import { createSocket } from 'node:dgram';
5
-
6
- var ErrorCode;
7
- (function (ErrorCode) {
8
- ErrorCode[ErrorCode["ILLEGAL_FUNCTION"] = 1] = "ILLEGAL_FUNCTION";
9
- ErrorCode[ErrorCode["ILLEGAL_DATA_ADDRESS"] = 2] = "ILLEGAL_DATA_ADDRESS";
10
- ErrorCode[ErrorCode["ILLEGAL_DATA_VALUE"] = 3] = "ILLEGAL_DATA_VALUE";
11
- ErrorCode[ErrorCode["SERVER_DEVICE_FAILURE"] = 4] = "SERVER_DEVICE_FAILURE";
12
- ErrorCode[ErrorCode["ACKNOWLEDGE"] = 5] = "ACKNOWLEDGE";
13
- ErrorCode[ErrorCode["SERVER_DEVICE_BUSY"] = 6] = "SERVER_DEVICE_BUSY";
14
- ErrorCode[ErrorCode["MEMORY_PARITY_ERROR"] = 8] = "MEMORY_PARITY_ERROR";
15
- ErrorCode[ErrorCode["GATEWAY_PATH_UNAVAILABLE"] = 10] = "GATEWAY_PATH_UNAVAILABLE";
16
- ErrorCode[ErrorCode["GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND"] = 11] = "GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND";
17
- })(ErrorCode || (ErrorCode = {}));
18
- const PREFIX = 'MODBUS_ERROR_CODE_';
19
- function getErrorByCode(code) {
20
- return new Error(PREFIX + code);
21
- }
22
- function getCodeByError(err) {
23
- if (err.message.startsWith(PREFIX)) {
24
- return Number(err.message.slice(PREFIX.length));
25
- }
26
- return ErrorCode.SERVER_DEVICE_FAILURE;
27
- }
28
-
29
- class AbstractPhysicalLayer extends EventEmitter {
30
- }
31
-
32
- class SerialPhysicalLayer extends AbstractPhysicalLayer {
33
- get isOpen() {
34
- return this._serialport.isOpen;
35
- }
36
- get destroyed() {
37
- return this._destroyed;
38
- }
39
- get baudRate() {
40
- return this._baudRate;
41
- }
42
- constructor(options) {
43
- super();
44
- Object.defineProperty(this, "TYPE", {
45
- enumerable: true,
46
- configurable: true,
47
- writable: true,
48
- value: 'SERIAL'
49
- });
50
- Object.defineProperty(this, "_serialport", {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- value: void 0
55
- });
56
- Object.defineProperty(this, "_destroyed", {
57
- enumerable: true,
58
- configurable: true,
59
- writable: true,
60
- value: false
61
- });
62
- Object.defineProperty(this, "_baudRate", {
63
- enumerable: true,
64
- configurable: true,
65
- writable: true,
66
- value: void 0
67
- });
68
- this._serialport = new SerialPort(Object.assign(Object.assign({}, options), { autoOpen: false }));
69
- this._baudRate = options.baudRate;
70
- }
71
- open() {
72
- if (this.destroyed) {
73
- return Promise.reject(new Error('Port is destroyed'));
74
- }
75
- return new Promise((resolve, reject) => {
76
- this._serialport.open((error) => {
77
- if (error) {
78
- reject(error);
79
- }
80
- else {
81
- this._serialport.on('data', (data) => {
82
- this.emit('data', data, (data) => this.write(data));
83
- });
84
- this._serialport.on('error', (error) => {
85
- this.emit('error', error);
86
- });
87
- this._serialport.on('close', () => {
88
- this._serialport.removeAllListeners();
89
- this.emit('close');
90
- });
91
- resolve();
92
- }
93
- });
94
- });
95
- }
96
- write(data) {
97
- return new Promise((resolve, reject) => {
98
- if (this.isOpen) {
99
- this._serialport.write(data, (error) => {
100
- if (error) {
101
- reject(error);
102
- }
103
- else {
104
- this.emit('write', data);
105
- resolve();
106
- }
107
- });
108
- }
109
- else {
110
- reject(new Error('Port is not open'));
111
- }
112
- });
113
- }
114
- close() {
115
- return new Promise((resolve) => {
116
- this._serialport.removeAllListeners();
117
- this._serialport.close(() => {
118
- resolve();
119
- });
120
- });
121
- }
122
- destroy() {
123
- this._destroyed = true;
124
- this.removeAllListeners();
125
- return this.close();
126
- }
127
- }
128
-
129
- class TcpClientPhysicalLayer extends AbstractPhysicalLayer {
130
- get isOpen() {
131
- return this._isOpen;
132
- }
133
- get destroyed() {
134
- return this._destroyed;
135
- }
136
- constructor(options) {
137
- super();
138
- Object.defineProperty(this, "TYPE", {
139
- enumerable: true,
140
- configurable: true,
141
- writable: true,
142
- value: 'NET'
143
- });
144
- Object.defineProperty(this, "_socket", {
145
- enumerable: true,
146
- configurable: true,
147
- writable: true,
148
- value: void 0
149
- });
150
- Object.defineProperty(this, "_isOpen", {
151
- enumerable: true,
152
- configurable: true,
153
- writable: true,
154
- value: false
155
- });
156
- Object.defineProperty(this, "_destroyed", {
157
- enumerable: true,
158
- configurable: true,
159
- writable: true,
160
- value: false
161
- });
162
- this._socket = new Socket(options);
163
- }
164
- open(options) {
165
- if (this.destroyed) {
166
- return Promise.reject(new Error('Port is destroyed'));
167
- }
168
- return new Promise((resolve, reject) => {
169
- let called = false;
170
- this._socket.connect(options !== null && options !== void 0 ? options : { port: 502 }, () => {
171
- called = true;
172
- this._isOpen = true;
173
- this._socket.on('data', (data) => {
174
- this.emit('data', data, (data) => this.write(data));
175
- });
176
- this._socket.on('close', () => {
177
- this._isOpen = false;
178
- this._socket.removeAllListeners();
179
- this.emit('close');
180
- });
181
- resolve();
182
- });
183
- this._socket.on('error', (error) => {
184
- if (called) {
185
- this.emit('error', error);
186
- }
187
- else {
188
- reject(error);
189
- }
190
- });
191
- });
192
- }
193
- write(data) {
194
- return new Promise((resolve, reject) => {
195
- if (this.isOpen) {
196
- this._socket.write(data, (error) => {
197
- if (error) {
198
- reject(error);
199
- }
200
- else {
201
- this.emit('write', data);
202
- resolve();
203
- }
204
- });
205
- }
206
- else {
207
- reject(new Error('Port is not open'));
208
- }
209
- });
210
- }
211
- close() {
212
- return new Promise((resolve) => {
213
- this._isOpen = false;
214
- this._socket.removeAllListeners();
215
- this._socket.destroy();
216
- resolve();
217
- });
218
- }
219
- destroy() {
220
- this._destroyed = true;
221
- this.removeAllListeners();
222
- return this.close();
223
- }
224
- }
225
-
226
- class TcpServerPhysicalLayer extends AbstractPhysicalLayer {
227
- get isOpen() {
228
- return this._isOpen;
229
- }
230
- get destroyed() {
231
- return this._destroyed;
232
- }
233
- constructor(options) {
234
- super();
235
- Object.defineProperty(this, "TYPE", {
236
- enumerable: true,
237
- configurable: true,
238
- writable: true,
239
- value: 'NET'
240
- });
241
- Object.defineProperty(this, "_server", {
242
- enumerable: true,
243
- configurable: true,
244
- writable: true,
245
- value: void 0
246
- });
247
- Object.defineProperty(this, "_isOpen", {
248
- enumerable: true,
249
- configurable: true,
250
- writable: true,
251
- value: false
252
- });
253
- Object.defineProperty(this, "_destroyed", {
254
- enumerable: true,
255
- configurable: true,
256
- writable: true,
257
- value: false
258
- });
259
- Object.defineProperty(this, "_sockets", {
260
- enumerable: true,
261
- configurable: true,
262
- writable: true,
263
- value: new Set()
264
- });
265
- this._server = createServer(options, (socket) => {
266
- this._sockets.add(socket);
267
- socket.on('data', (data) => {
268
- this.emit('data', data, (data) => new Promise((resolve, reject) => {
269
- socket.write(data, (error) => {
270
- if (error) {
271
- reject(error);
272
- }
273
- else {
274
- resolve();
275
- }
276
- });
277
- }));
278
- });
279
- socket.once('close', () => {
280
- socket.removeAllListeners();
281
- this._sockets.delete(socket);
282
- });
283
- });
284
- }
285
- open(options) {
286
- if (this.destroyed) {
287
- return Promise.reject(new Error('Port is destroyed'));
288
- }
289
- return new Promise((resolve, reject) => {
290
- var _a;
291
- let called = false;
292
- this._server.listen(Object.assign(Object.assign({}, options), { port: (_a = options === null || options === void 0 ? void 0 : options.port) !== null && _a !== void 0 ? _a : 502 }), () => {
293
- called = true;
294
- this._isOpen = true;
295
- this._sockets.clear();
296
- this._server.on('close', () => {
297
- this._isOpen = false;
298
- this._server.removeAllListeners();
299
- for (const socket of this._sockets) {
300
- socket.removeAllListeners();
301
- }
302
- this.emit('close');
303
- });
304
- resolve();
305
- });
306
- this._server.on('error', (error) => {
307
- if (called) {
308
- this.emit('error', error);
309
- }
310
- else {
311
- reject(error);
312
- }
313
- });
314
- });
315
- }
316
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
317
- write(data) {
318
- return new Promise((resolve, reject) => {
319
- reject(new Error('Not supported'));
320
- });
321
- }
322
- close() {
323
- return new Promise((resolve) => {
324
- this._isOpen = false;
325
- this._server.removeAllListeners();
326
- for (const socket of this._sockets) {
327
- socket.removeAllListeners();
328
- }
329
- this._server.close(() => {
330
- resolve();
331
- });
332
- });
333
- }
334
- destroy() {
335
- this._destroyed = true;
336
- this.removeAllListeners();
337
- return this.close();
338
- }
339
- }
340
-
341
- class UdpPhysicalLayer extends AbstractPhysicalLayer {
342
- get isOpen() {
343
- return this._isOpen;
344
- }
345
- get destroyed() {
346
- return this._destroyed;
347
- }
348
- /**
349
- *
350
- * @param options
351
- * @param remote If omitted, as server.
352
- * Otherwise as client.
353
- */
354
- constructor(options, remote) {
355
- var _a, _b;
356
- super();
357
- Object.defineProperty(this, "TYPE", {
358
- enumerable: true,
359
- configurable: true,
360
- writable: true,
361
- value: 'NET'
362
- });
363
- Object.defineProperty(this, "_socket", {
364
- enumerable: true,
365
- configurable: true,
366
- writable: true,
367
- value: void 0
368
- });
369
- Object.defineProperty(this, "_isOpen", {
370
- enumerable: true,
371
- configurable: true,
372
- writable: true,
373
- value: false
374
- });
375
- Object.defineProperty(this, "_destroyed", {
376
- enumerable: true,
377
- configurable: true,
378
- writable: true,
379
- value: false
380
- });
381
- Object.defineProperty(this, "_port", {
382
- enumerable: true,
383
- configurable: true,
384
- writable: true,
385
- value: void 0
386
- });
387
- Object.defineProperty(this, "_address", {
388
- enumerable: true,
389
- configurable: true,
390
- writable: true,
391
- value: void 0
392
- });
393
- Object.defineProperty(this, "isServer", {
394
- enumerable: true,
395
- configurable: true,
396
- writable: true,
397
- value: void 0
398
- });
399
- this._socket = createSocket(Object.assign(Object.assign({}, options), { type: (_a = options === null || options === void 0 ? void 0 : options.type) !== null && _a !== void 0 ? _a : 'udp4' }), (msg, rinfo) => {
400
- this.emit('data', msg, (data) => new Promise((resolve, reject) => {
401
- this._socket.send(data, rinfo.port, rinfo.address, (error) => {
402
- if (error) {
403
- reject(error);
404
- }
405
- else {
406
- resolve();
407
- }
408
- });
409
- }));
410
- });
411
- this.isServer = !remote;
412
- this._port = (_b = remote === null || remote === void 0 ? void 0 : remote.port) !== null && _b !== void 0 ? _b : 502;
413
- this._address = remote === null || remote === void 0 ? void 0 : remote.address;
414
- }
415
- open(options) {
416
- if (this.destroyed) {
417
- return Promise.reject(new Error('Port is destroyed'));
418
- }
419
- return new Promise((resolve, reject) => {
420
- var _a;
421
- if (this.isServer) {
422
- let called = false;
423
- this._socket.bind(Object.assign(Object.assign({}, options), { port: (_a = options === null || options === void 0 ? void 0 : options.port) !== null && _a !== void 0 ? _a : 502 }), () => {
424
- called = true;
425
- this._isOpen = true;
426
- this._socket.on('close', () => {
427
- this._isOpen = false;
428
- this._socket.removeAllListeners();
429
- this.emit('close');
430
- });
431
- resolve();
432
- });
433
- this._socket.on('error', (error) => {
434
- if (called) {
435
- this.emit('error', error);
436
- }
437
- else {
438
- reject(error);
439
- }
440
- });
441
- }
442
- else {
443
- this._isOpen = true;
444
- resolve();
445
- }
446
- });
447
- }
448
- write(data) {
449
- return new Promise((resolve, reject) => {
450
- if (this.isOpen) {
451
- this._socket.send(data, this._port, this._address, (error) => {
452
- if (error) {
453
- reject(error);
454
- }
455
- else {
456
- this.emit('write', data);
457
- resolve();
458
- }
459
- });
460
- }
461
- else {
462
- reject(new Error('Port is not open'));
463
- }
464
- });
465
- }
466
- close() {
467
- return new Promise((resolve) => {
468
- this._isOpen = false;
469
- this._socket.removeAllListeners();
470
- this._socket.close(() => {
471
- resolve();
472
- });
473
- });
474
- }
475
- destroy() {
476
- this._destroyed = true;
477
- this.removeAllListeners();
478
- return this.close();
479
- }
480
- }
481
-
482
- class AbstractApplicationLayer extends EventEmitter {
483
- }
484
-
485
- function checkRange(value, range) {
486
- if (range) {
487
- if (typeof range[0] === 'number' && typeof range[1] === 'number') {
488
- if (range[0] < range[1]) {
489
- return (Array.isArray(value) ? value : [value]).every((n) => n >= range[0] && n <= range[1]);
490
- }
491
- }
492
- else if (range.length > 0) {
493
- for (const r of range) {
494
- if (r[0] < r[1]) {
495
- if ((Array.isArray(value) ? value : [value]).every((n) => n >= r[0] && n <= r[1])) {
496
- return true;
497
- }
498
- }
499
- }
500
- return false;
501
- }
502
- }
503
- return true;
504
- }
505
-
506
- const TABLE = [
507
- 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, 0xcc01,
508
- 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, 0xd801, 0x18c0,
509
- 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, 0x1400, 0xd4c1, 0xd581,
510
- 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040, 0xf001, 0x30c0, 0x3180, 0xf141,
511
- 0x3300, 0xf3c1, 0xf281, 0x3240, 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01,
512
- 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0,
513
- 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40, 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681,
514
- 0x2640, 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
515
- 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441, 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, 0xaa01,
516
- 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840, 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, 0xbe01, 0x7ec0,
517
- 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381,
518
- 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041, 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, 0x9601, 0x56c0, 0x5780, 0x9741,
519
- 0x5500, 0x95c1, 0x9481, 0x5440, 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901,
520
- 0x59c0, 0x5880, 0x9841, 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0,
521
- 0x4c80, 0x8c41, 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081,
522
- 0x4040,
523
- ];
524
- function crc(data) {
525
- let crc = 0xffff;
526
- for (let index = 0; index < data.length; index++) {
527
- crc = (TABLE[(crc ^ data[index]) & 0xff] ^ (crc >> 8)) & 0xffff;
528
- }
529
- return crc;
530
- }
531
-
532
- /**
533
- * Get time interval between message frames witch well-known as 3.5T.
534
- * @param baudRate Serial port baud rate.
535
- * @param {number} [approximation=48] Approximate number of bits corresponding to 3.5T.
536
- * @returns `ms`.
537
- */
538
- function getThreePointFiveT(baudRate, approximation = 48) {
539
- return (approximation * 1000) / baudRate;
540
- }
541
-
542
- function lrc(data) {
543
- return (~data.reduce((sum, n) => sum + n, 0) + 1) & 0xff;
544
- }
545
-
546
- class RtuApplicationLayer extends AbstractApplicationLayer {
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) {
554
- super();
555
- Object.defineProperty(this, "_timerThreePointFive", {
556
- enumerable: true,
557
- configurable: true,
558
- writable: true,
559
- value: void 0
560
- });
561
- Object.defineProperty(this, "_bufferRx", {
562
- enumerable: true,
563
- configurable: true,
564
- writable: true,
565
- value: Buffer.alloc(0)
566
- });
567
- Object.defineProperty(this, "_removeAllListeners", {
568
- enumerable: true,
569
- configurable: true,
570
- writable: true,
571
- value: []
572
- });
573
- let threePointFiveT = 0;
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
- }
583
- }
584
- const handleData = (data, response) => {
585
- this._bufferRx = Buffer.concat([this._bufferRx, data]);
586
- clearTimeout(this._timerThreePointFive);
587
- const handleData = () => {
588
- const frame = this.framing(this._bufferRx);
589
- if (frame) {
590
- this.emit('framing', frame, response);
591
- }
592
- this._bufferRx = Buffer.alloc(0);
593
- };
594
- if (threePointFiveT) {
595
- this._timerThreePointFive = setTimeout(handleData, threePointFiveT);
596
- }
597
- else {
598
- handleData();
599
- }
600
- };
601
- physicalLayer.on('data', handleData);
602
- this._removeAllListeners.push(() => {
603
- physicalLayer.removeListener('data', handleData);
604
- });
605
- const handleClose = () => {
606
- clearTimeout(this._timerThreePointFive);
607
- this._bufferRx = Buffer.alloc(0);
608
- };
609
- physicalLayer.on('close', handleClose);
610
- this._removeAllListeners.push(() => {
611
- physicalLayer.removeListener('close', handleClose);
612
- });
613
- }
614
- framing(buffer) {
615
- if (buffer.length >= 4) {
616
- const crcPassed = buffer.readUInt16LE(buffer.length - 2) === crc(buffer.subarray(0, buffer.length - 2));
617
- if (crcPassed) {
618
- return {
619
- unit: buffer[0],
620
- fc: buffer[1],
621
- data: Array.from(buffer.subarray(2, buffer.length - 2)),
622
- buffer,
623
- };
624
- }
625
- }
626
- }
627
- encode(data) {
628
- const buffer = Buffer.alloc(data.data.length + 4);
629
- buffer.writeUInt8(data.unit, 0);
630
- buffer.writeUInt8(data.fc, 1);
631
- data.data.forEach((num, index) => {
632
- buffer.writeUInt8(num, 2 + index);
633
- });
634
- buffer.writeUInt16LE(crc(buffer.subarray(0, -2)), buffer.length - 2);
635
- return buffer;
636
- }
637
- destroy() {
638
- this.removeAllListeners();
639
- for (const removeAllListener of this._removeAllListeners) {
640
- removeAllListener();
641
- }
642
- clearTimeout(this._timerThreePointFive);
643
- }
644
- }
645
-
646
- const CHAR_CODE = {
647
- COLON: ':'.charCodeAt(0),
648
- CR: '\r'.charCodeAt(0),
649
- LF: '\n'.charCodeAt(0),
650
- };
651
- class AsciiApplicationLayer extends AbstractApplicationLayer {
652
- constructor(physicalLayer) {
653
- super();
654
- Object.defineProperty(this, "_status", {
655
- enumerable: true,
656
- configurable: true,
657
- writable: true,
658
- value: 'idle'
659
- });
660
- Object.defineProperty(this, "_frame", {
661
- enumerable: true,
662
- configurable: true,
663
- writable: true,
664
- value: []
665
- });
666
- Object.defineProperty(this, "_removeAllListeners", {
667
- enumerable: true,
668
- configurable: true,
669
- writable: true,
670
- value: []
671
- });
672
- const handleData = (data, response) => {
673
- data.forEach((value) => {
674
- switch (this._status) {
675
- case 'idle': {
676
- if (value === CHAR_CODE.COLON) {
677
- this._status = 'reception';
678
- this._frame = [];
679
- }
680
- break;
681
- }
682
- case 'reception': {
683
- if (value === CHAR_CODE.COLON) {
684
- this._frame = [];
685
- }
686
- else if (value === CHAR_CODE.CR) {
687
- this._status = 'waiting end';
688
- }
689
- else {
690
- this._frame.push(value);
691
- }
692
- break;
693
- }
694
- case 'waiting end': {
695
- if (value === CHAR_CODE.COLON) {
696
- this._status = 'reception';
697
- this._frame = [];
698
- }
699
- else {
700
- this._status = 'idle';
701
- if (value === CHAR_CODE.LF) {
702
- const frame = this.framing(Buffer.from(this._frame));
703
- if (frame) {
704
- this.emit('framing', frame, response);
705
- }
706
- }
707
- }
708
- break;
709
- }
710
- }
711
- });
712
- };
713
- physicalLayer.on('data', handleData);
714
- this._removeAllListeners.push(() => {
715
- physicalLayer.removeListener('data', handleData);
716
- });
717
- const handleClose = () => {
718
- this._status = 'reception';
719
- this._frame = [];
720
- };
721
- physicalLayer.on('close', handleClose);
722
- this._removeAllListeners.push(() => {
723
- physicalLayer.removeListener('close', handleClose);
724
- });
725
- }
726
- framing(_buffer) {
727
- if (_buffer.length >= 6 && _buffer.length % 2 === 0) {
728
- const frame = [];
729
- let num = '';
730
- for (const value of _buffer) {
731
- num += String.fromCharCode(value);
732
- if (num.length === 2) {
733
- frame.push(Number('0x' + num));
734
- num = '';
735
- }
736
- }
737
- const buffer = Buffer.from(frame);
738
- const lrcPassed = buffer[buffer.length - 1] === lrc(buffer.subarray(0, buffer.length - 1));
739
- if (lrcPassed) {
740
- return {
741
- unit: buffer[0],
742
- fc: buffer[1],
743
- data: Array.from(buffer.subarray(2, buffer.length - 1)),
744
- buffer: _buffer,
745
- };
746
- }
747
- }
748
- }
749
- encode(data) {
750
- const buffer = Buffer.alloc(data.data.length + 3);
751
- buffer.writeUInt8(data.unit, 0);
752
- buffer.writeUInt8(data.fc, 1);
753
- data.data.forEach((num, index) => {
754
- buffer.writeUInt8(num, 2 + index);
755
- });
756
- buffer.writeUInt8(lrc(buffer.subarray(0, -1)), buffer.length - 1);
757
- let frame = ':';
758
- for (const value of buffer) {
759
- frame += value.toString(16).toUpperCase().padStart(2, '0');
760
- }
761
- frame += '\r\n';
762
- return Buffer.from(frame);
763
- }
764
- destroy() {
765
- this.removeAllListeners();
766
- for (const removeAllListener of this._removeAllListeners) {
767
- removeAllListener();
768
- }
769
- }
770
- }
771
-
772
- class TcpApplicationLayer extends AbstractApplicationLayer {
773
- constructor(physicalLayer) {
774
- super();
775
- Object.defineProperty(this, "_transactionId", {
776
- enumerable: true,
777
- configurable: true,
778
- writable: true,
779
- value: 1
780
- });
781
- Object.defineProperty(this, "_removeAllListeners", {
782
- enumerable: true,
783
- configurable: true,
784
- writable: true,
785
- value: []
786
- });
787
- const handleData = (data, response) => {
788
- const frame = this.framing(data);
789
- if (frame) {
790
- this.emit('framing', frame, response);
791
- }
792
- };
793
- physicalLayer.on('data', handleData);
794
- this._removeAllListeners.push(() => {
795
- physicalLayer.removeListener('data', handleData);
796
- });
797
- }
798
- framing(buffer) {
799
- if (buffer.length >= 8) {
800
- if (buffer[2] === 0 && buffer[3] === 0 && buffer.readUInt16BE(4) === buffer.length - 6) {
801
- return {
802
- transaction: buffer.readUInt16BE(0),
803
- unit: buffer[6],
804
- fc: buffer[7],
805
- data: Array.from(buffer.subarray(8)),
806
- buffer,
807
- };
808
- }
809
- }
810
- }
811
- encode(data) {
812
- var _a;
813
- const buffer = Buffer.alloc(data.data.length + 8);
814
- buffer.writeUInt16BE((_a = data.transaction) !== null && _a !== void 0 ? _a : this._transactionId, 0);
815
- buffer.writeUInt16BE(0, 2);
816
- buffer.writeUInt16BE(data.data.length + 2, 4);
817
- buffer.writeUInt8(data.unit, 6);
818
- buffer.writeUInt8(data.fc, 7);
819
- data.data.forEach((num, index) => {
820
- buffer.writeUInt8(num, 8 + index);
821
- });
822
- this._transactionId = (this._transactionId + 1) % 256 || 1;
823
- return buffer;
824
- }
825
- destroy() {
826
- this.removeAllListeners();
827
- for (const removeAllListener of this._removeAllListeners) {
828
- removeAllListener();
829
- }
830
- }
831
- }
832
-
833
- /******************************************************************************
834
- Copyright (c) Microsoft Corporation.
835
-
836
- Permission to use, copy, modify, and/or distribute this software for any
837
- purpose with or without fee is hereby granted.
838
-
839
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
840
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
841
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
842
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
843
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
844
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
845
- PERFORMANCE OF THIS SOFTWARE.
846
- ***************************************************************************** */
847
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
848
-
849
-
850
- function __awaiter(thisArg, _arguments, P, generator) {
851
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
852
- return new (P || (P = Promise))(function (resolve, reject) {
853
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
854
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
855
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
856
- step((generator = generator.apply(thisArg, _arguments || [])).next());
857
- });
858
- }
859
-
860
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
861
- var e = new Error(message);
862
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
863
- };
864
-
865
- class ModbusMaster extends EventEmitter {
866
- get isOpen() {
867
- return this.physicalLayer.isOpen;
868
- }
869
- get destroyed() {
870
- return this.physicalLayer.destroyed;
871
- }
872
- constructor(applicationLayer, physicalLayer, timeout = 1000) {
873
- super();
874
- Object.defineProperty(this, "applicationLayer", {
875
- enumerable: true,
876
- configurable: true,
877
- writable: true,
878
- value: applicationLayer
879
- });
880
- Object.defineProperty(this, "physicalLayer", {
881
- enumerable: true,
882
- configurable: true,
883
- writable: true,
884
- value: physicalLayer
885
- });
886
- Object.defineProperty(this, "timeout", {
887
- enumerable: true,
888
- configurable: true,
889
- writable: true,
890
- value: timeout
891
- });
892
- Object.defineProperty(this, "_responses", {
893
- enumerable: true,
894
- configurable: true,
895
- writable: true,
896
- value: new Set()
897
- });
898
- Object.defineProperty(this, "writeFC1", {
899
- enumerable: true,
900
- configurable: true,
901
- writable: true,
902
- value: void 0
903
- });
904
- Object.defineProperty(this, "writeFC2", {
905
- enumerable: true,
906
- configurable: true,
907
- writable: true,
908
- value: void 0
909
- });
910
- Object.defineProperty(this, "writeFC3", {
911
- enumerable: true,
912
- configurable: true,
913
- writable: true,
914
- value: void 0
915
- });
916
- Object.defineProperty(this, "writeFC4", {
917
- enumerable: true,
918
- configurable: true,
919
- writable: true,
920
- value: void 0
921
- });
922
- Object.defineProperty(this, "writeFC5", {
923
- enumerable: true,
924
- configurable: true,
925
- writable: true,
926
- value: void 0
927
- });
928
- Object.defineProperty(this, "writeFC6", {
929
- enumerable: true,
930
- configurable: true,
931
- writable: true,
932
- value: void 0
933
- });
934
- Object.defineProperty(this, "writeFC15", {
935
- enumerable: true,
936
- configurable: true,
937
- writable: true,
938
- value: void 0
939
- });
940
- Object.defineProperty(this, "writeFC16", {
941
- enumerable: true,
942
- configurable: true,
943
- writable: true,
944
- value: void 0
945
- });
946
- Object.defineProperty(this, "handleFC17", {
947
- enumerable: true,
948
- configurable: true,
949
- writable: true,
950
- value: void 0
951
- });
952
- Object.defineProperty(this, "handleFC22", {
953
- enumerable: true,
954
- configurable: true,
955
- writable: true,
956
- value: void 0
957
- });
958
- Object.defineProperty(this, "handleFC23", {
959
- enumerable: true,
960
- configurable: true,
961
- writable: true,
962
- value: void 0
963
- });
964
- Object.defineProperty(this, "handleFC43_14", {
965
- enumerable: true,
966
- configurable: true,
967
- writable: true,
968
- value: void 0
969
- });
970
- this.writeFC1 = this.readCoils;
971
- this.writeFC2 = this.readDiscreteInputs;
972
- this.writeFC3 = this.readHoldingRegisters;
973
- this.writeFC4 = this.readInputRegisters;
974
- this.writeFC5 = this.writeSingleCoil;
975
- this.writeFC6 = this.writeSingleRegister;
976
- this.writeFC15 = this.writeMultipleCoils;
977
- this.writeFC16 = this.writeMultipleRegisters;
978
- this.handleFC17 = this.reportServerId;
979
- this.handleFC22 = this.maskWriteRegister;
980
- this.handleFC23 = this.readAndWriteMultipleRegisters;
981
- this.handleFC43_14 = this.readDeviceIdentification;
982
- applicationLayer.on('framing', (frame) => __awaiter(this, void 0, void 0, function* () {
983
- for (const response of this._responses) {
984
- response(frame);
985
- }
986
- }));
987
- physicalLayer.on('error', (error) => {
988
- this.emit('error', error);
989
- });
990
- physicalLayer.on('close', () => {
991
- this.emit('close');
992
- });
993
- }
994
- waitResponse(timeout, cb) {
995
- return new Promise((resolve, reject) => {
996
- const tid = setTimeout(() => {
997
- this._responses.delete(response);
998
- reject('Timeout');
999
- }, timeout);
1000
- const response = (frame) => {
1001
- const data = cb(frame);
1002
- if (typeof data !== 'undefined') {
1003
- clearTimeout(tid);
1004
- this._responses.delete(response);
1005
- resolve({
1006
- transaction: frame.transaction,
1007
- unit: frame.unit,
1008
- fc: frame.fc,
1009
- data,
1010
- buffer: frame.buffer,
1011
- });
1012
- }
1013
- };
1014
- this._responses.add(response);
1015
- });
1016
- }
1017
- writeFC1Or2(unit, fc, address, length, timeout) {
1018
- const bufferTx = Buffer.alloc(4);
1019
- bufferTx.writeUInt16BE(address, 0);
1020
- bufferTx.writeUInt16BE(length, 2);
1021
- this.physicalLayer.write(this.applicationLayer.encode({
1022
- unit,
1023
- fc,
1024
- data: Array.from(bufferTx),
1025
- }));
1026
- if (unit !== 0) {
1027
- return this.waitResponse(timeout, (frame) => {
1028
- const byteCount = Math.ceil(length / 8);
1029
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 1 + byteCount && frame.data[0] === byteCount) {
1030
- return Array.from({ length }).map((_, index) => (frame.data[1 + ~~(index / 8)] & (1 << index % 8)) > 0);
1031
- }
1032
- });
1033
- }
1034
- }
1035
- readCoils(unit, address, length, timeout = this.timeout) {
1036
- return this.writeFC1Or2(unit, 0x01, address, length, timeout);
1037
- }
1038
- readDiscreteInputs(unit, address, length, timeout = this.timeout) {
1039
- return this.writeFC1Or2(unit, 0x02, address, length, timeout);
1040
- }
1041
- writeFC3Or4(unit, fc, address, length, timeout) {
1042
- const bufferTx = Buffer.alloc(4);
1043
- bufferTx.writeUInt16BE(address, 0);
1044
- bufferTx.writeUInt16BE(length, 2);
1045
- this.physicalLayer.write(this.applicationLayer.encode({
1046
- unit,
1047
- fc,
1048
- data: Array.from(bufferTx),
1049
- }));
1050
- if (unit !== 0) {
1051
- return this.waitResponse(timeout, (frame) => {
1052
- const byteCount = length * 2;
1053
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 1 + byteCount && frame.data[0] === byteCount) {
1054
- const bufferRx = Buffer.from(frame.data.slice(1));
1055
- return Array.from({ length }).map((_, index) => bufferRx.readUInt16BE(index * 2));
1056
- }
1057
- });
1058
- }
1059
- }
1060
- readHoldingRegisters(unit, address, length, timeout = this.timeout) {
1061
- return this.writeFC3Or4(unit, 0x03, address, length, timeout);
1062
- }
1063
- readInputRegisters(unit, address, length, timeout = this.timeout) {
1064
- return this.writeFC3Or4(unit, 0x04, address, length, timeout);
1065
- }
1066
- writeSingleCoil(unit, address, value, timeout = this.timeout) {
1067
- const fc = 0x05;
1068
- const bufferTx = Buffer.alloc(4);
1069
- bufferTx.writeUInt16BE(address, 0);
1070
- bufferTx.writeUInt16BE(value ? 0xff00 : 0x0000, 2);
1071
- this.physicalLayer.write(this.applicationLayer.encode({
1072
- unit,
1073
- fc,
1074
- data: Array.from(bufferTx),
1075
- }));
1076
- if (unit !== 0) {
1077
- return this.waitResponse(timeout, (frame) => {
1078
- if (frame.unit === unit &&
1079
- frame.fc === fc &&
1080
- frame.data.length === bufferTx.length &&
1081
- frame.data.every((v, i) => v === bufferTx[i])) {
1082
- return value;
1083
- }
1084
- });
1085
- }
1086
- }
1087
- writeSingleRegister(unit, address, value, timeout = this.timeout) {
1088
- const fc = 0x06;
1089
- const bufferTx = Buffer.alloc(4);
1090
- bufferTx.writeUInt16BE(address, 0);
1091
- bufferTx.writeUInt16BE(value, 2);
1092
- this.physicalLayer.write(this.applicationLayer.encode({
1093
- unit,
1094
- fc,
1095
- data: Array.from(bufferTx),
1096
- }));
1097
- if (unit !== 0) {
1098
- return this.waitResponse(timeout, (frame) => {
1099
- if (frame.unit === unit &&
1100
- frame.fc === fc &&
1101
- frame.data.length === bufferTx.length &&
1102
- frame.data.every((v, i) => v === bufferTx[i])) {
1103
- return value;
1104
- }
1105
- });
1106
- }
1107
- }
1108
- writeMultipleCoils(unit, address, value, timeout = this.timeout) {
1109
- const fc = 0x0f;
1110
- const byteCount = Math.ceil(value.length / 8);
1111
- const bufferTx = Buffer.alloc(5 + byteCount);
1112
- bufferTx.writeUInt16BE(address, 0);
1113
- bufferTx.writeUInt16BE(value.length, 2);
1114
- bufferTx.writeUInt8(byteCount, 4);
1115
- value.forEach((v, i) => {
1116
- if (v) {
1117
- bufferTx[5 + ~~(i / 8)] |= 1 << i % 8;
1118
- }
1119
- });
1120
- this.physicalLayer.write(this.applicationLayer.encode({
1121
- unit,
1122
- fc,
1123
- data: Array.from(bufferTx),
1124
- }));
1125
- if (unit !== 0) {
1126
- return this.waitResponse(timeout, (frame) => {
1127
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 4 && frame.data.every((v, i) => v === bufferTx[i])) {
1128
- return value;
1129
- }
1130
- });
1131
- }
1132
- }
1133
- writeMultipleRegisters(unit, address, value, timeout = this.timeout) {
1134
- const fc = 0x10;
1135
- const byteCount = value.length * 2;
1136
- const bufferTx = Buffer.alloc(5 + byteCount);
1137
- bufferTx.writeUInt16BE(address, 0);
1138
- bufferTx.writeUInt16BE(value.length, 2);
1139
- bufferTx.writeUInt8(byteCount, 4);
1140
- value.forEach((v, i) => {
1141
- bufferTx.writeUInt16BE(v, 5 + i * 2);
1142
- });
1143
- this.physicalLayer.write(this.applicationLayer.encode({
1144
- unit,
1145
- fc,
1146
- data: Array.from(bufferTx),
1147
- }));
1148
- if (unit !== 0) {
1149
- return this.waitResponse(timeout, (frame) => {
1150
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 4 && frame.data.every((v, i) => v === bufferTx[i])) {
1151
- return value;
1152
- }
1153
- });
1154
- }
1155
- }
1156
- reportServerId(unit, timeout = this.timeout) {
1157
- const fc = 0x11;
1158
- this.physicalLayer.write(this.applicationLayer.encode({
1159
- unit,
1160
- fc,
1161
- data: [],
1162
- }));
1163
- if (unit !== 0) {
1164
- return this.waitResponse(timeout, (frame) => {
1165
- if (frame.unit === unit && frame.fc === fc && frame.data.length >= 3) {
1166
- const byteCount = frame.data[0];
1167
- if (frame.data.length - 1 === byteCount) {
1168
- return {
1169
- serverId: frame.data[1],
1170
- runIndicatorStatus: frame.data[2] === 0xff,
1171
- additionalData: frame.data.slice(3),
1172
- };
1173
- }
1174
- }
1175
- });
1176
- }
1177
- }
1178
- maskWriteRegister(unit, address, andMask, orMask, timeout = this.timeout) {
1179
- const fc = 0x16;
1180
- const bufferTx = Buffer.alloc(6);
1181
- bufferTx.writeUInt16BE(address, 0);
1182
- bufferTx.writeUInt16BE(andMask, 2);
1183
- bufferTx.writeUInt16BE(orMask, 4);
1184
- this.physicalLayer.write(this.applicationLayer.encode({
1185
- unit,
1186
- fc,
1187
- data: Array.from(bufferTx),
1188
- }));
1189
- if (unit !== 0) {
1190
- return this.waitResponse(timeout, (frame) => {
1191
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 6 && frame.data.every((v, i) => v === bufferTx[i])) {
1192
- return { andMask, orMask };
1193
- }
1194
- });
1195
- }
1196
- }
1197
- readAndWriteMultipleRegisters(unit, read, write, timeout = this.timeout) {
1198
- const fc = 0x17;
1199
- const byteCount = write.value.length * 2;
1200
- const bufferTx = Buffer.alloc(9 + byteCount);
1201
- bufferTx.writeUInt16BE(read.address, 0);
1202
- bufferTx.writeUInt16BE(read.length, 2);
1203
- bufferTx.writeUInt16BE(write.address, 4);
1204
- bufferTx.writeUInt16BE(write.value.length, 6);
1205
- bufferTx.writeUInt8(byteCount, 8);
1206
- write.value.forEach((v, i) => {
1207
- bufferTx.writeUInt16BE(v, 9 + i * 2);
1208
- });
1209
- this.physicalLayer.write(this.applicationLayer.encode({
1210
- unit,
1211
- fc,
1212
- data: Array.from(bufferTx),
1213
- }));
1214
- if (unit !== 0) {
1215
- return this.waitResponse(timeout, (frame) => {
1216
- if (frame.unit === unit && frame.fc === fc && frame.data.length === 1 + byteCount && frame.data[0] === byteCount) {
1217
- const bufferRx = Buffer.from(frame.data.slice(1));
1218
- return Array.from({ length: read.length }).map((_, index) => bufferRx.readUInt16BE(index * 2));
1219
- }
1220
- });
1221
- }
1222
- }
1223
- readDeviceIdentification(unit, readDeviceIDCode, objectId, timeout = this.timeout) {
1224
- const fc = 0x2b;
1225
- this.physicalLayer.write(this.applicationLayer.encode({
1226
- unit,
1227
- fc,
1228
- data: [0x0e, readDeviceIDCode, objectId],
1229
- }));
1230
- if (unit !== 0) {
1231
- return this.waitResponse(timeout, (frame) => {
1232
- if (frame.unit === unit &&
1233
- frame.fc === fc &&
1234
- frame.data.length >= 6 &&
1235
- frame.data[0] === 0x0e &&
1236
- frame.data[1] === readDeviceIDCode) {
1237
- const conformityLevel = frame.data[2];
1238
- const moreFollows = frame.data[3] === 0xff;
1239
- const nextObjectId = frame.data[4];
1240
- const objectLength = frame.data[5];
1241
- const objects = [];
1242
- let object = [];
1243
- for (const v of frame.data.slice(6)) {
1244
- switch (object.length) {
1245
- case 0:
1246
- case 1: {
1247
- object.push(v);
1248
- break;
1249
- }
1250
- case 2: {
1251
- object.push([v]);
1252
- break;
1253
- }
1254
- case 3: {
1255
- object[2].push(v);
1256
- break;
1257
- }
1258
- }
1259
- if (object.length === 3 && object[1] === object[2].length) {
1260
- objects.push({ id: object[0], value: Buffer.from(object[2]).toString() });
1261
- object = [];
1262
- }
1263
- }
1264
- if (objects.length === objectLength) {
1265
- return {
1266
- readDeviceIDCode,
1267
- conformityLevel,
1268
- moreFollows,
1269
- nextObjectId,
1270
- objects,
1271
- };
1272
- }
1273
- }
1274
- });
1275
- }
1276
- }
1277
- open(...args) {
1278
- return this.physicalLayer.open(...args);
1279
- }
1280
- close() {
1281
- return this.physicalLayer.close();
1282
- }
1283
- destroy() {
1284
- this.removeAllListeners();
1285
- this.applicationLayer.destroy();
1286
- return this.physicalLayer.destroy();
1287
- }
1288
- }
1289
-
1290
- class ModbusSlave extends EventEmitter {
1291
- get isOpen() {
1292
- return this.physicalLayer.isOpen;
1293
- }
1294
- get destroyed() {
1295
- return this.physicalLayer.destroyed;
1296
- }
1297
- constructor(model, applicationLayer, physicalLayer) {
1298
- super();
1299
- Object.defineProperty(this, "model", {
1300
- enumerable: true,
1301
- configurable: true,
1302
- writable: true,
1303
- value: model
1304
- });
1305
- Object.defineProperty(this, "applicationLayer", {
1306
- enumerable: true,
1307
- configurable: true,
1308
- writable: true,
1309
- value: applicationLayer
1310
- });
1311
- Object.defineProperty(this, "physicalLayer", {
1312
- enumerable: true,
1313
- configurable: true,
1314
- writable: true,
1315
- value: physicalLayer
1316
- });
1317
- Object.defineProperty(this, "unit", {
1318
- enumerable: true,
1319
- configurable: true,
1320
- writable: true,
1321
- value: 1
1322
- });
1323
- if (typeof model.unit !== 'undefined') {
1324
- this.unit = model.unit;
1325
- }
1326
- applicationLayer.on('framing', (frame, _response) => __awaiter(this, void 0, void 0, function* () {
1327
- if (!(frame.unit === 0x00 || frame.unit === this.unit)) {
1328
- return;
1329
- }
1330
- const response = frame.unit === 0x00 ? () => Promise.resolve() : _response;
1331
- if (model.interceptor) {
1332
- try {
1333
- const data = yield model.interceptor(frame.fc, frame.data);
1334
- if (data) {
1335
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data })));
1336
- return;
1337
- }
1338
- }
1339
- catch (error) {
1340
- this.responseError(frame, response, error);
1341
- return;
1342
- }
1343
- }
1344
- switch (frame.fc) {
1345
- case 0x01: {
1346
- this.handleFC1(frame, response);
1347
- break;
1348
- }
1349
- case 0x02: {
1350
- this.handleFC2(frame, response);
1351
- break;
1352
- }
1353
- case 0x03: {
1354
- this.handleFC3(frame, response);
1355
- break;
1356
- }
1357
- case 0x04: {
1358
- this.handleFC4(frame, response);
1359
- break;
1360
- }
1361
- case 0x05: {
1362
- this.handleFC5(frame, response);
1363
- break;
1364
- }
1365
- case 0x06: {
1366
- this.handleFC6(frame, response);
1367
- break;
1368
- }
1369
- case 0x0f: {
1370
- this.handleFC15(frame, response);
1371
- break;
1372
- }
1373
- case 0x10: {
1374
- this.handleFC16(frame, response);
1375
- break;
1376
- }
1377
- case 0x11: {
1378
- this.handleFC17(frame, response);
1379
- break;
1380
- }
1381
- case 0x16: {
1382
- this.handleFC22(frame, response);
1383
- break;
1384
- }
1385
- case 0x17: {
1386
- this.handleFC23(frame, response);
1387
- break;
1388
- }
1389
- case 0x2b: {
1390
- this.handleFC43_14(frame, response);
1391
- break;
1392
- }
1393
- default: {
1394
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1395
- break;
1396
- }
1397
- }
1398
- }));
1399
- physicalLayer.on('error', (error) => {
1400
- this.emit('error', error);
1401
- });
1402
- physicalLayer.on('close', () => {
1403
- this.emit('close');
1404
- });
1405
- }
1406
- handleFC1(frame, response) {
1407
- var _a, _b;
1408
- if (frame.data.length === 4) {
1409
- if (this.model.readCoils) {
1410
- const bufferRx = Buffer.from(frame.data);
1411
- const address = bufferRx.readUInt16BE(0);
1412
- const length = bufferRx.readUInt16BE(2);
1413
- if (length >= 0x0001 && length <= 0x07d0) {
1414
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).coils)) {
1415
- Promise.resolve(this.model.readCoils(address, length))
1416
- .then((coils) => {
1417
- const bufferTx = Buffer.alloc(Math.ceil(length / 8));
1418
- coils.forEach((coil, index) => {
1419
- if (coil) {
1420
- bufferTx[~~(index / 8)] |= 1 << index % 8;
1421
- }
1422
- });
1423
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [bufferTx.length].concat(Array.from(bufferTx)) })));
1424
- })
1425
- .catch((error) => {
1426
- this.responseError(frame, response, error);
1427
- });
1428
- }
1429
- else {
1430
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1431
- }
1432
- }
1433
- else {
1434
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1435
- }
1436
- }
1437
- else {
1438
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1439
- }
1440
- }
1441
- }
1442
- handleFC2(frame, response) {
1443
- var _a, _b;
1444
- if (frame.data.length === 4) {
1445
- if (this.model.readDiscreteInputs) {
1446
- const bufferRx = Buffer.from(frame.data);
1447
- const address = bufferRx.readUInt16BE(0);
1448
- const length = bufferRx.readUInt16BE(2);
1449
- if (length >= 0x0001 && length <= 0x07d0) {
1450
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).discreteInputs)) {
1451
- Promise.resolve(this.model.readDiscreteInputs(address, length))
1452
- .then((discreteInputs) => {
1453
- const bufferTx = Buffer.alloc(Math.ceil(length / 8));
1454
- discreteInputs.forEach((discreteInput, index) => {
1455
- if (discreteInput) {
1456
- bufferTx[~~(index / 8)] |= 1 << index % 8;
1457
- }
1458
- });
1459
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [bufferTx.length].concat(Array.from(bufferTx)) })));
1460
- })
1461
- .catch((error) => {
1462
- this.responseError(frame, response, error);
1463
- });
1464
- }
1465
- else {
1466
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1467
- }
1468
- }
1469
- else {
1470
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1471
- }
1472
- }
1473
- else {
1474
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1475
- }
1476
- }
1477
- }
1478
- handleFC3(frame, response) {
1479
- var _a, _b;
1480
- if (frame.data.length === 4) {
1481
- if (this.model.readHoldingRegisters) {
1482
- const bufferRx = Buffer.from(frame.data);
1483
- const address = bufferRx.readUInt16BE(0);
1484
- const length = bufferRx.readUInt16BE(2);
1485
- if (length >= 0x0001 && length <= 0x007d) {
1486
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).holdingRegisters)) {
1487
- Promise.resolve(this.model.readHoldingRegisters(address, length))
1488
- .then((registers) => {
1489
- const bufferTx = Buffer.alloc(length * 2);
1490
- registers.forEach((register, index) => {
1491
- bufferTx.writeUInt16BE(register, index * 2);
1492
- });
1493
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [bufferTx.length].concat(Array.from(bufferTx)) })));
1494
- })
1495
- .catch((error) => {
1496
- this.responseError(frame, response, error);
1497
- });
1498
- }
1499
- else {
1500
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1501
- }
1502
- }
1503
- else {
1504
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1505
- }
1506
- }
1507
- else {
1508
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1509
- }
1510
- }
1511
- }
1512
- handleFC4(frame, response) {
1513
- var _a, _b;
1514
- if (frame.data.length === 4) {
1515
- if (this.model.readInputRegisters) {
1516
- const bufferRx = Buffer.from(frame.data);
1517
- const address = bufferRx.readUInt16BE(0);
1518
- const length = bufferRx.readUInt16BE(2);
1519
- if (length >= 0x0001 && length <= 0x007d) {
1520
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).inputRegisters)) {
1521
- Promise.resolve(this.model.readInputRegisters(address, length))
1522
- .then((registers) => {
1523
- const bufferTx = Buffer.alloc(length * 2);
1524
- registers.forEach((register, index) => {
1525
- bufferTx.writeUInt16BE(register, index * 2);
1526
- });
1527
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [bufferTx.length].concat(Array.from(bufferTx)) })));
1528
- })
1529
- .catch((error) => {
1530
- this.responseError(frame, response, error);
1531
- });
1532
- }
1533
- else {
1534
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1535
- }
1536
- }
1537
- else {
1538
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1539
- }
1540
- }
1541
- else {
1542
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1543
- }
1544
- }
1545
- }
1546
- handleFC5(frame, response) {
1547
- var _a, _b;
1548
- if (frame.data.length === 4) {
1549
- if (this.model.writeSingleCoil) {
1550
- const bufferRx = Buffer.from(frame.data);
1551
- const address = bufferRx.readUInt16BE(0);
1552
- const value = bufferRx.readUInt16BE(2);
1553
- if (value === 0x0000 || value === 0xff00) {
1554
- if (checkRange(address, (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).coils)) {
1555
- Promise.resolve(this.model.writeSingleCoil(address, value === 0xff00))
1556
- .then(() => {
1557
- response(this.applicationLayer.encode(frame));
1558
- })
1559
- .catch((error) => {
1560
- this.responseError(frame, response, error);
1561
- });
1562
- }
1563
- else {
1564
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1565
- }
1566
- }
1567
- else {
1568
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1569
- }
1570
- }
1571
- else {
1572
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1573
- }
1574
- }
1575
- }
1576
- handleFC6(frame, response) {
1577
- var _a, _b;
1578
- if (frame.data.length === 4) {
1579
- if (this.model.writeSingleRegister) {
1580
- const bufferRx = Buffer.from(frame.data);
1581
- const address = bufferRx.readUInt16BE(0);
1582
- const value = bufferRx.readUInt16BE(2);
1583
- if (value >= 0x0000 && value <= 0xffff) {
1584
- if (checkRange(address, (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).holdingRegisters)) {
1585
- Promise.resolve(this.model.writeSingleRegister(address, value))
1586
- .then(() => {
1587
- response(this.applicationLayer.encode(frame));
1588
- })
1589
- .catch((error) => {
1590
- this.responseError(frame, response, error);
1591
- });
1592
- }
1593
- else {
1594
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1595
- }
1596
- }
1597
- else {
1598
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1599
- }
1600
- }
1601
- else {
1602
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1603
- }
1604
- }
1605
- }
1606
- handleFC15(frame, response) {
1607
- var _a, _b;
1608
- if (frame.data.length > 5 && frame.data.length === 5 + frame.data[4]) {
1609
- if (this.model.writeMultipleCoils || this.model.writeSingleCoil) {
1610
- const bufferRx = Buffer.from(frame.data);
1611
- const address = bufferRx.readUInt16BE(0);
1612
- const length = bufferRx.readUInt16BE(2);
1613
- const byteCount = bufferRx[4];
1614
- if (length >= 0x0001 && length <= 0x07b0 && byteCount === Math.ceil(length / 8)) {
1615
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).coils)) {
1616
- const value = Array.from({ length }).map((_, index) => (bufferRx[5 + ~~(index / 8)] & (1 << index % 8)) > 0);
1617
- Promise.resolve(this.model.writeMultipleCoils
1618
- ? this.model.writeMultipleCoils(address, value)
1619
- : Promise.all(value.map((v, i) => this.model.writeSingleCoil(address + i, v))))
1620
- .then(() => {
1621
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: Array.from(bufferRx).slice(0, 4) })));
1622
- })
1623
- .catch((error) => {
1624
- this.responseError(frame, response, error);
1625
- });
1626
- }
1627
- else {
1628
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1629
- }
1630
- }
1631
- else {
1632
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1633
- }
1634
- }
1635
- else {
1636
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1637
- }
1638
- }
1639
- }
1640
- handleFC16(frame, response) {
1641
- var _a, _b;
1642
- if (frame.data.length > 5 && frame.data.length === 5 + frame.data[4]) {
1643
- if (this.model.writeMultipleRegisters || this.model.writeSingleRegister) {
1644
- const bufferRx = Buffer.from(frame.data);
1645
- const address = bufferRx.readUInt16BE(0);
1646
- const length = bufferRx.readUInt16BE(2);
1647
- const byteCount = bufferRx[4];
1648
- if (length >= 0x0001 && length <= 0x007b && byteCount === length * 2) {
1649
- if (checkRange([address, address + length], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).holdingRegisters)) {
1650
- const value = Array.from({ length }).map((_, index) => bufferRx.readUInt16BE(5 + index * 2));
1651
- Promise.resolve(this.model.writeMultipleRegisters
1652
- ? this.model.writeMultipleRegisters(address, value)
1653
- : Promise.all(value.map((v, i) => this.model.writeSingleRegister(address + i, v))))
1654
- .then(() => {
1655
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: Array.from(bufferRx).slice(0, 4) })));
1656
- })
1657
- .catch((error) => {
1658
- this.responseError(frame, response, error);
1659
- });
1660
- }
1661
- else {
1662
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1663
- }
1664
- }
1665
- else {
1666
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1667
- }
1668
- }
1669
- else {
1670
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1671
- }
1672
- }
1673
- }
1674
- handleFC17(frame, response) {
1675
- if (frame.data.length === 0) {
1676
- if (this.model.reportServerId) {
1677
- Promise.resolve(this.model.reportServerId())
1678
- .then(({ serverId = this.unit, runIndicatorStatus = true, additionalData = [] }) => {
1679
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [2 + additionalData.length, serverId, runIndicatorStatus ? 0xff : 0x00].concat(additionalData) })));
1680
- })
1681
- .catch((error) => {
1682
- this.responseError(frame, response, error);
1683
- });
1684
- }
1685
- else {
1686
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1687
- }
1688
- }
1689
- }
1690
- handleFC22(frame, response) {
1691
- var _a, _b;
1692
- if (frame.data.length === 6) {
1693
- if (this.model.maskWriteRegister || (this.model.readHoldingRegisters && this.model.writeSingleRegister)) {
1694
- const bufferRx = Buffer.from(frame.data);
1695
- const address = bufferRx.readUInt16BE(0);
1696
- const andMask = bufferRx.readUInt16BE(2);
1697
- const orMask = bufferRx.readUInt16BE(4);
1698
- if (checkRange(address, (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).holdingRegisters)) {
1699
- Promise.resolve(this.model.maskWriteRegister
1700
- ? this.model.maskWriteRegister(address, andMask, orMask)
1701
- : Promise.resolve(this.model.readHoldingRegisters(address, 1)).then(([value]) => {
1702
- return Promise.resolve(this.model.writeSingleRegister(address, (value & andMask) | (orMask & (~andMask & 0xff))));
1703
- }))
1704
- .then(() => {
1705
- response(this.applicationLayer.encode(frame));
1706
- })
1707
- .catch((error) => {
1708
- this.responseError(frame, response, error);
1709
- });
1710
- }
1711
- else {
1712
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1713
- }
1714
- }
1715
- else {
1716
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1717
- }
1718
- }
1719
- }
1720
- handleFC23(frame, response) {
1721
- var _a, _b;
1722
- if (frame.data.length > 9 && frame.data.length === 9 + frame.data[8]) {
1723
- if (this.model.readHoldingRegisters && (this.model.writeMultipleRegisters || this.model.writeSingleRegister)) {
1724
- const bufferRx = Buffer.from(frame.data);
1725
- const address = {
1726
- read: bufferRx.readUInt16BE(0),
1727
- write: bufferRx.readUInt16BE(4),
1728
- };
1729
- const length = {
1730
- read: bufferRx.readUInt16BE(2),
1731
- write: bufferRx.readUInt16BE(6),
1732
- };
1733
- const byteCount = bufferRx[8];
1734
- if (length.read >= 0x0001 &&
1735
- length.read <= 0x007d &&
1736
- length.write >= 0x0001 &&
1737
- length.write <= 0x0079 &&
1738
- byteCount === length.write * 2) {
1739
- if (checkRange([address.read, address.read + length.read, address.write, address.write + length.write], (_b = (_a = this.model).getAddressRange) === null || _b === void 0 ? void 0 : _b.call(_a).holdingRegisters)) {
1740
- const value = Array.from({ length: length.write }).map((_, index) => bufferRx.readUInt16BE(9 + index * 2));
1741
- Promise.resolve(this.model.writeMultipleRegisters
1742
- ? this.model.writeMultipleRegisters(address.write, value)
1743
- : Promise.all(value.map((v, i) => this.model.writeSingleRegister(address.write + i, v))))
1744
- .then(() => Promise.resolve(this.model.readHoldingRegisters(address.read, length.read)))
1745
- .then((registers) => {
1746
- const bufferTx = Buffer.alloc(length.read * 2);
1747
- registers.forEach((register, index) => {
1748
- bufferTx.writeUInt16BE(register, index * 2);
1749
- });
1750
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [bufferTx.length].concat(Array.from(bufferTx)) })));
1751
- })
1752
- .catch((error) => {
1753
- this.responseError(frame, response, error);
1754
- });
1755
- }
1756
- else {
1757
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1758
- }
1759
- }
1760
- else {
1761
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1762
- }
1763
- }
1764
- else {
1765
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1766
- }
1767
- }
1768
- }
1769
- handleFC43_14(frame, response) {
1770
- if (frame.data.length === 3) {
1771
- if (frame.data[0] === 0x0e && this.model.readDeviceIdentification) {
1772
- const readDeviceIDCode = frame.data[1];
1773
- let objectID = frame.data[2];
1774
- switch (readDeviceIDCode) {
1775
- case 0x01: {
1776
- if (objectID > 0x02 || (objectID > 0x06 && objectID < 0x80)) {
1777
- objectID = 0x00;
1778
- }
1779
- break;
1780
- }
1781
- case 0x02: {
1782
- if (objectID >= 0x80 || (objectID > 0x06 && objectID < 0x80)) {
1783
- objectID = 0x00;
1784
- }
1785
- break;
1786
- }
1787
- case 0x03: {
1788
- if (objectID > 0x06 && objectID < 0x80) {
1789
- objectID = 0x00;
1790
- }
1791
- break;
1792
- }
1793
- case 0x04: {
1794
- if (objectID > 0x06 && objectID < 0x80) {
1795
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1796
- return;
1797
- }
1798
- break;
1799
- }
1800
- default: {
1801
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_VALUE));
1802
- return;
1803
- }
1804
- }
1805
- Promise.resolve(this.model.readDeviceIdentification())
1806
- .then((identification) => {
1807
- const objects = new Map([
1808
- [0x00, 'null'],
1809
- [0x01, 'null'],
1810
- [0x02, 'null'],
1811
- ]);
1812
- for (const [key, value] of Object.entries(identification)) {
1813
- const id = parseInt(key);
1814
- if (!isNaN(id) && id >= 0 && id <= 255) {
1815
- objects.set(id, value);
1816
- }
1817
- }
1818
- if (!objects.has(objectID)) {
1819
- if (readDeviceIDCode === 0x04) {
1820
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_DATA_ADDRESS));
1821
- return;
1822
- }
1823
- objectID = 0x00;
1824
- }
1825
- const ids = [];
1826
- let totalLength = 10;
1827
- let lastID = 0;
1828
- let conformityLevel = 0x81;
1829
- for (const [id, value] of objects.entries()) {
1830
- if (id < 0x00 || (id >= 0x07 && id <= 0x7f) || id > 0xff) {
1831
- this.responseError(frame, response, getErrorByCode(ErrorCode.SERVER_DEVICE_FAILURE));
1832
- return;
1833
- }
1834
- if (id > 0x02) {
1835
- conformityLevel = 0x82;
1836
- }
1837
- if (id > 0x80) {
1838
- conformityLevel = 0x83;
1839
- }
1840
- if (objectID > id) {
1841
- continue;
1842
- }
1843
- if (value.length > 245) {
1844
- this.responseError(frame, response, getErrorByCode(ErrorCode.SERVER_DEVICE_FAILURE));
1845
- return;
1846
- }
1847
- if (lastID !== 0) {
1848
- continue;
1849
- }
1850
- if (value.length + 2 > 253 - totalLength) {
1851
- if (lastID === 0) {
1852
- lastID = id;
1853
- }
1854
- }
1855
- else {
1856
- totalLength += value.length + 2;
1857
- ids.push(id);
1858
- if (readDeviceIDCode === 0x04) {
1859
- break;
1860
- }
1861
- }
1862
- }
1863
- ids.sort((a, b) => a - b);
1864
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { data: [0x0e, readDeviceIDCode, conformityLevel, lastID === 0 ? 0x00 : 0xff, lastID, ids.length].concat(ids
1865
- .map((id) => {
1866
- const value = objects.get(id);
1867
- return [id, value.length].concat(Array.from(Buffer.from(value)));
1868
- })
1869
- .flat()) })));
1870
- })
1871
- .catch((error) => {
1872
- this.responseError(frame, response, error);
1873
- });
1874
- }
1875
- else {
1876
- this.responseError(frame, response, getErrorByCode(ErrorCode.ILLEGAL_FUNCTION));
1877
- }
1878
- }
1879
- }
1880
- responseError(frame, response, error) {
1881
- response(this.applicationLayer.encode(Object.assign(Object.assign({}, frame), { fc: frame.fc | 0x80, data: [getCodeByError(error)] })));
1882
- }
1883
- open(...args) {
1884
- return this.physicalLayer.open(...args);
1885
- }
1886
- close() {
1887
- return this.physicalLayer.close();
1888
- }
1889
- destroy() {
1890
- this.removeAllListeners();
1891
- this.applicationLayer.destroy();
1892
- return this.physicalLayer.destroy();
1893
- }
1894
- }
1895
-
1896
- export { AbstractApplicationLayer, AbstractPhysicalLayer, AsciiApplicationLayer, ErrorCode, ModbusMaster, ModbusSlave, RtuApplicationLayer, SerialPhysicalLayer, TcpApplicationLayer, TcpClientPhysicalLayer, TcpServerPhysicalLayer, UdpPhysicalLayer, getCodeByError, getErrorByCode };