-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
index.js
1208 lines (1048 loc) · 37.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/**
* Copyright (c) 2015-2017, Yaacov Zamir <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Add bit operation functions to Buffer
*/
require("./utils/buffer_bit")();
const { SerialPort } = require("serialport");
const crc16 = require("./utils/crc16");
const modbusSerialDebug = require("debug")("modbus-serial");
const events = require("events");
const EventEmitter = events.EventEmitter || events;
const PORT_NOT_OPEN_MESSAGE = "Port Not Open";
const PORT_NOT_OPEN_ERRNO = "ECONNREFUSED";
const BAD_ADDRESS_MESSAGE = "Bad Client Address";
const BAD_ADDRESS_ERRNO = "ECONNREFUSED";
const TRANSACTION_TIMED_OUT_MESSAGE = "Timed out";
const TRANSACTION_TIMED_OUT_ERRNO = "ETIMEDOUT";
const modbusErrorMessages = [
"Unknown error",
"Illegal function (device does not support this read/write function)",
"Illegal data address (register not supported by device)",
"Illegal data value (value cannot be written to this register)",
"Slave device failure (device reports internal error)",
"Acknowledge (requested data will be available later)",
"Slave device busy (retry request again later)",
"Negative acknowledge (slave device cannot perform programming functions)",
"Memory parity error (slave device detected a parity error in memory)",
"Unknown error",
"Gateway path unavailable (misconfigured gateway)",
"Gateway target device failed to respond (retry request again later)"
];
const PortNotOpenError = function() {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = PORT_NOT_OPEN_MESSAGE;
this.errno = PORT_NOT_OPEN_ERRNO;
};
const BadAddressError = function() {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = BAD_ADDRESS_MESSAGE;
this.errno = BAD_ADDRESS_ERRNO;
};
const TransactionTimedOutError = function() {
this.name = this.constructor.name;
this.message = TRANSACTION_TIMED_OUT_MESSAGE;
this.errno = TRANSACTION_TIMED_OUT_ERRNO;
};
const SerialPortError = function() {
this.name = this.constructor.name;
this.message = null;
this.errno = "ECONNREFUSED";
};
/**
* @fileoverview ModbusRTU module, exports the ModbusRTU class.
* this class makes ModbusRTU calls fun and easy.
*
* Modbus is a serial communications protocol, first used in 1979.
* Modbus is simple and robust, openly published, royalty-free and
* easy to deploy and maintain.
*/
/**
* Parse the data for a Modbus -
* Read Coils (FC=02, 01)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC2(data, next) {
const length = data.readUInt8(2);
const contents = [];
for (let i = 0; i < length; i++) {
let reg = data[i + 3];
for (let j = 0; j < 8; j++) {
contents.push((reg & 1) === 1);
reg = reg >> 1;
}
}
if (next)
next(null, { "data": contents, "buffer": data.slice(3, 3 + length) });
}
/**
* Parse the data for a Modbus -
* Read Input Registers (FC=04, 03)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC3or4(data, next) {
const length = data.readUInt8(2);
const contents = [];
for (let i = 0; i < length; i += 2) {
const reg = data.readUInt16BE(i + 3);
contents.push(reg);
}
if (next)
next(null, { "data": contents, "buffer": data.slice(3, 3 + length) });
}
/**
* Parse the data for a Modbus (Enron) -
* Read Registers (FC=04, 03)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC3or4Enron(data, next) {
const length = data.readUInt8(2);
const contents = [];
for (let i = 0; i < length; i += 4) {
const reg = data.readUInt32BE(i + 3);
contents.push(reg);
}
if (next)
next(null, { "data": contents, "buffer": data.slice(3, 3 + length) });
}
/**
* Parse the data for a Modbus -
* Force Single Coil (FC=05)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC5(data, next) {
const dataAddress = data.readUInt16BE(2);
const state = data.readUInt16BE(4);
if (next)
next(null, { "address": dataAddress, "state": (state === 0xff00) });
}
/**
* Parse the data for a Modbus -
* Preset Single Registers (FC=06)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC6(data, next) {
const dataAddress = data.readUInt16BE(2);
const value = data.readUInt16BE(4);
if (next)
next(null, { "address": dataAddress, "value": value });
}
/**
* Parse the data for a Modbus (Enron) -
* Preset Single Registers (FC=06)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC6Enron(data, next) {
const dataAddress = data.readUInt16BE(2);
const value = data.readUInt32BE(4);
if (next)
next(null, { "address": dataAddress, "value": value });
}
/**
* Parse the data for a Modbus -
* Preset Multiple Registers (FC=15, 16)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC16(data, next) {
const dataAddress = data.readUInt16BE(2);
const length = data.readUInt16BE(4);
if (next)
next(null, { "address": dataAddress, "length": length });
}
/**
* Parse the data for a Modbus -
* Report server ID (FC=17)
*
* @param {Buffer} data the data buffer to parse.
* @param {Function} next the function to call next.
*/
function _readFC17(data, next) {
const length = parseInt(data.readUInt8(2));
const serverId = parseInt(data.readUInt8(3));
const running = data.readUInt8(4) === 0xFF;
let additionalData;
if (length > 2) {
additionalData = Buffer.alloc(length - 2);
// copy additional data
data.copy(additionalData, 0, 5, data.length - 2);
} else {
additionalData = Buffer.alloc(0);
}
if (next)
next(null, { serverId: serverId, running: running, additionalData: additionalData });
}
/**
* Parse the data fro Modbus -
* Read File Records
*
* @param {Buffer4} buffer
* @param {Function} next
*/
function _readFC20(data, next) {
const fileRespLength = parseInt(data.readUInt8(2));
const result = [];
for (let i = 5; i < fileRespLength + 5; i++) {
const reg = data.readUInt8(i);
result.push(reg);
}
if(next)
next(null, { "data": result, "length": fileRespLength });
}
/**
* Parse the data for a Modbus -
* Read Device Identification (FC=43)
*
* @param {Buffer} data the data buffer to parse.
* @param {Modbus} modbus the client in case we need to read more device information
* @param {Function} next the function to call next.
*/
function _readFC43(data, modbus, next) {
const address = parseInt(data.readUInt8(0));
const readDeviceIdCode = parseInt(data.readUInt8(3));
const conformityLevel = parseInt(data.readUInt8(4));
const moreFollows = parseInt(data.readUInt8(5));
const nextObjectId = parseInt(data.readUInt8(6));
const numOfObjects = parseInt(data.readUInt8(7));
let startAt = 8;
const result = {};
// The modbus specification states that numOfObjects is the number of
// objects in the response, but the example on page 45 shows the total
// number over all responses. Therefore be careful about reading more data than available
for (let i = 0; i < numOfObjects && startAt < data.length; i++) {
const objectId = parseInt(data.readUInt8(startAt));
const objectLength = parseInt(data.readUInt8(startAt + 1));
const startOfData = startAt + 2;
result[objectId] = data.toString("ascii", startOfData, startOfData + objectLength);
startAt = startOfData + objectLength;
}
// is it saying to follow and did you previously get data
// if you did not previously get data go ahead and halt to prevent an infinite loop
if (moreFollows && numOfObjects) {
const cb = function(err, data) {
data.data = Object.assign(data.data, result);
return next(err, data);
};
modbus.writeFC43(address, readDeviceIdCode, nextObjectId, cb);
} else if (next) {
next(null, { data: result, conformityLevel });
}
}
/**
* Wrapper method for writing to a port with timeout. <code><b>[this]</b></code> has the context of ModbusRTU
* @param {Buffer} buffer The data to send
* @private
*/
function _writeBufferToPort(buffer, transactionId) {
const transaction = this._transactions[transactionId];
if (transaction) {
transaction._timeoutFired = false;
transaction._timeoutHandle = _startTimeout(this._timeout, transaction);
// If in debug mode, stash a copy of the request payload
if (this._debugEnabled) {
transaction.request = Uint8Array.prototype.slice.call(buffer);
transaction.responses = [];
}
}
this._port.write(buffer);
}
/**
* Starts the timeout timer with the given duration.
* If the timeout ends before it was cancelled, it will call the callback with an error.
* @param {number} duration the timeout duration in milliseconds.
* @param {Function} next the function to call next.
* @return {number} The handle of the timeout
* @private
*/
function _startTimeout(duration, transaction) {
if (!duration) {
return undefined;
}
return setTimeout(function() {
transaction._timeoutFired = true;
if (transaction.next) {
const err = new TransactionTimedOutError();
if (transaction.request && transaction.responses) {
err.modbusRequest = transaction.request;
err.modbusResponses = transaction.responses;
}
transaction.next(err);
}
}, duration);
}
/**
* Cancel the given timeout.
*
* @param {number} timeoutHandle The handle of the timeout
* @private
*/
function _cancelTimeout(timeoutHandle) {
clearTimeout(timeoutHandle);
}
/**
* Handle incoming data from the Modbus port.
*
* @param {Buffer} data The data received
* @private
*/
function _onReceive(data) {
const modbus = this;
let error;
// set locale helpers variables
const transaction = modbus._transactions[modbus._port._transactionIdRead];
// the _transactionIdRead can be missing, ignore wrong transaction it's
if (!transaction) {
return;
}
if (transaction.responses) {
/* Stash what we received */
transaction.responses.push(Uint8Array.prototype.slice.call(data));
}
/* What do we do next? */
const next = function(err, res) {
if (transaction.next) {
/* Include request/response data if enabled */
if (transaction.request && transaction.responses) {
if (err) {
err.modbusRequest = transaction.request;
err.modbusResponses = transaction.responses;
}
if (res) {
res.request = transaction.request;
res.responses = transaction.responses;
}
}
/* Pass the data on */
return transaction.next(err, res);
}
};
/* cancel the timeout */
_cancelTimeout(transaction._timeoutHandle);
transaction._timeoutHandle = undefined;
/* check if the timeout fired */
if (transaction._timeoutFired === true) {
// we have already called back with an error, so don't generate a new callback
return;
}
/* check incoming data
*/
/* check minimal length
*/
if (!transaction.lengthUnknown && data.length < 5) {
error = "Data length error, expected " +
transaction.nextLength + " got " + data.length;
next(new Error(error));
return;
}
/* check message CRC
* if CRC is bad raise an error
*/
const crcIn = data.readUInt16LE(data.length - 2);
if (crcIn !== crc16(data.slice(0, -2))) {
error = "CRC error";
next(new Error(error));
return;
}
// if crc is OK, read address and function code
const address = data.readUInt8(0);
const code = data.readUInt8(1);
/* check for modbus exception
*/
if (data.length >= 5 &&
code === (0x80 | transaction.nextCode)) {
const errorCode = data.readUInt8(2);
if (transaction.next) {
error = new Error("Modbus exception " + errorCode + ": " + (modbusErrorMessages[errorCode] || "Unknown error"));
error.modbusCode = errorCode;
next(error);
}
return;
}
/* check enron options are valid
*/
if (modbus._enron) {
const example = {
enronTables: {
booleanRange: [1001, 1999],
shortRange: [3001, 3999],
longRange: [5001, 5999],
floatRange: [7001, 7999]
}
};
if (typeof modbus._enronTables === "undefined" ||
modbus._enronTables.shortRange.length !== 2 ||
modbus._enronTables.shortRange[0] >= modbus._enronTables.shortRange[1]) {
next(new Error("Enron table definition missing from options. Example: " + JSON.stringify(example)));
return;
}
}
/* check message length
* if we do not expect this data
* raise an error
*/
if (!transaction.lengthUnknown && data.length !== transaction.nextLength) {
error = "Data length error, expected " +
transaction.nextLength + " got " + data.length;
next(new Error(error));
return;
}
/* check message address
* if we do not expect this message
* raise an error
*/
if (address !== transaction.nextAddress) {
error = "Unexpected data error, expected " +
"address " + transaction.nextAddress + " got " + address;
if (transaction.next)
next(new Error(error));
return;
}
/* check message code
* if we do not expect this message
* raise an error
*/
if (code !== transaction.nextCode) {
error = "Unexpected data error, expected " +
"code " + transaction.nextCode + " got " + code;
if (transaction.next)
next(new Error(error));
return;
}
/* parse incoming data
*/
try {
switch (code) {
case 1:
case 2:
// Read Coil Status (FC=01)
// Read Input Status (FC=02)
_readFC2(data, next);
break;
case 3:
case 4:
// Read Input Registers (FC=04)
// Read Holding Registers (FC=03)
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC3or4Enron(data, next);
} else {
_readFC3or4(data, next);
}
break;
case 5:
// Force Single Coil
_readFC5(data, next);
break;
case 6:
// Preset Single Register
if (modbus._enron && !(transaction.nextDataAddress >= modbus._enronTables.shortRange[0] && transaction.nextDataAddress <= modbus._enronTables.shortRange[1])) {
_readFC6Enron(data, next);
} else {
_readFC6(data, next);
}
break;
case 15:
case 16:
// Force Multiple Coils
// Preset Multiple Registers
_readFC16(data, next);
break;
case 17:
_readFC17(data, next);
break;
case 20:
_readFC20(data, transaction.next);
break;
case 43:
// read device identification
_readFC43(data, modbus, next);
}
} catch (e) {
if (transaction.next) {
next(e);
}
}
}
/**
* Handle SerialPort errors.
*
* @param {Error} error The error received
* @private
*/
function _onError(e) {
const err = new SerialPortError();
err.message = e.message;
err.stack = e.stack;
this.emit("error", err);
}
class ModbusRTU extends EventEmitter {
/**
* Class making ModbusRTU calls fun and easy.
*
* @param {SerialPort} port the serial port to use.
*/
constructor(port) {
super();
// the serial port to use
this._port = port;
// state variables
this._transactions = {};
this._timeout = null; // timeout in msec before unanswered request throws timeout error
this._unitID = 1;
// Flag to indicate whether debug mode (pass-through of raw
// request/response) is enabled.
this._debugEnabled = false;
this._onReceive = _onReceive.bind(this);
this._onError = _onError.bind(this);
}
/**
* Open the serial port and register Modbus parsers
*
* @param {Function} callback the function to call next on open success
* of failure.
*/
open(callback) {
const modbus = this;
// open the serial port
modbus._port.open(function(error) {
if (error) {
modbusSerialDebug({ action: "port open error", error: error });
/* On serial port open error call next function */
if (callback)
callback(error);
} else {
/* init ports transaction id and counter */
modbus._port._transactionIdRead = 1;
modbus._port._transactionIdWrite = 1;
/* On serial port success
* (re-)register the modbus parser functions
*/
modbus._port.removeListener("data", modbus._onReceive);
modbus._port.on("data", modbus._onReceive);
/* On serial port error
* (re-)register the error listener function
*/
modbus._port.removeListener("error", modbus._onError);
modbus._port.on("error", modbus._onError);
/* Hook the close event so we can relay it to our callers. */
modbus._port.once("close", modbus.emit.bind(modbus, "close"));
/* On serial port open OK call next function with no error */
if (callback)
callback(error);
}
});
}
get isDebugEnabled() {
return this._debugEnabled;
}
set isDebugEnabled(enable) {
enable = Boolean(enable);
this._debugEnabled = enable;
}
get isOpen() {
if (this._port) {
return this._port.isOpen;
}
return false;
}
/**
* Clears the timeout for all pending transactions.
* This essentially cancels all pending requests.
*/
_cancelPendingTransactions() {
if (Object.keys(this._transactions).length > 0) {
Object.values(this._transactions).forEach((transaction) => {
if (transaction._timeoutHandle) {
_cancelTimeout(transaction._timeoutHandle);
}
});
}
}
/**
* Close the serial port
*
* @param {Function} callback the function to call next on close success
* or failure.
*/
close(callback) {
// close the serial port if exist
if (this._port) {
this._port.removeAllListeners("data");
this._port.close(callback);
} else {
// nothing needed to be done
callback();
}
}
/**
* Destroy the serial port
*
* @param {Function} callback the function to call next on close success
* or failure.
*/
destroy(callback) {
// cancel all pending requests as we're closing the port
this._cancelPendingTransactions();
// close the serial port if exist and it has a destroy function
if (this._port && this._port.destroy) {
this._port.removeAllListeners("data");
this._port.destroy();
callback();
} else {
// nothing needed to be done
callback();
}
}
/**
* Write a Modbus "Read Coil Status" (FC=01) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the first coil.
* @param {number} length the total number of coils requested.
* @param {Function} next the function to call next.
*/
writeFC1(address, dataAddress, length, next) {
this.writeFC2(address, dataAddress, length, next, 1);
}
/**
* Write a Modbus "Read Input Status" (FC=02) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the first digital input.
* @param {number} length the total number of digital inputs requested.
* @param {Function} next the function to call next.
*/
writeFC2(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
// function code defaults to 2
code = code || 2;
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextCode: code,
nextLength: 3 + parseInt((length - 1) / 8 + 1) + 2,
next: next
};
const codeLength = 6;
const buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt16BE(dataAddress, 2);
buf.writeUInt16BE(length, 4);
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.subarray(0, -2)), codeLength);
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);
}
/**
* Write a Modbus "Read Holding Registers" (FC=03) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the first register.
* @param {number} length the total number of registers requested.
* @param {Function} next the function to call next.
*/
writeFC3(address, dataAddress, length, next) {
this.writeFC4(address, dataAddress, length, next, 3);
}
/**
* Write a Modbus "Read Input Registers" (FC=04) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the first register.
* @param {number} length the total number of registers requested.
* @param {Function} next the function to call next.
*/
writeFC4(address, dataAddress, length, next, code) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
// function code defaults to 4
code = code || 4;
let valueSize = 2;
if (this._enron && !(dataAddress >= this._enronTables.shortRange[0] && dataAddress <= this._enronTables.shortRange[1])) {
valueSize = 4;
}
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextDataAddress: dataAddress,
nextCode: code,
nextLength: 3 + (valueSize * length) + 2,
next: next
};
const codeLength = 6;
const buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt16BE(dataAddress, 2);
buf.writeUInt16BE(length, 4);
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.subarray(0, -2)), codeLength);
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);
}
/**
* Write a Modbus "Force Single Coil" (FC=05) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the coil.
* @param {number} state the boolean state to write to the coil (true / false).
* @param {Function} next the function to call next.
*/
writeFC5(address, dataAddress, state, next) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
const code = 5;
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
next: next
};
const codeLength = 6;
const buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt16BE(dataAddress, 2);
if (state) {
buf.writeUInt16BE(0xff00, 4);
} else {
buf.writeUInt16BE(0x0000, 4);
}
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.subarray(0, -2)), codeLength);
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);
}
/**
* Write a Modbus "Preset Single Register " (FC=6) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the register.
* @param {number} value the value to write to the register.
* @param {Function} next the function to call next.
*/
writeFC6(address, dataAddress, value, next) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
const code = 6;
let valueSize = 8;
if (this._enron && !(dataAddress >= this._enronTables.shortRange[0] && dataAddress <= this._enronTables.shortRange[1])) {
valueSize = 10;
}
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextDataAddress: dataAddress,
nextCode: code,
nextLength: valueSize,
next: next
};
let codeLength = 6; // 1B deviceAddress + 1B functionCode + 2B dataAddress + (2B value | 4B value (enron))
if (this._enron && !(dataAddress >= this._enronTables.shortRange[0] && dataAddress <= this._enronTables.shortRange[1])) {
codeLength = 8;
}
const buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt16BE(dataAddress, 2);
if (Buffer.isBuffer(value)) {
value.copy(buf, 4);
} else if (this._enron && !(dataAddress >= this._enronTables.shortRange[0] && dataAddress <= this._enronTables.shortRange[1])) {
buf.writeUInt32BE(value, 4);
} else {
buf.writeUInt16BE(value, 4);
}
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.subarray(0, -2)), codeLength);
// write buffer to serial port
_writeBufferToPort.call(this, buf, this._port._transactionIdWrite);
}
/**
* Write a Modbus "Force Multiple Coils" (FC=15) to serial port.
*
* @param {number} address the slave unit address.
* @param {number} dataAddress the Data Address of the first coil.
* @param {Array} array the array of boolean states to write to coils.
* @param {Function} next the function to call next.
*/
writeFC15(address, dataAddress, array, next) {
// check port is actually open before attempting write
if (this.isOpen !== true) {
if (next) next(new PortNotOpenError());
return;
}
// sanity check
if (typeof address === "undefined" || typeof dataAddress === "undefined") {
if (next) next(new BadAddressError());
return;
}
const code = 15;
let i = 0;
// set state variables
this._transactions[this._port._transactionIdWrite] = {
nextAddress: address,
nextCode: code,
nextLength: 8,
next: next
};
const dataBytes = Math.ceil(array.length / 8);
const codeLength = 7 + dataBytes;
const buf = Buffer.alloc(codeLength + 2); // add 2 crc bytes
buf.writeUInt8(address, 0);
buf.writeUInt8(code, 1);
buf.writeUInt16BE(dataAddress, 2);
buf.writeUInt16BE(array.length, 4);
buf.writeUInt8(dataBytes, 6);
// clear the data bytes before writing bits data
for (i = 0; i < dataBytes; i++) {
buf.writeUInt8(0, 7 + i);
}
for (i = 0; i < array.length; i++) {
// buffer bits are already all zero (0)
// only set the ones set to one (1)
if (array[i]) {
buf.writeBit(1, i, 7);
}
}
// add crc bytes to buffer
buf.writeUInt16LE(crc16(buf.subarray(0, -2)), codeLength);