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