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.
Files changed (101) hide show
  1. package/.readthedocs.yaml +35 -0
  2. package/LICENSE.md +21 -21
  3. package/README.md +163 -48
  4. package/docs/Makefile +20 -0
  5. package/docs/client/channel.rst +209 -0
  6. package/docs/client/nodbus_master_serial.rst +559 -0
  7. package/docs/client/nodbus_master_tcp.rst +573 -0
  8. package/docs/conf.py +41 -0
  9. package/docs/images/01-readcoils.png +0 -0
  10. package/docs/images/02-readinputs.png +0 -0
  11. package/docs/images/03-readholding.png +0 -0
  12. package/docs/images/04-readinputsreg.png +0 -0
  13. package/docs/images/05-writecoil.png +0 -0
  14. package/docs/images/06-writeregister.png +0 -0
  15. package/docs/images/15-writecoil.png +0 -0
  16. package/docs/images/16.png +0 -0
  17. package/docs/images/22-mask.png +0 -0
  18. package/docs/images/23.png +0 -0
  19. package/docs/images/7.png +0 -0
  20. package/docs/images/modbus_pdu.png +0 -0
  21. package/docs/images/serial_adu.png +0 -0
  22. package/docs/images/tcp_adu.png +0 -0
  23. package/docs/index.rst +30 -0
  24. package/docs/make.bat +35 -0
  25. package/docs/protocol/modbus_master.rst +276 -0
  26. package/docs/protocol/modbus_master_serial.rst +290 -0
  27. package/docs/protocol/modbus_master_tcp.rst +296 -0
  28. package/docs/protocol/modbus_server.rst +469 -0
  29. package/docs/protocol/modbus_server_serial.rst +543 -0
  30. package/docs/protocol/modbus_server_tcp.rst +365 -0
  31. package/docs/requirements.txt +4 -0
  32. package/docs/server/net_server.rst +242 -0
  33. package/docs/server/nodbus_serial_server.rst +652 -0
  34. package/docs/server/nodbus_tcp_server.rst +505 -0
  35. package/docs/starting.rst +192 -0
  36. package/docs/static/simple logo.jpg +0 -0
  37. package/package.json +39 -30
  38. package/samples/mb_serial_server.js +77 -0
  39. package/samples/mb_tcp_client.js +114 -0
  40. package/samples/mb_tcp_server.js +58 -0
  41. package/src/client/net/serialchannel.js +195 -0
  42. package/src/client/net/tcpchannel.js +233 -0
  43. package/src/client/net/udpchannel.js +243 -0
  44. package/src/client/nodbus_serial_client.js +577 -0
  45. package/src/client/nodbus_tcp_client.js +542 -0
  46. package/src/nodbus-plus.js +157 -110
  47. package/src/protocol/modbus_master.js +298 -961
  48. package/src/protocol/modbus_master_serial.js +247 -0
  49. package/src/protocol/modbus_master_tcp.js +219 -0
  50. package/src/protocol/modbus_server.js +936 -0
  51. package/src/protocol/modbus_server_serial.js +368 -0
  52. package/src/protocol/modbus_server_tcp.js +129 -0
  53. package/src/protocol/utils.js +296 -0
  54. package/src/server/net/serialserver.js +184 -0
  55. package/src/server/net/tcpserver.js +290 -0
  56. package/src/server/net/udpserver.js +242 -0
  57. package/src/server/nodbus_serial_server.js +238 -0
  58. package/src/server/nodbus_tcp_server.js +249 -0
  59. package/test/modbus_master.test.js +279 -0
  60. package/test/modbus_master_serial.test.js +124 -0
  61. package/test/modbus_master_tcp.test.js +178 -0
  62. package/test/modbus_server.test.js +506 -0
  63. package/test/modbus_server_serial.test.js +328 -0
  64. package/test/modbus_server_tcp.test.js +91 -0
  65. package/test/nodbus_client_serial.test.js +307 -0
  66. package/test/nodbus_client_tcp.test.js +334 -0
  67. package/test/nodbus_server_serial.test.js +255 -0
  68. package/test/nodbus_server_tcp.test.js +216 -0
  69. package/CHANGELOG.md +0 -27
  70. package/src/client/m_stcp_client.js +0 -214
  71. package/src/client/m_tcp_client.js +0 -234
  72. package/src/net/tcpclient.js +0 -173
  73. package/src/net/tcpserver.js +0 -329
  74. package/src/protocol/adu.js +0 -40
  75. package/src/protocol/ascii_adu.js +0 -139
  76. package/src/protocol/boolean_register.js +0 -78
  77. package/src/protocol/functions/Force_Multiple_Coils.js +0 -76
  78. package/src/protocol/functions/Force_Single_Coil.js +0 -54
  79. package/src/protocol/functions/Mask_Holding_Register.js +0 -47
  80. package/src/protocol/functions/Preset_Multiple_Registers.js +0 -53
  81. package/src/protocol/functions/Preset_Single_Register.js +0 -39
  82. package/src/protocol/functions/Read_Coil_Status.js +0 -59
  83. package/src/protocol/functions/Read_Holding_Registers.js +0 -52
  84. package/src/protocol/functions/Read_Input_Registers.js +0 -52
  85. package/src/protocol/functions/Read_Input_Status.js +0 -58
  86. package/src/protocol/mbap.js +0 -60
  87. package/src/protocol/modbus_device.js +0 -35
  88. package/src/protocol/modbus_slave.js +0 -522
  89. package/src/protocol/pdu.js +0 -70
  90. package/src/protocol/rtu_adu.js +0 -122
  91. package/src/protocol/serial_adu.js +0 -29
  92. package/src/protocol/tcp_adu.js +0 -84
  93. package/src/protocol/word_register.js +0 -122
  94. package/src/server/m_stcp_server.js +0 -310
  95. package/src/server/m_tcp_server.js +0 -295
  96. package/test/modbus-stcp-server-test.js +0 -72
  97. package/test/modbus-stcp-server-test1.js +0 -72
  98. package/test/modbus-tcp-client-test.js +0 -159
  99. package/test/modbus-tcp-server-test.js +0 -75
  100. package/test/modbus-tcp-server-test2.js +0 -75
  101. package/test/modbus_stcp_client.js +0 -149
