nodbus-plus 0.8.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.readthedocs.yaml +35 -0
- package/LICENSE.md +21 -21
- package/README.md +163 -48
- package/docs/Makefile +20 -0
- package/docs/client/channel.rst +209 -0
- package/docs/client/nodbus_master_serial.rst +559 -0
- package/docs/client/nodbus_master_tcp.rst +573 -0
- package/docs/conf.py +41 -0
- package/docs/images/01-readcoils.png +0 -0
- package/docs/images/02-readinputs.png +0 -0
- package/docs/images/03-readholding.png +0 -0
- package/docs/images/04-readinputsreg.png +0 -0
- package/docs/images/05-writecoil.png +0 -0
- package/docs/images/06-writeregister.png +0 -0
- package/docs/images/15-writecoil.png +0 -0
- package/docs/images/16.png +0 -0
- package/docs/images/22-mask.png +0 -0
- package/docs/images/23.png +0 -0
- package/docs/images/7.png +0 -0
- package/docs/images/modbus_pdu.png +0 -0
- package/docs/images/serial_adu.png +0 -0
- package/docs/images/tcp_adu.png +0 -0
- package/docs/index.rst +30 -0
- package/docs/make.bat +35 -0
- package/docs/protocol/modbus_master.rst +276 -0
- package/docs/protocol/modbus_master_serial.rst +290 -0
- package/docs/protocol/modbus_master_tcp.rst +296 -0
- package/docs/protocol/modbus_server.rst +469 -0
- package/docs/protocol/modbus_server_serial.rst +543 -0
- package/docs/protocol/modbus_server_tcp.rst +365 -0
- package/docs/requirements.txt +4 -0
- package/docs/server/net_server.rst +242 -0
- package/docs/server/nodbus_serial_server.rst +652 -0
- package/docs/server/nodbus_tcp_server.rst +505 -0
- package/docs/starting.rst +192 -0
- package/docs/static/simple logo.jpg +0 -0
- package/package.json +39 -30
- package/samples/mb_serial_server.js +77 -0
- package/samples/mb_tcp_client.js +114 -0
- package/samples/mb_tcp_server.js +58 -0
- package/src/client/net/serialchannel.js +195 -0
- package/src/client/net/tcpchannel.js +233 -0
- package/src/client/net/udpchannel.js +243 -0
- package/src/client/nodbus_serial_client.js +577 -0
- package/src/client/nodbus_tcp_client.js +542 -0
- package/src/nodbus-plus.js +157 -110
- package/src/protocol/modbus_master.js +298 -961
- package/src/protocol/modbus_master_serial.js +247 -0
- package/src/protocol/modbus_master_tcp.js +219 -0
- package/src/protocol/modbus_server.js +936 -0
- package/src/protocol/modbus_server_serial.js +368 -0
- package/src/protocol/modbus_server_tcp.js +129 -0
- package/src/protocol/utils.js +296 -0
- package/src/server/net/serialserver.js +184 -0
- package/src/server/net/tcpserver.js +290 -0
- package/src/server/net/udpserver.js +242 -0
- package/src/server/nodbus_serial_server.js +238 -0
- package/src/server/nodbus_tcp_server.js +249 -0
- package/test/modbus_master.test.js +279 -0
- package/test/modbus_master_serial.test.js +124 -0
- package/test/modbus_master_tcp.test.js +178 -0
- package/test/modbus_server.test.js +506 -0
- package/test/modbus_server_serial.test.js +328 -0
- package/test/modbus_server_tcp.test.js +91 -0
- package/test/nodbus_client_serial.test.js +307 -0
- package/test/nodbus_client_tcp.test.js +334 -0
- package/test/nodbus_server_serial.test.js +255 -0
- package/test/nodbus_server_tcp.test.js +216 -0
- package/CHANGELOG.md +0 -27
- package/src/client/m_stcp_client.js +0 -214
- package/src/client/m_tcp_client.js +0 -234
- package/src/net/tcpclient.js +0 -173
- package/src/net/tcpserver.js +0 -329
- package/src/protocol/adu.js +0 -40
- package/src/protocol/ascii_adu.js +0 -139
- package/src/protocol/boolean_register.js +0 -78
- package/src/protocol/functions/Force_Multiple_Coils.js +0 -76
- package/src/protocol/functions/Force_Single_Coil.js +0 -54
- package/src/protocol/functions/Mask_Holding_Register.js +0 -47
- package/src/protocol/functions/Preset_Multiple_Registers.js +0 -53
- package/src/protocol/functions/Preset_Single_Register.js +0 -39
- package/src/protocol/functions/Read_Coil_Status.js +0 -59
- package/src/protocol/functions/Read_Holding_Registers.js +0 -52
- package/src/protocol/functions/Read_Input_Registers.js +0 -52
- package/src/protocol/functions/Read_Input_Status.js +0 -58
- package/src/protocol/mbap.js +0 -60
- package/src/protocol/modbus_device.js +0 -35
- package/src/protocol/modbus_slave.js +0 -522
- package/src/protocol/pdu.js +0 -70
- package/src/protocol/rtu_adu.js +0 -122
- package/src/protocol/serial_adu.js +0 -29
- package/src/protocol/tcp_adu.js +0 -84
- package/src/protocol/word_register.js +0 -122
- package/src/server/m_stcp_server.js +0 -310
- package/src/server/m_tcp_server.js +0 -295
- package/test/modbus-stcp-server-test.js +0 -72
- package/test/modbus-stcp-server-test1.js +0 -72
- package/test/modbus-tcp-client-test.js +0 -159
- package/test/modbus-tcp-server-test.js +0 -75
- package/test/modbus-tcp-server-test2.js +0 -75
- package/test/modbus_stcp_client.js +0 -149
package/docs/make.bat
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
@ECHO OFF
|
|
2
|
+
|
|
3
|
+
pushd %~dp0
|
|
4
|
+
|
|
5
|
+
REM Command file for Sphinx documentation
|
|
6
|
+
|
|
7
|
+
if "%SPHINXBUILD%" == "" (
|
|
8
|
+
set SPHINXBUILD=sphinx-build
|
|
9
|
+
)
|
|
10
|
+
set SOURCEDIR=.
|
|
11
|
+
set BUILDDIR=build
|
|
12
|
+
|
|
13
|
+
%SPHINXBUILD% >NUL 2>NUL
|
|
14
|
+
if errorlevel 9009 (
|
|
15
|
+
echo.
|
|
16
|
+
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
|
|
17
|
+
echo.installed, then set the SPHINXBUILD environment variable to point
|
|
18
|
+
echo.to the full path of the 'sphinx-build' executable. Alternatively you
|
|
19
|
+
echo.may add the Sphinx directory to PATH.
|
|
20
|
+
echo.
|
|
21
|
+
echo.If you don't have Sphinx installed, grab it from
|
|
22
|
+
echo.https://www.sphinx-doc.org/
|
|
23
|
+
exit /b 1
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
if "%1" == "" goto help
|
|
27
|
+
|
|
28
|
+
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
29
|
+
goto end
|
|
30
|
+
|
|
31
|
+
:help
|
|
32
|
+
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
|
|
33
|
+
|
|
34
|
+
:end
|
|
35
|
+
popd
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
.. _modbus_master:
|
|
2
|
+
|
|
3
|
+
==========================
|
|
4
|
+
Class: ModbusClient
|
|
5
|
+
==========================
|
|
6
|
+
|
|
7
|
+
.. contents:: Table of Contents
|
|
8
|
+
:depth: 3
|
|
9
|
+
|
|
10
|
+
**Nodbus-Plus v1.0 Documentation**
|
|
11
|
+
|
|
12
|
+
This class is an EventEmitter. It provides the basic functionalities to make Modbus Protocol Data Units (PDU).
|
|
13
|
+
|
|
14
|
+
.. Figure:: /images/modbus_pdu.png
|
|
15
|
+
|
|
16
|
+
*Modbus Protocol Data Unis*
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Creating a ModbusClient Instance
|
|
20
|
+
================================
|
|
21
|
+
|
|
22
|
+
new ModbusClient()
|
|
23
|
+
------------------
|
|
24
|
+
|
|
25
|
+
* **Returns:** <ModbusClient>
|
|
26
|
+
|
|
27
|
+
Constructor for new ModbusClient instance.
|
|
28
|
+
|
|
29
|
+
.. code-block:: javascript
|
|
30
|
+
|
|
31
|
+
const ModbusClient = require('nodbus-plus').ModbusClient;
|
|
32
|
+
let modbusClient = new ModbusClient();
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
ModbusClient's Methods
|
|
36
|
+
======================
|
|
37
|
+
|
|
38
|
+
.. _modbus_client_methods:
|
|
39
|
+
|
|
40
|
+
Method: modbusClient.readCoilStatusPdu([startCoil],[coilQuantity])
|
|
41
|
+
------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
* **startCoil** <number>: First coil to read, starting at address 0. Default value is 0
|
|
44
|
+
* **coilQuantity** <number>: Number of coils to read. Default value is 1
|
|
45
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
46
|
+
|
|
47
|
+
.. Figure:: /images/01-readcoils.png
|
|
48
|
+
|
|
49
|
+
*Modbus Read Coils Request and Response*
|
|
50
|
+
|
|
51
|
+
This method create the read coil status request pdu. Function code 01.
|
|
52
|
+
|
|
53
|
+
Method: modbusClient.readInputStatusPdu([startInput],[inputQuantity])
|
|
54
|
+
---------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
* **startInput** <number>: First input to read, starting at address 0. Default value is 0
|
|
57
|
+
* **inputQuantity** <number>: Number of inputs to read. Default value is 1
|
|
58
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
59
|
+
|
|
60
|
+
.. Figure:: /images/02-readinputs.png
|
|
61
|
+
|
|
62
|
+
*Modbus Read Inputs Request and Response*
|
|
63
|
+
|
|
64
|
+
This method create the read input status request pdu. Function code 02.
|
|
65
|
+
|
|
66
|
+
Method: modbusClient.readHoldingRegistersPdu([startRegister],[registerQuantity])
|
|
67
|
+
--------------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
* **startRegister** <number>: First register to read, starting at address 0. Default value is 0
|
|
70
|
+
* **registerQuantity** <number>: Number of registers to read. Default value is 1
|
|
71
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
72
|
+
|
|
73
|
+
.. Figure:: /images/03-readholding.png
|
|
74
|
+
|
|
75
|
+
*Modbus Read Holding Registers Request and Response*
|
|
76
|
+
|
|
77
|
+
This method create the read holding register request pdu. Function code 03.
|
|
78
|
+
|
|
79
|
+
Method: modbusClient.readInputRegistersPdu([startRegister],[registerQuantity])
|
|
80
|
+
------------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
* **startRegister** <number>: First register to read, starting at address 0. Default value is 0
|
|
83
|
+
* **registerQuantity** <number>: Number of registers to read. Default value is 1
|
|
84
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
85
|
+
|
|
86
|
+
.. Figure:: /images/04-readinputsreg.png
|
|
87
|
+
|
|
88
|
+
*Modbus Read Inputs Registers Request and Response*
|
|
89
|
+
|
|
90
|
+
This method create the read inputs register request pdu. Function code 04.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
Method: modbusClient.forceSingleCoilPdu(value, [startCoil])
|
|
94
|
+
------------------------------------------------------------
|
|
95
|
+
|
|
96
|
+
* **value** <Buffer>: Two bytes length buffer. Valid values are [0x00, 0x00] for false and [0xFF, 0x00] for true.
|
|
97
|
+
* **startCoil** <number>: Coil to be writed. Default value is 0.
|
|
98
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
99
|
+
|
|
100
|
+
.. Figure:: /images/05-writecoil.png
|
|
101
|
+
|
|
102
|
+
*Modbus Write Single Coil Request and Response*
|
|
103
|
+
|
|
104
|
+
This method create the force single coil request pdu. Function code 05. If value is not a Buffer throw a TypeError and if value's length is diferent than 2
|
|
105
|
+
throw a RangeError.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
Method: modbusClient.presetSingleRegisterPdu(value, [startRegister])
|
|
109
|
+
--------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
* **value** <Buffer>: Two bytes length buffer.
|
|
112
|
+
* **startRegister** <number>: Register's address to be writed. Default value is 0.
|
|
113
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
114
|
+
|
|
115
|
+
.. Figure:: /images/06-writeregister.png
|
|
116
|
+
|
|
117
|
+
*Modbus Write Single holding Register Request and Response*
|
|
118
|
+
|
|
119
|
+
This method create the preset single register request pdu. Function code 06. If value is not a Buffer throw a TypeError and if value's length is diferent than 2
|
|
120
|
+
throw a RangeError.
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
Method: modbusClient.forceMultipleCoilsPdu(values, startCoil, coilQuantity)
|
|
124
|
+
---------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
* **values** <Buffer>: Buffer with coils values.
|
|
127
|
+
* **startCoil** <number>: First Coil starting address.
|
|
128
|
+
* **coilQuantity** <number>: Number of coils to write.
|
|
129
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
130
|
+
|
|
131
|
+
.. Figure:: /images/15-writecoil.png
|
|
132
|
+
|
|
133
|
+
*Modbus Write Multiple Coils Request and Response*
|
|
134
|
+
|
|
135
|
+
This method create the force multiples coils request pdu. Function code 15. If values is not a Buffer throw a TypeError and if value's length is higher than 246
|
|
136
|
+
throw a RangeError.
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
Method: modbusClient.presetMultipleRegistersPdu(values, startRegister, [registerQuantity])
|
|
140
|
+
-------------------------------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
* **values** <Buffer>: Buffer with registers values.
|
|
143
|
+
* **startRegister** <number>: First register starting address.
|
|
144
|
+
* **registerQuantity** <number>: Number of registers to write. Default value is values.length/2.
|
|
145
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
146
|
+
|
|
147
|
+
.. Figure:: /images/16.png
|
|
148
|
+
|
|
149
|
+
*Modbus Write Multiple Registers Request and Response*
|
|
150
|
+
|
|
151
|
+
This method create the preset multiples registers request pdu. Function code 16. If values is not a Buffer throw a TypeError and if value's length is higher than 246
|
|
152
|
+
throw a RangeError.
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
Method: modbusClient.maskHoldingRegisterPdu(values, [startRegister])
|
|
156
|
+
---------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
* **values** <Buffer>: Buffer with registers values.
|
|
159
|
+
* **startRegister** <number>: Register address to modify.
|
|
160
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
161
|
+
|
|
162
|
+
.. Figure:: /images/22-mask.png
|
|
163
|
+
|
|
164
|
+
*Modbus Mask Register Request and Response*
|
|
165
|
+
|
|
166
|
+
This method create the mask holding register request pdu. Function code 22. If values is not a Buffer throw a TypeError and if value's length is diferent than 4
|
|
167
|
+
throw a RangeError.
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
Method: modbusClient.readWriteMultipleRegistersPdu(values, readStartingAddress, quantitytoRead, writeStartingAddress, quantityToWrite)
|
|
171
|
+
---------------------------------------------------------------------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
* **values** <Buffer>: Buffer with registers values to write.
|
|
174
|
+
* **readStartingAddress** <number>: First register's address to read.
|
|
175
|
+
* **quantitytoRead** <number>: Number of register to read.
|
|
176
|
+
* **writeStartingAddress** <number>: First register's address to write.
|
|
177
|
+
* **quantityToWrite** <number>: Number of registers to write.
|
|
178
|
+
* **Return** <Buffer>: buffer with req pdu.
|
|
179
|
+
|
|
180
|
+
.. Figure:: /images/23.png
|
|
181
|
+
|
|
182
|
+
*Modbus Read and Write Multiple Registers Request and Response*
|
|
183
|
+
|
|
184
|
+
This method create the read and write holding register request pdu. Function code 23. If values is not a Buffer throw a TypeError and if value's length is greater than 243
|
|
185
|
+
throw a RangeError.
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
Method: modbusClient.boolToBuffer(value)
|
|
189
|
+
---------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
* **value** <boolean>
|
|
192
|
+
* **Return** <Buffer>: Two bytes length Buffer.
|
|
193
|
+
|
|
194
|
+
This is a utitlity method. It returns a buffer with a boolean value encoded for use on forceSingleCoilPdu function as value argument. Example:
|
|
195
|
+
|
|
196
|
+
.. code-block:: javascript
|
|
197
|
+
|
|
198
|
+
let value = modbusClient.boolToBuffer(false);
|
|
199
|
+
console.log(value); //Buffer:[0x00, 0x00]
|
|
200
|
+
value = modbusClient.boolToBuffer(true);
|
|
201
|
+
console.log(value); //Buffer:[0xFF, 0x00]
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
Method: modbusClient.getMaskRegisterBuffer(value)
|
|
205
|
+
---------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
* **value** <Array>: An 16 numbers length array indicating how to mask the register.
|
|
208
|
+
* **Return** <Buffer>: Four bytes length Buffer.
|
|
209
|
+
|
|
210
|
+
This is a utility method that return a four-byte length buffer with the AND_MASK and OR_MASK values encoded for use in the maskHoldingRegisterPdu function as the value argument.
|
|
211
|
+
|
|
212
|
+
The value argument is a 16-number array, with each number representing the position of one bit inside the register. If the number is 1, then the corresponding bit will be set to 1.
|
|
213
|
+
If the number is 0, then the corresponding bit will be set to 0. If the number is different from 0 or 1, then the corresponding bit will remain unchanged. For example:
|
|
214
|
+
|
|
215
|
+
.. code-block:: javascript
|
|
216
|
+
|
|
217
|
+
let value = [-1, 0, 1, -1, -1, -1, 0, 0, 1, -1, -1, -1, -1, -1, 1, 1];
|
|
218
|
+
maskBuffer = modbusClient.getMaskRegisterBuffer(value);
|
|
219
|
+
|
|
220
|
+
//masks
|
|
221
|
+
let andMask = maskBuffer.readUInt16BE(0);
|
|
222
|
+
let orMask = maskBuffer.readUInt16BE(2);
|
|
223
|
+
|
|
224
|
+
let testRegister = Buffer.from([0x9A, 0xFB]);
|
|
225
|
+
console.log(testRegister)
|
|
226
|
+
let currentContent = testRegister.readUInt16BE(0);
|
|
227
|
+
let finalResult = (currentContent & andMask) | (orMask & (~andMask)); //Modbus Spec
|
|
228
|
+
|
|
229
|
+
let finalRegister = Buffer.alloc(2);
|
|
230
|
+
finalRegister.writeUInt16BE(finalResult, 0);
|
|
231
|
+
console.log(finalRegister)
|
|
232
|
+
|
|
233
|
+
//Output
|
|
234
|
+
//<Buffer 9a fb>
|
|
235
|
+
//<Buffer db 3d>
|
|
236
|
+
|
|
237
|
+
Method: modbusClient.boolsToBuffer(value)
|
|
238
|
+
---------------------------------------------------------------------
|
|
239
|
+
|
|
240
|
+
* **value** <Array>: A boolean array.
|
|
241
|
+
* **Return** <Buffer>: a buffer with binary representation of boolean array.
|
|
242
|
+
|
|
243
|
+
This is a utility method that return a buffer from a boolean array for modbus function code 15.
|
|
244
|
+
|
|
245
|
+
The value argument is an array of boolean with values to be force to coils. For example:
|
|
246
|
+
|
|
247
|
+
.. code-block:: javascript
|
|
248
|
+
|
|
249
|
+
let values = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]; //at 0 index stat LSB Byte
|
|
250
|
+
valBuffer = modbusClient.boolsToBuffer(values);
|
|
251
|
+
|
|
252
|
+
//result valBuffer [0xC2 0x04]
|
|
253
|
+
// calling force multiples colis
|
|
254
|
+
let pdu = modbusClient.forceMultipleCoilsPdu(valBuffer, 10, values.length) //calling force multiples coils at coil 10 and 11 coils to force
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
Method: modbusClient.getWordFromBuffer(targetBuffer, [offset])
|
|
259
|
+
--------------------------------------------------------------
|
|
260
|
+
|
|
261
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to read.
|
|
262
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
263
|
+
* **Return** <Buffer>: A two bytes length buffer.
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
This method read two bytes from target buffer with 16 bits align. Offset 0 get bytes 0 and 1, offset 4 gets bytes 8 and 9
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
Method: modbusClient.setWordToBuffer(value, targetBuffer, [offset])
|
|
270
|
+
-------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
* **value** <Buffer>: two bytes length buffer.
|
|
273
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to write.
|
|
274
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
275
|
+
|
|
276
|
+
This method write a 16 bits register inside a buffer. The offset is 16 bits aligned.
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
.. _modbus_serial_master:
|
|
2
|
+
|
|
3
|
+
===========================
|
|
4
|
+
Class: ModbusSerialClient
|
|
5
|
+
===========================
|
|
6
|
+
|
|
7
|
+
**Nodbus-Plus v1.0 Documentation**
|
|
8
|
+
|
|
9
|
+
.. contents:: Table of Contents
|
|
10
|
+
:depth: 3
|
|
11
|
+
|
|
12
|
+
This class extends :ref:`ModbusClient Class <modbus_master>`. It provides the basic functionalities to handle Modbus Serial Aplication Data Units (ADU).
|
|
13
|
+
|
|
14
|
+
.. Figure:: /images/serial_adu.png
|
|
15
|
+
|
|
16
|
+
*Modbus Serial Aplication Data Unis*
|
|
17
|
+
|
|
18
|
+
Creating a ModbusSerialClient Instance
|
|
19
|
+
======================================
|
|
20
|
+
|
|
21
|
+
new ModbusSerialClient()
|
|
22
|
+
-------------------------
|
|
23
|
+
|
|
24
|
+
* **Returns:** <ModbusSerialClient>
|
|
25
|
+
|
|
26
|
+
Constructor for new ModbusSerialClient instance.
|
|
27
|
+
|
|
28
|
+
.. code-block:: javascript
|
|
29
|
+
|
|
30
|
+
const ModbusSerialClient = require('nodbus-plus').ModbusSerialClient;
|
|
31
|
+
let modbusSerialClient = new ModbusSerialClient();
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
ModbusSerialClient's Events
|
|
35
|
+
===========================
|
|
36
|
+
|
|
37
|
+
Event: 'broadcast-timeout'
|
|
38
|
+
---------------------------
|
|
39
|
+
|
|
40
|
+
This event is emmited when the number of milliseconds pass to :ref:`Method: modbusSerialClient.setTurnAroundDelay([timeout])` is reached. Indicate that client
|
|
41
|
+
has no pending broadcast request and is free to send another request.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
Event: 'req-timeout'
|
|
45
|
+
--------------------
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
* **req** <Buffer>: Modbus request adu buffer.
|
|
49
|
+
|
|
50
|
+
.. code-block:: javascript
|
|
51
|
+
|
|
52
|
+
modbusSerialClient.on('req-timeout', (req) ->{
|
|
53
|
+
console.log('Timeout error from slave: ' + req[0] + '\n');
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
This event is emmited when the number of milliseconds pass to :ref:`Method: modbusSerialClient.setReqTimer([timeout])` ends without call
|
|
57
|
+
:ref:`Method: modbusSerialClient.clearReqTimer()`.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
Event: 'transaction'
|
|
62
|
+
--------------------
|
|
63
|
+
|
|
64
|
+
* **req** <Buffer>: Modbus Tcp request adu.
|
|
65
|
+
* **res** <Buffer>: Modbus Tcp request adu.
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
This event is emmited when the :ref:`Method: modbusSerialClient.processResAdu(bufferAdu, [ascii])` is called to manage a server response.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
ModbusSerialClient's Atributes
|
|
73
|
+
===============================
|
|
74
|
+
|
|
75
|
+
Atribute: modbusSerialClient.activeRequest
|
|
76
|
+
------------------------------------------
|
|
77
|
+
|
|
78
|
+
* <Buffer>
|
|
79
|
+
|
|
80
|
+
This property store the current active request, if no request is pending then is null.
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
Atribute: modbusSerialClient.activeRequestTimerId
|
|
84
|
+
-------------------------------------------------
|
|
85
|
+
|
|
86
|
+
* <Number>
|
|
87
|
+
|
|
88
|
+
A property to store active request's timer. Each request start a timeout timer when is sended to server. This map store the timer for the active request.
|
|
89
|
+
|
|
90
|
+
Atribute: modbusSerialClient.turnAroundDelay
|
|
91
|
+
---------------------------------------------
|
|
92
|
+
|
|
93
|
+
* <number>
|
|
94
|
+
|
|
95
|
+
When the serial client send a broadcast request have to await for the turnaround timer to send a new request. This property staore the value in milliseconds for this timer.
|
|
96
|
+
Default value is 10 ms.
|
|
97
|
+
|
|
98
|
+
ModbusSerialClient's Methods
|
|
99
|
+
============================
|
|
100
|
+
|
|
101
|
+
.. _modbus_serial_client_methods:
|
|
102
|
+
|
|
103
|
+
See :ref:`ModbusClient Class Methods <modbus_client_methods>` for base class inherited methods.
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
Method: modbusSerialClient.aduAsciiToRtu(asciiFrame)
|
|
107
|
+
----------------------------------------------------
|
|
108
|
+
|
|
109
|
+
* **asciiFrame** <Buffer>: A serial ascii adu.
|
|
110
|
+
* **Returns** <Buffer>: A serial rtu adu.
|
|
111
|
+
|
|
112
|
+
This method get a ascii adu and convert it in a equivalent rtu adu, including the crc checksum.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
Method: modbusSerialClient.aduRtuToAscii(rtuFrame)
|
|
116
|
+
----------------------------------------------------
|
|
117
|
+
|
|
118
|
+
* **rtuFrame** <Buffer>: A serial rtu adu.
|
|
119
|
+
* **Returns** <Buffer>: A serial ascii adu.
|
|
120
|
+
|
|
121
|
+
This method get a rtu adu and convert it in a equivalent ascii adu, including the lrc checksum.
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
Method: modbusSerialClient.boolToBuffer(value)
|
|
125
|
+
---------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
* **value** <boolean>
|
|
128
|
+
* **Return** <Buffer>: Two bytes length Buffer.
|
|
129
|
+
|
|
130
|
+
This is a utitlity method. It gets a buffer with a boolean value encoded for use on forceSingleCoilPdu function as value argument. Example:
|
|
131
|
+
|
|
132
|
+
.. code-block:: javascript
|
|
133
|
+
|
|
134
|
+
let value = modbusSerialClient.boolToBuffer(false);
|
|
135
|
+
console.log(value); //Buffer:[0x00, 0x00]
|
|
136
|
+
value = modbusSerialClient.boolToBuffer(true);
|
|
137
|
+
console.log(value); //Buffer:[0xFF, 0x00]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
Method: modbusSerialClient.calcCRC(frame)
|
|
141
|
+
--------------------------------------------------
|
|
142
|
+
|
|
143
|
+
* **frame** <Buffer>: A serial rtu adu request buffer received by server.
|
|
144
|
+
* **Returns** <number>: crc value for request.
|
|
145
|
+
|
|
146
|
+
This method calculate the checksum for he buffer request and return it. It receives a complete rtu frame and ignore the crc field (last two bytes) when calculate the crc value.
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
Method: modbusSerialClient.calcLRC(frame)
|
|
150
|
+
--------------------------------------------------
|
|
151
|
+
|
|
152
|
+
* **frame** <Buffer>: A serial ascii adu request buffer received by server.
|
|
153
|
+
* **Returns** <number>: lrc value for request.
|
|
154
|
+
|
|
155
|
+
This method calculate the checksum for he buffer request and return it. It receives a complete ascii frame including start character (:) and ending characters.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
Method: modbusSerialClient.getMaskRegisterBuffer(value)
|
|
159
|
+
---------------------------------------------------------------------
|
|
160
|
+
|
|
161
|
+
* **value** <Array>: An 16 numbers length array indicating how to mask the register.
|
|
162
|
+
* **Return** <Buffer>: Four bytes length Buffer.
|
|
163
|
+
|
|
164
|
+
This is a utility method that return a four-byte length buffer with the AND_MASK and OR_MASK values encoded for use in the maskHoldingRegisterPdu function as the value argument.
|
|
165
|
+
|
|
166
|
+
The value argument is a 16-number array, with each number representing the position of one bit inside the register. If the number is 1, then the corresponding bit will be set to 1.
|
|
167
|
+
If the number is 0, then the corresponding bit will be set to 0. If the number is different from 0 or 1, then the corresponding bit will remain unchanged. For example:
|
|
168
|
+
|
|
169
|
+
.. code-block:: javascript
|
|
170
|
+
|
|
171
|
+
let value = [-1, 0, 1, -1, -1, -1, 0, 0, 1, -1, -1, -1, -1, -1, 1, 1];
|
|
172
|
+
maskBuffer = modbusSerialClient.getMaskRegisterBuffer(value);
|
|
173
|
+
|
|
174
|
+
//masks
|
|
175
|
+
let andMask = maskBuffer.readUInt16BE(0);
|
|
176
|
+
let orMask = maskBuffer.readUInt16BE(2);
|
|
177
|
+
|
|
178
|
+
let testRegister = Buffer.from([0x9A, 0xFB]);
|
|
179
|
+
console.log(testRegister)
|
|
180
|
+
let currentContent = testRegister.readUInt16BE(0);
|
|
181
|
+
let finalResult = (currentContent & andMask) | (orMask & (~andMask)); //Modbus Spec
|
|
182
|
+
|
|
183
|
+
let finalRegister = Buffer.alloc(2);
|
|
184
|
+
finalRegister.writeUInt16BE(finalResult, 0);
|
|
185
|
+
console.log(finalRegister)
|
|
186
|
+
|
|
187
|
+
//Output
|
|
188
|
+
//<Buffer 9a fb>
|
|
189
|
+
//<Buffer db 3d>
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
Method: modbusSerialClient.boolsToBuffer(value)
|
|
193
|
+
---------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
* **value** <Array>: A boolean array.
|
|
196
|
+
* **Return** <Buffer>: a buffer with binary representation of boolean array.
|
|
197
|
+
|
|
198
|
+
This is a utility method that return a buffer from a boolean array for modbus function code 15.
|
|
199
|
+
|
|
200
|
+
The value argument is a array of boolean with values to bu force to coils. For example:
|
|
201
|
+
|
|
202
|
+
.. code-block:: javascript
|
|
203
|
+
|
|
204
|
+
let values = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]; //at 0 index stat LSB Byte
|
|
205
|
+
valBuffer = modbusSerialClient.boolsToBuffer(values);
|
|
206
|
+
|
|
207
|
+
//result valBuffer [0xC2 0x04]
|
|
208
|
+
// calling force multiples colis
|
|
209
|
+
let pdu = modbusSerialClient.forceMultipleCoilsPdu(valBuffer, 10, values.length) //calling force multiples coils at coil 10 and 11 coils to force
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
Method: modbusSerialClient.makeRequest(address, pdu, asciiMode)
|
|
213
|
+
---------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
* **address** <number>: Modbus address.
|
|
216
|
+
* **pdu** <Buffer>: The pdu's buffer.
|
|
217
|
+
* **asciiMode** <boolean> A flag to indicate if the request must be in ascii format. Default value is false, rtu mode.
|
|
218
|
+
* **Returns** <Buffer>: return a serial adu request's buffer
|
|
219
|
+
|
|
220
|
+
This functions create a modbus serial request ready to be send to the client.
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
Method: modbusSerialClient.setReqTimer([timeout])
|
|
224
|
+
---------------------------------------------------
|
|
225
|
+
|
|
226
|
+
* **timeout** <number>: Number of milliseconds to await for a response or fire timeout event.
|
|
227
|
+
* **Returns** <number>: Timer's id to be use on clearTimeout.
|
|
228
|
+
|
|
229
|
+
This functions store a timerId in the :ref:`request timers pool <Atribute: modbusSerialClient.activeRequestTimerId>`.
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
Method: modbusSerialClient.setTurnAroundDelay([timeout])
|
|
233
|
+
---------------------------------------------------
|
|
234
|
+
|
|
235
|
+
* **timeout** <number>: Number of milliseconds to await for fire broadcast-timeout event.
|
|
236
|
+
* **Returns** <number>: Timer's id to be use on clearTimeout.
|
|
237
|
+
|
|
238
|
+
This functions store a timerId in the :ref:`request timers pool <Atribute: modbusSerialClient.turnAroundDelay>`. Is used when a broadcast request
|
|
239
|
+
is sended.
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
Method: modbusSerialClient.clearReqTimer()
|
|
243
|
+
--------------------------------------------
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
This functions call the build in clearTimeout function to avoid emit the'req-timeout' event.
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
Method: modbusSerialClient.processResAdu(bufferAdu, [ascii])
|
|
250
|
+
-------------------------------------------------------------
|
|
251
|
+
|
|
252
|
+
* **bufferAdu** <Buffer>: A modbus tcp adu response buffer.
|
|
253
|
+
* **ascii** <boolean>: A flag indicating if the response is in ascii mode.
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
This method is used to managed server response. Call the :ref:`Method: modbusSerialClient.clearReqTimer()` to avoid emit 'req-timeout' event and emit the 'transaction' event.
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
Method: modbusSerialClient.getWordFromBuffer(targetBuffer, [offset])
|
|
260
|
+
--------------------------------------------------------------
|
|
261
|
+
|
|
262
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to read.
|
|
263
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
264
|
+
* **Return** <Buffer>: A two bytes length buffer.
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
This method read two bytes from target buffer with 16 bits align. Offset 0 get bytes 0 and 1, offset 4 gets bytes 8 and 9
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
Method: modbusSerialClient.setWordToBuffer(value, targetBuffer, [offset])
|
|
271
|
+
-------------------------------------------------------------------
|
|
272
|
+
|
|
273
|
+
* **value** <Buffer>: two bytes length buffer.
|
|
274
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to write.
|
|
275
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
276
|
+
|
|
277
|
+
This method write a 16 bits register inside a buffer. The offset is 16 bits aligned.
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
Method: modbusSerialClient.storeRequest(bufferReq, ascciiMode)
|
|
281
|
+
-------------------------------------------------------------------
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
* **bufferRequest** <Buffer>: A buffer with the modbus request.
|
|
285
|
+
* **asciiMode** <boolean>: A flag that indicate that request stored is ascii.
|
|
286
|
+
* **return** <boolean>: True if success
|
|
287
|
+
|
|
288
|
+
This method checks if activeRequest property is null, if not it return false indicating tha are still a request pending.
|
|
289
|
+
It also checks if the request is ascii or not and set the internal ascii flag for active request.
|
|
290
|
+
|