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
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
.. _modbus_tcp_server:
|
|
2
|
+
|
|
3
|
+
======================
|
|
4
|
+
Class: ModbusTcpServer
|
|
5
|
+
======================
|
|
6
|
+
|
|
7
|
+
**Nodbus-Plus v1.0 Documentation**
|
|
8
|
+
|
|
9
|
+
.. contents:: Table of Contents
|
|
10
|
+
:depth: 3
|
|
11
|
+
|
|
12
|
+
This class extends :ref:`ModbusServer Class <modbus_server>`. It provides the basic functionalities to handle Modbus TCP Aplication Data Units (ADU).
|
|
13
|
+
|
|
14
|
+
.. Figure:: /images/tcp_adu.png
|
|
15
|
+
|
|
16
|
+
*Modbus Tcp Aplication Data Unis*
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
Creating a ModbusTCPServer Instance
|
|
20
|
+
===================================
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
new ModbusTcpServer([options])
|
|
24
|
+
-------------------------------
|
|
25
|
+
|
|
26
|
+
* **options** <object>: Configuration object with following properties:
|
|
27
|
+
|
|
28
|
+
* inputs <number>: The cuantity of inputs that the server will have. It's an integer between 0 and 65535. If a value of 0 is entered, then the inputs will share the same Buffer as the inputs registers. Default value is 2048.
|
|
29
|
+
|
|
30
|
+
* coils <number>: The cuantity of coils that the server will have. It's an integer between 0 and 65535. If a value of 0 is entered, then the coils will share the same Buffer as holding registers. Default value is 2048.
|
|
31
|
+
|
|
32
|
+
* holdingRegisters <number>: The cuantity of holding registers that the server will have. It's an integer between 1 and 65535. Default value is 2048.
|
|
33
|
+
|
|
34
|
+
* inputRegisters <number>: The cuantity of input registers that the server will have. It's an integer between 1 and 65535. Default value is 2048.
|
|
35
|
+
|
|
36
|
+
* **Returns:** <ModbusTcpServer>
|
|
37
|
+
|
|
38
|
+
Constructor for new ModbusTcpServer instance.
|
|
39
|
+
|
|
40
|
+
.. code-block:: javascript
|
|
41
|
+
|
|
42
|
+
const ModbusTcpServer = require('nodbus-plus').ModbusTcpServer;
|
|
43
|
+
let modbusTcpServer = new ModbusTcpServer({inputs: 1024, coils: 512}); //new server with 1024 inputs, 512 coils and 2048 holding and inputs registers
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
ModbusTcpServer's Events
|
|
47
|
+
=========================
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
Event: 'error'
|
|
51
|
+
--------------
|
|
52
|
+
|
|
53
|
+
* **e** <Error>: The error object.
|
|
54
|
+
|
|
55
|
+
Emitted when a error occurs.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Event: 'exception'
|
|
59
|
+
---------------------
|
|
60
|
+
|
|
61
|
+
* **functionCode** <number>: request function code.
|
|
62
|
+
* **exceptionCode** <number>: the code of exception
|
|
63
|
+
* **name** <string>: Name of exception.
|
|
64
|
+
|
|
65
|
+
.. raw:: html
|
|
66
|
+
|
|
67
|
+
<table>
|
|
68
|
+
<tr>
|
|
69
|
+
<th>Code</th>
|
|
70
|
+
<th>Name</th>
|
|
71
|
+
<th>Meaning</th>
|
|
72
|
+
</tr>
|
|
73
|
+
<tr>
|
|
74
|
+
<td>01</td>
|
|
75
|
+
<td>ILLEGAL FUNCTION</td>
|
|
76
|
+
<td>The function code received in the query is not an allowable action for the server.</td>
|
|
77
|
+
</tr>
|
|
78
|
+
<tr>
|
|
79
|
+
<td>02</td>
|
|
80
|
+
<td>ILLEGAL DATA ADDRESS</td>
|
|
81
|
+
<td>The data address received in the query is not an allowable address for the server.</td>
|
|
82
|
+
</tr>
|
|
83
|
+
<tr>
|
|
84
|
+
<td>03</td>
|
|
85
|
+
<td>ILLEGAL DATA VALUE</td>
|
|
86
|
+
<td>A value contained in the query data field is not an allowable value for server</td>
|
|
87
|
+
</tr>
|
|
88
|
+
<tr>
|
|
89
|
+
<td>04</td>
|
|
90
|
+
<td>SLAVE DEVICE FAILURE</td>
|
|
91
|
+
<td>An unrecoverable error occurred while the server was attempting to perform the requested action.</td>
|
|
92
|
+
</tr>
|
|
93
|
+
<tr>
|
|
94
|
+
<td>05</td>
|
|
95
|
+
<td>ACKNOWLEDGE</td>
|
|
96
|
+
<td>The server (or slave) has accepted the request and is processing it, but a long duration of time will be required to do so.
|
|
97
|
+
This response is returned to prevent a timeout error from occurringin the client (or master).</td>
|
|
98
|
+
</tr>
|
|
99
|
+
<tr>
|
|
100
|
+
<td>06</td>
|
|
101
|
+
<td>SLAVE DEVICE BUSY</td>
|
|
102
|
+
<td>Specialized use in conjunction with programming commands. The server (or slave) is engaged in processing a long–duration program command.</td>
|
|
103
|
+
</tr>
|
|
104
|
+
<tr>
|
|
105
|
+
<td>08</td>
|
|
106
|
+
<td>MEMORY PARITY ERROR</td>
|
|
107
|
+
<td>Specialized use in conjunction with function codes 20 and 21 and reference type 6, to indicate that the extended file area failed to pass a consistency check.</td>
|
|
108
|
+
</tr>
|
|
109
|
+
<tr>
|
|
110
|
+
<td>0A</td>
|
|
111
|
+
<td>GATEWAY PATH UNAVAILABLE</td>
|
|
112
|
+
<td>Specialized use in conjunction with gateways, indicates that the gateway was unable to allocate an internal communication path from the input port to the output port for processing the request.
|
|
113
|
+
Usually means that the gateway is misconfigured or overloaded.</td>
|
|
114
|
+
</tr>
|
|
115
|
+
<tr>
|
|
116
|
+
<td>0B</td>
|
|
117
|
+
<td>GATEWAY TARGET DEVICE FAILED TO RESPOND</td>
|
|
118
|
+
<td>Specialized use in conjunction with gateways, indicates that no response was obtained from the target device. Usually means that the device is not present on the network.</td>
|
|
119
|
+
</tr>
|
|
120
|
+
</table>
|
|
121
|
+
|
|
122
|
+
Emitted when a Modbus exception occurs.
|
|
123
|
+
|
|
124
|
+
Event: 'write-coils'
|
|
125
|
+
--------------
|
|
126
|
+
|
|
127
|
+
* **startCoil** <number> Indicate in wich coil start the new value.
|
|
128
|
+
|
|
129
|
+
* **cuantityOfCoils** <number>: amound of coils modificated
|
|
130
|
+
|
|
131
|
+
Emitted after change a coil value due to a clienst write coil request.
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
Event: 'write-registers'
|
|
135
|
+
--------------
|
|
136
|
+
|
|
137
|
+
* **startRegister** <number> Indicate in wich register start the new value.
|
|
138
|
+
|
|
139
|
+
* **cuantityOfRegister** <number>: amound of register modificated.
|
|
140
|
+
|
|
141
|
+
Emitted after change a holding register value due to a clienst write register request.
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
ModbusTcpServer's Atributes
|
|
145
|
+
===========================
|
|
146
|
+
|
|
147
|
+
Atribute: modbusTcpServer._internalFunctionCode
|
|
148
|
+
--------------------------------------------
|
|
149
|
+
|
|
150
|
+
* <Map>
|
|
151
|
+
|
|
152
|
+
This property stores the Modbus functions codes supported by the server.
|
|
153
|
+
It's a map composed of an integer number with the Modbus function code as the key and the name of the method that will be invoked to resolve that code as the value.
|
|
154
|
+
|
|
155
|
+
.. code-block:: javascript
|
|
156
|
+
|
|
157
|
+
//Example of how to add new custom modbus function code handle function
|
|
158
|
+
class ModbusTcpServerExtended extends ModbusTcpServer{
|
|
159
|
+
constructor(mbServerCfg){
|
|
160
|
+
super(mbServerCfg)
|
|
161
|
+
//adding the new function code and the name of handler
|
|
162
|
+
this._internalFunctionCode.set(68, 'customService68');
|
|
163
|
+
}
|
|
164
|
+
//New method to handle function code 68. receive a buffer with pdu data as argument.
|
|
165
|
+
customService68(pduReqData){
|
|
166
|
+
let resp = Buffer.alloc(2);
|
|
167
|
+
resp[0] = 68;
|
|
168
|
+
resp[1] = pduReqData[0];
|
|
169
|
+
return resp
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
Atribute: modbusTcpServer.supportedFunctionCode
|
|
175
|
+
------------------------------------------------
|
|
176
|
+
|
|
177
|
+
* <iterator>
|
|
178
|
+
|
|
179
|
+
This is a getter that return an iterator object trhough modbusTcpServer._internalFunctionCode keys. It's the same that call modbusTcpServer._internalFunctionCode.keys().
|
|
180
|
+
|
|
181
|
+
.. code-block:: javascript
|
|
182
|
+
|
|
183
|
+
//Example of getting all suported function code.
|
|
184
|
+
for(const functionCode of modbusTcpServer.supportedFunctionCode){
|
|
185
|
+
console.log(functionCode)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
Atribute: modbusTcpServer.holdingRegisters
|
|
189
|
+
-------------------------------------------
|
|
190
|
+
|
|
191
|
+
* <Buffer>
|
|
192
|
+
|
|
193
|
+
This property is a Buffer that store the servers' holding registers.
|
|
194
|
+
The Modbus protocol specifies the order in which bytes are sent and receive. Modbus Plus uses a big-endian encoding to send the content of 16-bit registers.
|
|
195
|
+
This means that byte[0] of the register will be considered the MSB and byte[1] the LSB.
|
|
196
|
+
|
|
197
|
+
Each register starts at the even byte of the buffer.Therefore, register 0 starts at byte 0 and occupies bytes 0 and 1, register 1 starts at byte 2 and occupies bytes 2 and 3, and so on.
|
|
198
|
+
|
|
199
|
+
To read or write values in the registers, you can use the buffer's methods (see Node.js documentation), but it is recommended to use the
|
|
200
|
+
:ref:`getWordFromBuffer method <Method: modbusTcpServer.getWordFromBuffer(targetBuffer, [offset])>` and the :ref:`setWordtoBuffer method <Method: modbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])>`.
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
Atribute: modbusTcpServer.inputRegisters
|
|
204
|
+
------------------------------------------
|
|
205
|
+
|
|
206
|
+
* <Buffer>
|
|
207
|
+
|
|
208
|
+
This property is a Buffer that store the servers' input registers.
|
|
209
|
+
The Modbus protocol specifies the order in which bytes are sent and receive. Modbus Plus uses a big-endian encoding to send the content of 16-bit registers.
|
|
210
|
+
This means that byte[0] of the register will be considered the MSB and byte[1] the LSB.
|
|
211
|
+
|
|
212
|
+
Each register starts at the even byte of the buffer.Therefore, register 0 starts at byte 0 and occupies bytes 0 and 1, register 1 starts at byte 2 and occupies bytes 2 and 3, and so on.
|
|
213
|
+
|
|
214
|
+
To read or write values in the registers, you can use the buffer's methods (see Node.js documentation), but it is recommended to use the
|
|
215
|
+
:ref:`getWordFromBuffer method <Method: modbusTcpServer.getWordFromBuffer(targetBuffer, [offset])>` and the :ref:`setWordtoBuffer method <Method: modbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])>`.
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
Atribute: modbusTcpServer.inputs
|
|
219
|
+
---------------------------------
|
|
220
|
+
|
|
221
|
+
* <Buffer>
|
|
222
|
+
|
|
223
|
+
This property is a Buffer that store the servers' digital inputs. The byte 0 store the inputs 0 to 7, byte 1 store inputs 8-15 and so on.
|
|
224
|
+
|
|
225
|
+
To read and write digital values to the buffer, the modbus server provides the methods :ref:`getBoolFromBuffer <Method: modbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])>`
|
|
226
|
+
and :ref:`setBooltoBuffer method <Method: modbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])>`.
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
Atribute: modbusTcpServer.coils
|
|
230
|
+
-------------------------------
|
|
231
|
+
|
|
232
|
+
* <Buffer>
|
|
233
|
+
|
|
234
|
+
This property is a Buffer that store the servers' digital coils. The byte 0 store the coils 0 to 7, byte 1 store coils 8-15 and so on.
|
|
235
|
+
|
|
236
|
+
To read and write digital values to the buffer, the modbus server provides the methods :ref:`getBoolFromBuffer <Method: modbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])>` and :ref:`setBooltoBuffer method <Method: modbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])>`.
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
ModbusTcpServer's Methods
|
|
240
|
+
=========================
|
|
241
|
+
|
|
242
|
+
.. _modbus_tcp_server_methods:
|
|
243
|
+
|
|
244
|
+
See :ref:`ModbusServer Class Methods <modbus_server_methods>` for all base class inherited methods.
|
|
245
|
+
|
|
246
|
+
Method: modbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])
|
|
247
|
+
--------------------------------------------------------------
|
|
248
|
+
|
|
249
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive boolean value to read.
|
|
250
|
+
* **offset** <number>: A number with value's offset inside the buffer.
|
|
251
|
+
* **Return** <boolean>: value.
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
This method read a boolean value inside a buffer. The buffer's first byte store the 0-7 boolean values's offset. Example:
|
|
255
|
+
|
|
256
|
+
.. code-block:: javascript
|
|
257
|
+
|
|
258
|
+
modbusTcpServer.inputs[0] = 0x44 //first byte 0100 0100
|
|
259
|
+
modbusTcpServer.coils[1] = 0x55 //second byte 0101 0101
|
|
260
|
+
|
|
261
|
+
modbusTcpServer.getBoolFromBuffer(modbusTcpServer.inputs, 6) //return 1
|
|
262
|
+
modbusTcpServer.getBoolFromBuffer(modbusTcpServer.coils, 5) //return 0
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
Method: modbusTcpServer.getPdu(reqAduBuffer)
|
|
266
|
+
----------------------------------------------
|
|
267
|
+
|
|
268
|
+
* **reqAduBuffer** <Buffer>: adu buffer containing the header and pdu.
|
|
269
|
+
* **Return** <Buffer>: buffer with the pdu.
|
|
270
|
+
|
|
271
|
+
This method return the pdu part of a modbus tcp adu.
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
Method: modbusTcpServer.getMbapHeader(reqAduBuffer)
|
|
275
|
+
---------------------------------------------------
|
|
276
|
+
|
|
277
|
+
* **reqAduBuffer** <Buffer>: adu buffer containing the header and pdu.
|
|
278
|
+
* **Return** <Buffer>: buffer with the header.
|
|
279
|
+
|
|
280
|
+
This method return the header part of a modbus tcp adu.
|
|
281
|
+
|
|
282
|
+
Method: modbusTcpServer.getResponseAdu(reqAduBuffer)
|
|
283
|
+
----------------------------------------------------
|
|
284
|
+
|
|
285
|
+
* **reqAduBuffer** <Buffer>: adu buffer containing the header and pdu.
|
|
286
|
+
* **Return** <Buffer>: Response Adu in a buffer object.
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
This method is the main TCP server's method. It receives a Modbus TCP request as an argument, processes it, and returns a buffer with the response ready to be send.
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
Method: modbusTcpServer.getWordFromBuffer(targetBuffer, [offset])
|
|
293
|
+
--------------------------------------------------------------
|
|
294
|
+
|
|
295
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to read.
|
|
296
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
297
|
+
* **Return** <Buffer>: A two bytes length buffer.
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
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
|
|
301
|
+
|
|
302
|
+
.. code-block:: javascript
|
|
303
|
+
|
|
304
|
+
modbusTcpServer.holdingRegisters[0] = 0x11;
|
|
305
|
+
modbusTcpServer.holdingRegisters[1] = 0x22;
|
|
306
|
+
modbusTcpServer.holdingRegisters[2] = 0x33;
|
|
307
|
+
modbusTcpServer.holdingRegisters[3] = 0x44;
|
|
308
|
+
|
|
309
|
+
modbusTcpServer.holdingRegisters.readUInt16BE(0) //returns 0x1122
|
|
310
|
+
modbusTcpServer.holdingRegisters.readUInt16BE(1) //returns 0x2233
|
|
311
|
+
modbusTcpServer.getWordFromBuffer(modbusTcpServer.holdingRegisters, 0) //returns Buffer:[0x11, 0x22]
|
|
312
|
+
modbusTcpServer.getWordFromBuffer(modbusTcpServer.holdingRegisters, 1) //returns Buffer:[0x33, 0x44]
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
Method: modbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])
|
|
316
|
+
-------------------------------------------------------------------
|
|
317
|
+
|
|
318
|
+
* **value** <boolean>: Value to write.
|
|
319
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive boolean value to write.
|
|
320
|
+
* **offset** <number>: A number with value's offset inside the buffer.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
This method write a boolean value inside a buffer. The buffer's first byte store the 0-7 boolean values's offset. Example:
|
|
324
|
+
|
|
325
|
+
.. code-block:: javascript
|
|
326
|
+
|
|
327
|
+
modbusTcpServer.getBoolFromBuffer(true, modbusTcpServer.coils, 5)
|
|
328
|
+
console.log(modbusTcpServer.coils[1]) //now second byte is 0x75 (0111 0101)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
Method: modbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])
|
|
332
|
+
-------------------------------------------------------------------
|
|
333
|
+
|
|
334
|
+
* **value** <Buffer>: two bytes length buffer.
|
|
335
|
+
* **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to write.
|
|
336
|
+
* **offset** <number>: A number with register's offset inside the buffer.
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
This method write a 16 bits register inside a buffer. The offset is 16 bits aligned. Example:
|
|
341
|
+
|
|
342
|
+
.. code-block:: javascript
|
|
343
|
+
|
|
344
|
+
let realValue = Buffer.alloc(4);
|
|
345
|
+
realValue.writeFloatBE(3.14);
|
|
346
|
+
let register1 = realValue.subarray(0, 2);
|
|
347
|
+
let register2 = realValue.subarray(2, 4);
|
|
348
|
+
|
|
349
|
+
//writing pi value in bytes 2, 3, 4, 5
|
|
350
|
+
modbusTcpServer.setWordToBuffer(register1, modbusTcpServer.holdingRegisters, 1);
|
|
351
|
+
modbusTcpServer.setWordToBuffer(register2, modbusTcpServer.holdingRegisters, 2);
|
|
352
|
+
|
|
353
|
+
//instead this write pi value in bytes 1, 2, 3, 4
|
|
354
|
+
modbusTcpServer.holdingRegisters.writefloatBE(3.14, 1) //alignment problem
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
Method: modbusTcpServer.validateMbapHeader(mbapBuffer)
|
|
358
|
+
------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
* **mbapBuffer** <Buffer>: adu's header buffer.
|
|
361
|
+
* **Return** <boolean>: True if is a valid header otherwise false.
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
This method return tru if header's buffer has 7 bytes length and the protocol's field is 0.
|
|
365
|
+
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
.. _nodbus_net_server:
|
|
2
|
+
|
|
3
|
+
===========================
|
|
4
|
+
API: Net Server
|
|
5
|
+
===========================
|
|
6
|
+
|
|
7
|
+
**Nodbus-Plus v1.0 Documentation**
|
|
8
|
+
|
|
9
|
+
.. contents:: Table of Contents
|
|
10
|
+
:depth: 3
|
|
11
|
+
|
|
12
|
+
Nodbus implementation for a modbus TCP or serial servers use a netServer object to implement the network layer. This object can be one of the following types:
|
|
13
|
+
|
|
14
|
+
* **tcpServer**: A wrapper around node `net.Server <https://nodejs.org/api/net.html#class-netserver>`_.
|
|
15
|
+
|
|
16
|
+
* **udpserver**: A wrapper around node `dgram.Socket <https://nodejs.org/api/dgram.html#class-dgramsocket>`_.
|
|
17
|
+
|
|
18
|
+
* **serialServer**: A wrapper around node `serialport <https://serialport.io/>`_ .
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
Creating a Nodbus NetServer Instance
|
|
22
|
+
====================================
|
|
23
|
+
|
|
24
|
+
new NetServer([options])
|
|
25
|
+
-------------------------
|
|
26
|
+
|
|
27
|
+
* **options** <object>: Configuration object with the following properties (for tcp and udp net layer):
|
|
28
|
+
|
|
29
|
+
* port <number> : The tcp or udp port to listen. Default 502.
|
|
30
|
+
* maxConnections <number>: Max number of simultaneous connection supported. (Only tcp net server). Default 32.
|
|
31
|
+
* udpType <string>: Used in udp server to set 'udp4' or 'udp6'. Default 'udp6'.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
* **options** <object>: Configuration object with the following properties (for serial net layer):
|
|
35
|
+
|
|
36
|
+
* port <string> : The path to the serial port. Example 'COM1.
|
|
37
|
+
* speed <number>: Enum with following baudrates in bps : 0-110, 1-300, 2-1200, 3-2400, 4-4800, 5-9600, 6-14400, 7-19200, 8-38400, 9-57600, 10-115200. Default 7.
|
|
38
|
+
* dataBits <number>: 7 or 8. Default 8.
|
|
39
|
+
* stopBits <number>: 0 or 1.
|
|
40
|
+
* parity <number>: Enum with following value. 0-'none', 1-'even', 2-'odd'. Default 1.
|
|
41
|
+
* timeBetweenFrame <number>: Number of millisends to await without receiving data to consider end of modbus frame.
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
Constructor for new NetServer instance.
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
NetServer Event's Hooks
|
|
48
|
+
========================
|
|
49
|
+
|
|
50
|
+
The net server object is not a event emitter, instead it uses the core server events to call hooks functions.
|
|
51
|
+
|
|
52
|
+
onConnectionAcceptedHook
|
|
53
|
+
-------------------------
|
|
54
|
+
|
|
55
|
+
* **socket** <object>: socket created
|
|
56
|
+
|
|
57
|
+
This function is called when the core server emits the 'connection' event and the connection is accepted by the server.
|
|
58
|
+
|
|
59
|
+
onDataHook
|
|
60
|
+
-----------
|
|
61
|
+
|
|
62
|
+
* **socket** <object>: socket that emit the data event
|
|
63
|
+
* **data** <Buffer>: Data received.
|
|
64
|
+
|
|
65
|
+
This function is called when the core server emits the data event.
|
|
66
|
+
|
|
67
|
+
onErrorHook
|
|
68
|
+
-----------
|
|
69
|
+
|
|
70
|
+
* **e** <object>: error object.
|
|
71
|
+
|
|
72
|
+
This function is called when the core server emits the 'error' event.
|
|
73
|
+
|
|
74
|
+
onListeningHook
|
|
75
|
+
----------------
|
|
76
|
+
|
|
77
|
+
This function is called when the core server emit the 'listening' event. It is called with no arguments.
|
|
78
|
+
|
|
79
|
+
onMbAduHook
|
|
80
|
+
-------------
|
|
81
|
+
|
|
82
|
+
* **socket** <object>: socket that emit the data event
|
|
83
|
+
* **data** <Buffer>: Data received.
|
|
84
|
+
|
|
85
|
+
This hook function is similar to onDataHook, but is only called when the buffer received has been validated and has correct length for modbus tcp or correct checksum
|
|
86
|
+
for modbus serial.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
onServerCloseHook
|
|
90
|
+
------------------
|
|
91
|
+
|
|
92
|
+
This hook function is called when core server emits the 'close' event. It is called with no arguments.
|
|
93
|
+
|
|
94
|
+
onWriteHook
|
|
95
|
+
-----------
|
|
96
|
+
|
|
97
|
+
* **socket** <object>: socket that emit the data event
|
|
98
|
+
* **data** <Buffer>: Data sended to client.
|
|
99
|
+
|
|
100
|
+
This hook function is called when data has been sennded by server to a client. It is called when connection socket write some data.
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
NetServer's Atributes
|
|
104
|
+
=====================
|
|
105
|
+
|
|
106
|
+
Atribute: netServer.activeConnections
|
|
107
|
+
--------------------------------------------
|
|
108
|
+
|
|
109
|
+
* <array>: An array with active connections.
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
Atribute: netServer.coreServer
|
|
113
|
+
-------------------------------
|
|
114
|
+
|
|
115
|
+
* <object>
|
|
116
|
+
|
|
117
|
+
* **net.Server**: For tcp `node <https://nodejs.org/api/net.html#class-netserver>`_.
|
|
118
|
+
* **dgram.Socket**: For udp `node <https://nodejs.org/api/dgram.html#class-dgramsocket>`_.
|
|
119
|
+
* **SerialPort**: A wrapper around node `serialport <https://serialport.io/docs/api-serialport>`_ .
|
|
120
|
+
|
|
121
|
+
This property is a node net.Server in nodbus tcpServer class or node udp.Socket in nodbus udpServer or serialport from serialport library in nodbus serialServer.
|
|
122
|
+
The netServer class in Nodbus-Plus library is a wrapper around one of this main class.
|
|
123
|
+
|
|
124
|
+
Atribute: netServer.isListening
|
|
125
|
+
-------------------------------------
|
|
126
|
+
|
|
127
|
+
* <bool>
|
|
128
|
+
|
|
129
|
+
True if the coreServer is listening.
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
Atribute: netServer.maxConnections
|
|
133
|
+
-------------------------------------
|
|
134
|
+
|
|
135
|
+
* <number>
|
|
136
|
+
|
|
137
|
+
The max number of connection accepted in the tcpServer type of netServer. In udpServer has no efect.
|
|
138
|
+
|
|
139
|
+
Atribute: netServer.onConnectionAcceptedHook
|
|
140
|
+
----------------------------------------------
|
|
141
|
+
|
|
142
|
+
* <function>
|
|
143
|
+
|
|
144
|
+
This property is a reference for a hook function. See :ref:`onConnectionAcceptedHook`
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
Atribute: netServer.onDataHook
|
|
148
|
+
----------------------------------
|
|
149
|
+
|
|
150
|
+
* <function>
|
|
151
|
+
|
|
152
|
+
This property is a reference for a hook function. See :ref:`onDataHook`
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
Atribute: netServer.onErrorHook
|
|
156
|
+
----------------------------------
|
|
157
|
+
|
|
158
|
+
* <function>
|
|
159
|
+
|
|
160
|
+
This property is a reference for a hook function. See :ref:`onErrorHook`
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
Atribute: netServer.onListeningHook
|
|
164
|
+
------------------------------------
|
|
165
|
+
|
|
166
|
+
* <function>
|
|
167
|
+
|
|
168
|
+
This property is a reference for a hook function. See :ref:`onListeningHook`
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
Atribute: netServer.onMbAduHook
|
|
172
|
+
----------------------------------
|
|
173
|
+
|
|
174
|
+
* <function>
|
|
175
|
+
|
|
176
|
+
This property is a reference for a hook function. See :ref:`onMbAduHook`
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
Atribute: netServer.onServerCloseHook
|
|
180
|
+
--------------------------------------
|
|
181
|
+
|
|
182
|
+
* <function>
|
|
183
|
+
|
|
184
|
+
This property is a reference for a hook function. See :ref:`onServerCloseHook`
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
Atribute: netServer.onWriteHook
|
|
188
|
+
----------------------------------
|
|
189
|
+
|
|
190
|
+
* <function>
|
|
191
|
+
|
|
192
|
+
This property is a reference for a hook function. See :ref:`onWriteHook`
|
|
193
|
+
|
|
194
|
+
Atribute: netServer.port
|
|
195
|
+
-----------------------------
|
|
196
|
+
|
|
197
|
+
* <number>
|
|
198
|
+
|
|
199
|
+
Port to listen to.
|
|
200
|
+
|
|
201
|
+
Atribute: netServer.tcpCoalescingDetection
|
|
202
|
+
--------------------------------------------
|
|
203
|
+
|
|
204
|
+
* <boolean>
|
|
205
|
+
|
|
206
|
+
Activate o deactivate the tcp coalscing detection function for modbus tcp protocol. Default false.
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
Atribute: netServer.validateFrame
|
|
210
|
+
----------------------------------
|
|
211
|
+
|
|
212
|
+
* <function>
|
|
213
|
+
|
|
214
|
+
This property is a reference to a function that performs validation.
|
|
215
|
+
It defines how the nodbus server executes certain protocols for validating data at the network layer level.
|
|
216
|
+
|
|
217
|
+
It is called with a Buffer as argument with the modbus frame received.
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
NetServer's Methods
|
|
221
|
+
====================
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
Method: netServer.start()
|
|
225
|
+
-------------------------------
|
|
226
|
+
|
|
227
|
+
This method start the server.
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
Method: netServer.stop()
|
|
231
|
+
-----------------------------
|
|
232
|
+
|
|
233
|
+
This functions stop the server. No further connection are accepted.
|
|
234
|
+
|
|
235
|
+
Method: netServer.write(socket, frame)
|
|
236
|
+
-------------------------------------------------
|
|
237
|
+
|
|
238
|
+
* **socket** <object>: buffer containig the pdu's data.
|
|
239
|
+
* **frame** <Buffer>: buffer with response pdu.
|
|
240
|
+
|
|
241
|
+
function to write data to a client. It takes a srteam object and a buffer to wrie to. When data has been send, the function calls onWriteHook funtion.
|
|
242
|
+
|