@@ -0,0 +1,505 @@
1
+ .. _nodbus_tcp_server:
2
+
3
+ ===========================
4
+ Class: NodbusTcpServer
5
+ ===========================
6
+
7
+ **Nodbus-Plus v1.0 Documentation**
8
+
9
+ .. contents:: Table of Contents
10
+ :depth: 3
11
+
12
+
13
+
14
+ The NodbusTcpServer class extends the :ref:`ModbusTcpServer Class <modbus_tcp_server>`. This class implements a fully funcional modbus tcp server.
15
+
16
+ Creating a NodbusTcpServer Instance
17
+ ====================================
18
+
19
+ new nodbusTcpServer([netType], [options])
20
+ ------------------------------------------
21
+
22
+ * **netType** <Class>: This argument define the constructor for the net layer. See :ref:`NetServer Class <nodbus_net_server>`
23
+
24
+ * **options** <object>: Configuration object with following properties:
25
+
26
+ * 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.
27
+
28
+ * 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.
29
+
30
+ * holdingRegisters <number>: The cuantity of holding registers that the server will have. It's an integer between 1 and 65535. Default value is 2048.
31
+
32
+ * inputRegisters <number>: The cuantity of input registers that the server will have. It's an integer between 1 and 65535. Default value is 2048.
33
+
34
+ * port <number>: TCP port on which the server will listen. Default 502
35
+
36
+ * maxConnections <number>: Simultaneous conextions allowed by the server. Default 32.
37
+
38
+ * udpType <string>: Define the type of udp socket id udp net type is configured. Can take two values 'ud4' and 'usp6'. Default 'udp4'.
39
+
40
+ * **Returns:** <NodbusTcpServer>
41
+
42
+ NodbusPlus expose the function createTcpServer([netConstructor], [options]) to create new instances for NodbusTcpClass.
43
+
44
+ .. code-block:: javascript
45
+
46
+ const nodbus = require('nodbus-plus');
47
+ let nodbusTcpServer = nodbus.createTcpServer('tcp'); //default settings, net layer is tcp
48
+
49
+ let config = {
50
+ port:1502
51
+ }
52
+ // modbus tcp server listen to port 1502 and udp6
53
+ let nodbusTcpServer2 = nodbus.createTcpServer('udp6', config);
54
+ //or udp version 4
55
+ let nodbusTcpServer3 = nodbus.createTcpServer('udp4', config);
56
+
57
+ However new NodbusTcpServer instance can be created with customs :ref:`NetServer <nodbus_net_server>` importing the NodbusTcpServer Class.
58
+
59
+ .. code-block:: javascript
60
+
61
+ const NodbusTcpServer = require('nodbus-plus').NodbusTcpServer;
62
+ const NetServer = require('custom\net\custome_server.js'); //this is a example file for a user net server, it do not exist on nodbus-plus library
63
+
64
+ let config = {};
65
+ let nodbusTcpServer = new NodbusTcpServer(NetServer, config);
66
+
67
+
68
+
69
+ NodbusTcpServer's Events
70
+ =========================
71
+
72
+
73
+ Event: 'closed'
74
+ ----------------
75
+
76
+ Emitted when the server is closed.
77
+
78
+
79
+ Event: 'connection'
80
+ -------------------
81
+
82
+ * **socket** <Object>: A node `net.Socket <https://nodejs.org/api/net.html#class-netsocket>`_
83
+
84
+ Emitted when a client connect. Only emmited when 'tcp' type layer is used.
85
+
86
+
87
+ Event: 'connection-closed'
88
+ ---------------------------
89
+
90
+ Emitted when the client's socket is closed and destroyed.
91
+
92
+
93
+ Event: 'error'
94
+ --------------
95
+
96
+ * **e** <Error>: The error object.
97
+
98
+ Emitted when a error occurs.
99
+
100
+
101
+ Event: 'data'
102
+ ---------------------
103
+
104
+ * **socket** <object>: Can be a node `net.Socket <https://nodejs.org/api/net.html#class-netsocket>`_
105
+ if tcp is used or datagram `message rinfo <https://nodejs.org/api/dgram.html#event-message>`_.
106
+ * **data** <Buffer>: Data received.
107
+
108
+ Emitted when the underlaying net server emit the data event.
109
+
110
+
111
+ Event: 'listening'
112
+ ------------------
113
+
114
+ * **port** <number>: TCP port on which the server is listening.
115
+
116
+ Emitted when the server is listening.
117
+
118
+
119
+ Event: 'exception'
120
+ ---------------------
121
+
122
+ * **functionCode** <number>: request function code.
123
+ * **exceptionCode** <number>: the code of exception
124
+ * **name** <string>: Name of exception.
125
+
126
+ .. raw:: html
127
+
128
+ <table>
129
+ <tr>
130
+ <th>Code</th>
131
+ <th>Name</th>
132
+ <th>Meaning</th>
133
+ </tr>
134
+ <tr>
135
+ <td>01</td>
136
+ <td>ILLEGAL FUNCTION</td>
137
+ <td>The function code received in the query is not an allowable action for the server.</td>
138
+ </tr>
139
+ <tr>
140
+ <td>02</td>
141
+ <td>ILLEGAL DATA ADDRESS</td>
142
+ <td>The data address received in the query is not an allowable address for the server.</td>
143
+ </tr>
144
+ <tr>
145
+ <td>03</td>
146
+ <td>ILLEGAL DATA VALUE</td>
147
+ <td>A value contained in the query data field is not an allowable value for server</td>
148
+ </tr>
149
+ <tr>
150
+ <td>04</td>
151
+ <td>SLAVE DEVICE FAILURE</td>
152
+ <td>An unrecoverable error occurred while the server was attempting to perform the requested action.</td>
153
+ </tr>
154
+ <tr>
155
+ <td>05</td>
156
+ <td>ACKNOWLEDGE</td>
157
+ <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.
158
+ This response is returned to prevent a timeout error from occurringin the client (or master).</td>
159
+ </tr>
160
+ <tr>
161
+ <td>06</td>
162
+ <td>SLAVE DEVICE BUSY</td>
163
+ <td>Specialized use in conjunction with programming commands. The server (or slave) is engaged in processing a long–duration program command.</td>
164
+ </tr>
165
+ <tr>
166
+ <td>08</td>
167
+ <td>MEMORY PARITY ERROR</td>
168
+ <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>
169
+ </tr>
170
+ <tr>
171
+ <td>0A</td>
172
+ <td>GATEWAY PATH UNAVAILABLE</td>
173
+ <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.
174
+ Usually means that the gateway is misconfigured or overloaded.</td>
175
+ </tr>
176
+ <tr>
177
+ <td>0B</td>
178
+ <td>GATEWAY TARGET DEVICE FAILED TO RESPOND</td>
179
+ <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>
180
+ </tr>
181
+ </table>
182
+
183
+ Emitted when a Modbus exception occurs.
184
+
185
+
186
+ Event: 'request'
187
+ ----------------
188
+
189
+ * **socket** <object>: Can be a node `net.Socket <https://nodejs.org/api/net.html#class-netsocket>`_
190
+ if tcp is used or datagram `message rinfo <https://nodejs.org/api/dgram.html#event-message>`_.
191
+ * **request** <object>: A with following properties:
192
+
193
+ * *timeStamp* <number>: A timestamp for the request.
194
+
195
+ * *transactionId* <number>: The header's transaction id field value.
196
+
197
+ * *unitId* <number>: The header's unit id field value.
198
+
199
+ * *functionCode* <number>: The modbus request's function code.
200
+
201
+ * *data* <Buffer>: The pdu's data.
202
+
203
+ Emited after the data event and only if the data had been validate at net layer level (data's length greater than 7 and equal to header's length field plus 6).
204
+
205
+
206
+ Event: 'response'
207
+ ----------------
208
+
209
+ * **socket** <object>: Can be a node `net.Socket <https://nodejs.org/api/net.html#class-netsocket>`_ if tcp is used or datagram `message rinfo <https://nodejs.org/api/dgram.html#event-message>`_.
210
+
211
+ * **response** <object>: A with following properties:
212
+
213
+ * *timeStamp* <number>: A timestamp for the request.
214
+
215
+ * *transactionId* <number>: The header's transaction id field value.
216
+
217
+ * *unitId* <number>: The header's unit id field value.
218
+
219
+ * *functionCode* <number>: The modbus request's function code.
220
+
221
+ * *data* <Buffer>: The pdu's data.
222
+
223
+ Emited before to send the response adu's buffer to the socket to be sended.
224
+
225
+
226
+ Event: 'write'
227
+ ---------------------
228
+
229
+ * **socket** <object>: Can be a node `net.Socket <https://nodejs.org/api/net.html#class-netsocket>`_ if tcp is used or datagram `message rinfo <https://nodejs.org/api/dgram.html#event-message>`_.
230
+ * **res** <Buffer>: Server's response.
231
+
232
+ Emitted when the underlaying net server write data to the socket.
233
+
234
+
235
+ Event: 'write-coils'
236
+ --------------
237
+
238
+ * **startCoil** <number> Indicate in wich coil start the new value.
239
+ * **cuantityOfCoils** <number>: amound of coils modificated
240
+
241
+ Emitted after change a coil value due to a clienst write coil request.
242
+
243
+
244
+ Event: 'write-registers'
245
+ --------------
246
+
247
+ * **startRegister** <number> Indicate in wich register start the new value.
248
+ * **cuantityOfRegister** <number>: amound of register modificated.
249
+
250
+ Emitted after change a holding register value due to a clienst write register request.
251
+
252
+
253
+ NodbusTcpServer's Atributes
254
+ ===========================
255
+
256
+
257
+ Atribute: nodbusTcpServer._internalFunctionCode
258
+ --------------------------------------------
259
+
260
+ * <Map>
261
+
262
+ This property stores the Modbus functions codes supported by the server.
263
+ 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.
264
+
265
+ .. code-block:: javascript
266
+
267
+ //Example of how to add new custom modbus function code handle function
268
+ class NodbusTcpServerExtended extends NodbusTcpServer{
269
+ constructor(mbServerCfg){
270
+ super(mbServerCfg)
271
+ //adding the new function code and the name of handler
272
+ this._internalFunctionCode.set(68, 'customService68');
273
+ }
274
+ //New method to handle function code 68. receive a buffer with pdu data as argument.
275
+ customService68(pduReqData){
276
+ let resp = Buffer.alloc(2);
277
+ resp[0] = 68;
278
+ resp[1] = pduReqData[0];
279
+ return resp
280
+ }
281
+ }
282
+
283
+
284
+
285
+ Atribute: nodbusTcpServer.coils
286
+ -------------------------------
287
+
288
+ * <Buffer>
289
+
290
+ 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.
291
+
292
+ To read and write digital values to the buffer, the modbus server provides the methods :ref:`getBoolFromBuffer <Method: nodbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])>`
293
+ and :ref:`setBooltoBuffer method <Method: nodbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])>`.
294
+
295
+
296
+
297
+ Atribute: nodbusTcpServer.holdingRegisters
298
+ -------------------------------------------
299
+
300
+ * <Buffer>
301
+
302
+ This property is a Buffer that store the servers' holding registers.
303
+ 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.
304
+ This means that byte[0] of the register will be considered the MSB and byte[1] the LSB.
305
+
306
+ 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.
307
+
308
+ 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
309
+ :ref:`getWordFromBuffer method <Method: nodbusTcpServer.getWordFromBuffer(targetBuffer, [offset])>` and
310
+ the :ref:`setWordtoBuffer method <Method: nodbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])>`.
311
+
312
+
313
+ Atribute: nodbusTcpServer.inputRegisters
314
+ ------------------------------------------
315
+
316
+ * <Buffer>
317
+
318
+ This property is a Buffer that store the servers' input registers.
319
+ 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.
320
+ This means that byte[0] of the register will be considered the MSB and byte[1] the LSB.
321
+
322
+ 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.
323
+
324
+ 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
325
+ :ref:`getWordFromBuffer method <Method: nodbusTcpServer.getWordFromBuffer(targetBuffer, [offset])>` and
326
+ the :ref:`setWordtoBuffer method <Method: nodbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])>`.
327
+
328
+
329
+ Atribute: nodbusTcpServer.inputs
330
+ ---------------------------------
331
+
332
+ * <Buffer>
333
+
334
+ 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.
335
+
336
+ To read and write digital values to the buffer, the modbus server provides the methods :ref:`getBoolFromBuffer <Method: nodbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])>`
337
+ and :ref:`setBooltoBuffer method <Method: nodbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])>`.
338
+
339
+
340
+
341
+ Atribute: nodbusServer.isListening
342
+ --------------------------------------------
343
+
344
+ * <boolean>
345
+
346
+ A getter that return the listening status.
347
+
348
+
349
+ Atribute: nodbusTcpServer.net
350
+ --------------------------------------------
351
+
352
+ * <Object>
353
+
354
+ A instance of a NetServer Class. See :ref:`NetServer Class <nodbus_net_server>`.
355
+
356
+
357
+ Atribute: nodbusTcpServer.maxConnections
358
+ --------------------------------------------
359
+
360
+ * <number>
361
+
362
+ Max number of simultaneous connections allowed by the server.
363
+
364
+
365
+ Atribute: nodbusTcpServer.port
366
+ --------------------------------------------
367
+
368
+ * <number>
369
+
370
+ TCP port on which the server will listen.
371
+
372
+ Atribute: nodbusTcpServer.supportedFunctionCode
373
+ ------------------------------------------------
374
+
375
+ * <iterator>
376
+
377
+ This is a getter that return an iterator object trhough nodbusTcpServer._internalFunctionCode keys. It's the same that call nodbusTcpServer._internalFunctionCode.keys().
378
+
379
+ .. code-block:: javascript
380
+
381
+ //Example of getting all suported function code.
382
+ for(const functionCode of nodbusTcpServer.supportedFunctionCode){
383
+ console.log(functionCode)
384
+ }
385
+
386
+
387
+
388
+ NodbusTcpServer's Methods
389
+ =========================
390
+
391
+ See :ref:`ModbusTcpServer Class Methods <modbus_tcp_server_methods>` for all base class inherited methods.
392
+
393
+ Method: nodbusTcpServer.getBoolFromBuffer(targetBuffer, [offset])
394
+ --------------------------------------------------------------
395
+
396
+ * **targetBuffer** <Buffer>: Buffer with the objetive boolean value to read.
397
+ * **offset** <number>: A number with value's offset inside the buffer.
398
+ * **Return** <boolean>: value.
399
+
400
+
401
+ This method read a boolean value inside a buffer. The buffer's first byte store the 0-7 boolean values's offset. Example:
402
+
403
+ .. code-block:: javascript
404
+
405
+ nodbusTcpServer.inputs[0] = 0x44 //first byte 0100 0100
406
+ nodbusTcpServer.coils[1] = 0x55 //second byte 0101 0101
407
+
408
+ nodbusTcpServer.getBoolFromBuffer(nodbusTcpServer.inputs, 6) //return 1
409
+ nodbusTcpServer.getBoolFromBuffer(nodbusTcpServer.coils, 5) //return 0
410
+
411
+
412
+ Method: nodbusTcpServer.getPdu(reqAduBuffer)
413
+ ----------------------------------------------
414
+
415
+ * **reqAduBuffer** <Buffer>: adu buffer containing the header and pdu.
416
+ * **Return** <Buffer>: buffer with the pdu.
417
+
418
+ This method return the pdu part of a modbus tcp adu.
419
+
420
+
421
+ Method: nodbusTcpServer.getMbapHeader(reqAduBuffer)
422
+ ---------------------------------------------------
423
+
424
+ * **reqAduBuffer** <Buffer>: adu buffer containing the header and pdu.
425
+ * **Return** <Buffer>: buffer with the header.
426
+
427
+ This method return the header part of a modbus tcp adu.
428
+
429
+
430
+ Method: nodbusTcpServer.getWordFromBuffer(targetBuffer, [offset])
431
+ --------------------------------------------------------------
432
+
433
+ * **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to read.
434
+ * **offset** <number>: A number with register's offset inside the buffer.
435
+ * **Return** <Buffer>: A two bytes length buffer.
436
+
437
+
438
+ 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
439
+
440
+ .. code-block:: javascript
441
+
442
+ nodbusTcpServer.holdingRegisters[0] = 0x11;
443
+ nodbusTcpServer.holdingRegisters[1] = 0x22;
444
+ nodbusTcpServer.holdingRegisters[2] = 0x33;
445
+ nodbusTcpServer.holdingRegisters[3] = 0x44;
446
+
447
+ nodbusTcpServer.holdingRegisters.readUInt16BE(0) //returns 0x1122
448
+ nodbusTcpServer.holdingRegisters.readUInt16BE(1) //returns 0x2233
449
+ nodbusTcpServer.getWordFromBuffer(nodbusTcpServer.holdingRegisters, 0) //returns Buffer:[0x11, 0x22]
450
+ nodbusTcpServer.getWordFromBuffer(nodbusTcpServer.holdingRegisters, 1) //returns Buffer:[0x33, 0x44]
451
+
452
+
453
+ Method: nodbusTcpServer.setBoolToBuffer(value, targetBuffer, [offset])
454
+ -------------------------------------------------------------------
455
+
456
+ * **value** <boolean>: Value to write.
457
+ * **targetBuffer** <Buffer>: Buffer with the objetive boolean value to write.
458
+ * **offset** <number>: A number with value's offset inside the buffer.
459
+
460
+
461
+ This method write a boolean value inside a buffer. The buffer's first byte store the 0-7 boolean values's offset. Example:
462
+
463
+ .. code-block:: javascript
464
+
465
+ nodbusTcpServer.getBoolFromBuffer(true, nodbusTcpServer.coils, 5)
466
+ console.log(nodbusTcpServer.coils[1]) //now second byte is 0x75 (0111 0101)
467
+
468
+
469
+ Method: nodbusTcpServer.start()
470
+ ------------------------------------------------
471
+
472
+ Start the server. The server will emit the event 'listening' whhen is ready for accept connections.
473
+
474
+
475
+ Method: nodbusTcpServer.setWordToBuffer(value, targetBuffer, [offset])
476
+ -------------------------------------------------------------------
477
+
478
+ * **value** <Buffer>: two bytes length buffer.
479
+ * **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to write.
480
+ * **offset** <number>: A number with register's offset inside the buffer.
481
+
482
+
483
+
484
+ This method write a 16 bits register inside a buffer. The offset is 16 bits aligned. Example:
485
+
486
+ .. code-block:: javascript
487
+
488
+ let realValue = Buffer.alloc(4);
489
+ realValue.writeFloatBE(3.14);
490
+ let register1 = realValue.subarray(0, 2);
491
+ let register2 = realValue.subarray(2, 4);
492
+
493
+ //writing pi value in bytes 2, 3, 4, 5
494
+ nodbusTcpServer.setWordToBuffer(register1, nodbusTcpServer.holdingRegisters, 1);
495
+ nodbusTcpServer.setWordToBuffer(register2, nodbusTcpServer.holdingRegisters, 2);
496
+
497
+ //instead this write pi value in bytes 1, 2, 3, 4
498
+ nodbusTcpServer.holdingRegisters.writefloatBE(3.14, 1) //alignment problem
499
+
500
+
501
+
502
+ Method: nodbusTcpServer.stop()
503
+ ------------------------------------------------
504
+
505
+ Stop the server. The server will emit the event 'closed' when all connection are destroyed.
@@ -0,0 +1,192 @@
1
+ .. _gstarting:
2
+
3
+ Getting Starting
4
+ =================
5
+
6
+ Nodbus Plus has been designed as a stack for the Modbus protocol.
7
+ It can be used to create both your own Modbus client and server using its API,
8
+ or you can use its build in client or server and have an application with Modbus communication in just minutes.
9
+
10
+ Instalation
11
+ ------------
12
+
13
+ Nodbus Plus can be easily installed using npm.
14
+
15
+ .. code-block:: console
16
+
17
+ $ npm install nodbus-plus
18
+
19
+ Basic Usage and Tutorial
20
+ -------------------------
21
+
22
+ In the "samples" folder, you will find three example programs for creating a Modbus TCP or serial server and a Modbus serial client.
23
+ First Nodbus-Plus must be inported:
24
+
25
+ .. code-block:: javascript
26
+
27
+ const nodbus = require('nodbus-plus');
28
+
29
+
30
+ * **Server**
31
+
32
+
33
+ Then the configuration object mus be created for server constructor and the function createTcpServer must be called.
34
+
35
+ .. code-block:: javascript
36
+
37
+ //Basic config for tcp server. Default values.
38
+ const cfg = {
39
+ inputs : 2048, //total inputs
40
+ coils : 2048, //total coils
41
+ holdingRegisters : 2048, //total holding register
42
+ inputRegisters : 2048, //total input register
43
+ port : 502, //port to listen to
44
+ }
45
+
46
+ let server = nodbus.createTcpServer('tcp', cfg);
47
+
48
+ The first argument is the type of transport layer used. The nodbus-plus tcp server suport 'tcp', 'udp4' and 'udp6'. To create a
49
+ serial server the procedure is the same, but calling createSerialServer function instead.
50
+
51
+ .. code-block:: javascript
52
+
53
+ //Basic config for tcp server. Default values.
54
+ let cfg = {
55
+ address : 1,
56
+ transmitionMode: 0, //0-rtu, 1 - ascii
57
+ inputs : 2048,
58
+ coils : 2048,
59
+ holdingRegisters : 2048,
60
+ inputRegisters : 2048,
61
+ port : 502,
62
+ }
63
+
64
+ let server = nodbus.createSerialServer('tcp', cfg);
65
+
66
+ The createSerialServer function can take the value 'serial' as fist argument as well, however the property port mus be a string
67
+ with the path to a serial port and adicional port configuration property may be needed. See nodbus-plus api for more details.
68
+
69
+ After instantiate the server, the user's app must add listener for server's event. Example of basic events
70
+
71
+ .. code-block:: javascript
72
+
73
+ //listenning event
74
+ server.on('listening', function(port){
75
+ console.log('Server listening on: ' + port);
76
+ });
77
+
78
+ //event emited when a request are received
79
+ server.on('request', function(sock, req){
80
+ console.log('Request received')
81
+ console.log(req)
82
+ });
83
+
84
+ //Event emited when server send a response to client
85
+ server.on('response', function(sock, res){
86
+ console.log('Responding')
87
+ console.log(res)
88
+ });
89
+
90
+ server.on('error', function(err){
91
+ console.log(err)
92
+ });
93
+
94
+
95
+ A complete list of available events can be found on :ref:`Nodbus Server documentation <nodbus_serial_server>`.
96
+
97
+ All the nodbus servers suports the following function codes:
98
+
99
+ * 01-Read Coils Status
100
+ * 02-Read Inputs Status
101
+ * 03-Read holding registers
102
+ * 04-Read Inputs registers
103
+ * 05-Force Single Coil
104
+ * 06-Preset Single Register
105
+ * 15-Force Multiple Coils
106
+ * 16-Force Multiples Registes
107
+ * 22-Mask Register
108
+ * 23-Read and Write Multiples Registers
109
+
110
+ Additionally a serial server also suports function code 07 Read Exception Coils. Se api :ref:`ModbusSerialServer documentation <modbus_serial_server>` for more details,
111
+
112
+ Finally the server must be started.
113
+
114
+ .. code-block:: javascript
115
+
116
+ server.start();
117
+
118
+
119
+ * **Client**
120
+
121
+
122
+ To create a modbus client the functions createTcpClient or createSerialClient are available.
123
+
124
+ .. code-block:: javascript
125
+
126
+ let client = nodbus.createSerialClient();
127
+
128
+ After instantiate the client, the user's app must add listener for client's event. Example of basic events.
129
+
130
+ .. code-block:: javascript
131
+
132
+ //emitted when the client stablish connection with the server
133
+ client.on('connection', (id)=>{
134
+ console.log('connection stablish')
135
+ })
136
+
137
+ //emited when error occurs
138
+ client.on('error', (e)=>{
139
+ console.log(e)
140
+ })
141
+
142
+ //emitted when a request is sended to server
143
+ client.on('request', (id, req)=>{
144
+ console.log('request sended to device: ' + id);
145
+ })
146
+
147
+ //emited when no response is received
148
+ client.on('req-timeout', (id, adu)=>{
149
+ console.log('timeout')
150
+ })
151
+
152
+ //emited when a response is received
153
+ client.on('response', (id, res)=>{
154
+ console.log(res)
155
+ })
156
+
157
+ A complete list of available events can be found on :ref:`Nodbus Client documentation <nodbus_serial_master>`.
158
+
159
+ Then channels must be add to the client. The client will create a connection per channel. The following example add a modbus serial over tcp server, and conect to it.
160
+
161
+
162
+ .. code-block:: javascript
163
+
164
+ //channel
165
+ channelCfg = {
166
+ ip:'127.0.0.1',
167
+ port:502,
168
+ timeout:250,
169
+ }
170
+
171
+ client.addChannel('device', 'tcp1', channelCfg);
172
+ client.connect('device')
173
+
174
+
175
+ The function addChannel take three arguments, the first one is channel's id, the seconds is channel's type. Three types are build in 'tcp1', 'udp1', and 'serial1'.
176
+ See Api docs for more details.
177
+
178
+ Once the client is connected, and event listener configured, data can be exchange using availables modbus function.
179
+
180
+ .. code-block:: javascript
181
+
182
+ //reading from cannel 'device', modbus address 1, two coils from 0 coil's address
183
+ client.readCoils('device', 1, 0, 2);
184
+
185
+ A list for all available functions can be found on Clients Documentation API. See :ref:`NodbusSerialClient <nodbus_serial_master>` or :ref:`NodbusTcpClient <nodbus_tcp_master>`.
186
+
187
+ Conclusion
188
+ ----------
189
+
190
+ We hope that this help has provided you with the necessary information to start using Nodbus-Plus. We look forward to receiving your feedback and contributions in the future.
191
+
192
+ Thank you for choosing Nodbus-Plus, and happy development!
Binary file