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,296 @@
1
+ .. _modbus_tcp_master:
2
+
3
+ ======================
4
+ Class: ModbusTcpClient
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 TCP Aplication Data Units (ADU).
13
+
14
+ .. Figure:: /images/tcp_adu.png
15
+
16
+ *Modbus Tcp Aplication Data Unis*
17
+
18
+ Creating a ModbusTcpClient Instance
19
+ ===================================
20
+
21
+ new ModbusTcpClient()
22
+ ---------------------
23
+
24
+ * **Returns:** <ModbusTcpClient>
25
+
26
+ Constructor for new ModbusTcpClient instance.
27
+
28
+ .. code-block:: javascript
29
+
30
+ const ModbusTcpClient = require('nodbus-plus').ModbusTcpClient;
31
+ let modbusTcpClient = new ModbusTcpClient();
32
+
33
+
34
+ ModbusTcpClient's Events
35
+ ========================
36
+
37
+ Event: 'req-timeout'
38
+ --------------------
39
+
40
+ * **transactionId** <number>: Indicate wich request fires the timeout event.
41
+ * **vreq** <Buffer>: Modbus request adu buffer.
42
+
43
+ .. code-block:: javascript
44
+
45
+ modbusTcpClient.on('req-timeout', (id, req) ->{
46
+ console.log('Timeout error from request: ' + id + '\n');
47
+ })
48
+
49
+ This event is emmited when the number of milliseconds pass to :ref:`Method: modbusTcpClient.setReqTimer(transactionId, [timeout])` ends without call
50
+ :ref:`Method: modbusTcpClient.clearReqTimer(transactionId)`
51
+
52
+ Event: 'transaction'
53
+ --------------------
54
+
55
+ * **req** <Buffer>: Modbus Tcp request adu.
56
+ * **res** <Buffer>: Modbus Tcp request adu.
57
+
58
+ This event is emmited when the :ref:`Method: modbusTcpClient.processResAdu(bufferAdu)` is called to manage a server response.
59
+
60
+
61
+
62
+ ModbusTcpClient's Atributes
63
+ ===========================
64
+
65
+ Atribute: modbusTcpClient._transactionCount
66
+ --------------------------------------------
67
+
68
+ * <number>
69
+
70
+ This property stores the tcp client's transactions counter. It should be not us directly instead through the accessor property transactionCount.
71
+
72
+ Atribute: modbusTcpClient.maxNumberOfTransaction
73
+ -------------------------------------------------
74
+
75
+ * <number>
76
+
77
+ This property stores the maximum value of simultaneously open transactions allowed for the client. Dafault value is 64.
78
+
79
+ Atribute: modbusTcpClient.reqPool
80
+ -----------------------------------------
81
+
82
+ * <Map>
83
+ * *key* <number>: Transaction ID
84
+ * *value* <Buffer>: Modbus Tcp Adu.
85
+
86
+ A map to store active request. Each request is stored with his transaction's id as key.
87
+
88
+ Atribute: modbusTcpClient.reqTimersPool
89
+ ----------------------------------------------
90
+
91
+ * <Map>
92
+ * *key* <number>: Transaction ID
93
+ * *value* <Buffer>: timer's id.
94
+
95
+ A map to store active request's timer. Each request start a timeout timer when is sended to server. This map store the timers is for each request using her transaction's id as key.
96
+
97
+
98
+ Atribute: modbusTcpClient.transactionCount
99
+ -------------------------------------------
100
+
101
+ * <number>
102
+
103
+ Accesor property to get and set the transaction counter.
104
+
105
+
106
+ ModbusTcpClient's Methods
107
+ =========================
108
+
109
+ .. _modbus_tcp_client_methods:
110
+
111
+ See :ref:`ModbusClient Class Methods <modbus_client_methods>` for all base class inherited methods.
112
+
113
+
114
+ Method: modbusTcpClient.boolToBuffer(value)
115
+ ---------------------------------------------------------------------
116
+
117
+ * **value** <boolean>
118
+ * **Return** <Buffer>: Two bytes length Buffer.
119
+
120
+ This is a utitlity method. It gets a buffer with a boolean value encoded for use on forceSingleCoilPdu function as value argument. Example:
121
+
122
+ .. code-block:: javascript
123
+
124
+ let value = modbusTcpClient.boolToBuffer(false);
125
+ console.log(value); //Buffer:[0x00, 0x00]
126
+ value = modbusTcpClient.boolToBuffer(true);
127
+ console.log(value); //Buffer:[0xFF, 0x00]
128
+
129
+
130
+
131
+ Method: modbusTcpClient.getMaskRegisterBuffer(value)
132
+ ---------------------------------------------------------------------
133
+
134
+ * **value** <Array>: An 16 numbers length array indicating how to mask the register.
135
+ * **Return** <Buffer>: Four bytes length Buffer.
136
+
137
+ 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.
138
+
139
+ 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.
140
+ 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:
141
+
142
+ .. code-block:: javascript
143
+
144
+ let value = [-1, 0, 1, -1, -1, -1, 0, 0, 1, -1, -1, -1, -1, -1, 1, 1];
145
+ maskBuffer = modbusTcpClient.getMaskRegisterBuffer(value);
146
+
147
+ //masks
148
+ let andMask = maskBuffer.readUInt16BE(0);
149
+ let orMask = maskBuffer.readUInt16BE(2);
150
+
151
+ let testRegister = Buffer.from([0x9A, 0xFB]);
152
+ console.log(testRegister)
153
+ let currentContent = testRegister.readUInt16BE(0);
154
+ let finalResult = (currentContent & andMask) | (orMask & (~andMask)); //Modbus Spec
155
+
156
+ let finalRegister = Buffer.alloc(2);
157
+ finalRegister.writeUInt16BE(finalResult, 0);
158
+ console.log(finalRegister)
159
+
160
+ //Output
161
+ //<Buffer 9a fb>
162
+ //<Buffer db 3d>
163
+
164
+ Method: modbusTcpClient.boolsToBuffer(value)
165
+ ---------------------------------------------------------------------
166
+
167
+ * **value** <Array>: A boolean array.
168
+ * **Return** <Buffer>: a buffer with binary representation of boolean array.
169
+
170
+ This is a utility method that return a buffer from a boolean array for modbus function code 15.
171
+
172
+ The value argument is a array of boolean with values to bu force to coils. For example:
173
+
174
+ .. code-block:: javascript
175
+
176
+ let values = [0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]; //at 0 index stat LSB Byte
177
+ valBuffer = modbusTcpClient.boolsToBuffer(values);
178
+
179
+ //result valBuffer [0xC2 0x04]
180
+ // calling force multiples colis
181
+ let pdu = modbusTcpClient.forceMultipleCoilsPdu(valBuffer, 10, values.length) //calling force multiples coils at coil 10 and 11 coils to force
182
+
183
+
184
+
185
+ Method: modbusTcpClient.makeHeader(unitId, pduLength)
186
+ ---------------------------------------------------------
187
+
188
+ * **unitId** <number>: Legacy modbus address for being using for a gateway. Modbus spec recomend using 255.
189
+ * **pduLength** <number>: The pdu's buffer length
190
+ * **Returns** <Buffer>: return a 7 bytes buffer with modbus tcp header
191
+
192
+ This functions create a modbus tcp header's buffer. Example:
193
+
194
+ .. code-block:: javascript
195
+
196
+ modbusTcpClient.transactionCount = 10;
197
+ header = modbusTcpClient.makeMbapHeader(2, 5);
198
+ console.log(header);
199
+ //Output
200
+ //<Buffer 0x00 0x0a 0x00 0x00 0x00 0x06, 0x02>
201
+
202
+ Method: modbusTcpClient.parseHeader(bufferHeader)
203
+ ---------------------------------------------------------
204
+
205
+ * **bufferHeader** <Buffer>: Legacy modbus address for being using for a gateway. Modbus spec recomend using 255.
206
+ * **Returns** <object>: return a object with header's fields as properties:
207
+ * *transactionId* <number>: the transaction id.
208
+ * *protocolId* <number>: Must be 0 for modbus tcp protocol.
209
+ * *length* <number>: the number a bytes following the header including the unit id byte.
210
+ * *unitId* <number>: The unit id field, using by gateways to transalte modbus tcp adu to modbus serial adu.
211
+
212
+ This functions create a modbus tcp header's object. It throws a TypeError if argument is not a buffer instance and throw a RangeError if his length is diferent than 7. Example:
213
+
214
+ .. code-block:: javascript
215
+
216
+ let rawHeader = Buffer.from([0x00, 0x10, 0x00, 0x00, 0x00, 0x07, 0x05]);
217
+ let header = modbusTcpClient.parseHeader(rawHeader);
218
+ console.log(header.transactionId);
219
+ console.log(header.protocolId);
220
+ console.log(header.length);
221
+ console.log(header.unitId);
222
+ //Output
223
+ //16
224
+ //0
225
+ //7
226
+ //5
227
+
228
+
229
+ Method: modbusTcpClient.makeRequest(unitId, pdu)
230
+ ---------------------------------------------------------
231
+
232
+ * **unitId** <number>: Legacy modbus address for being using for a gateway. Modbus spec recomend using 255.
233
+ * **pdu** <Buffer>: The pdu's buffer.
234
+ * **Returns** <Buffer>: return a tcp adu request's buffer
235
+
236
+ This functions first increment the transaction counter and create a modbus tcp request ready to be send to the client.
237
+
238
+ Method: modbusTcpClient.storeRequest(bufferReq)
239
+ ---------------------------------------------------------
240
+
241
+ * **bufferReq** <Buffer>: A modbus tcp adu request buffer.
242
+ * **Returns** <bool>: return true if was succesfully stored, otherwise false
243
+
244
+ This functions store a adu request in the :ref:`request Pool <Atribute: modbusTcpClient.reqPool>` if the size of the pool is less than
245
+ :ref:`max number of transaction allowed simultaniously <Atribute: modbusTcpClient.maxNumberOfTransaction>`
246
+
247
+ Method: modbusTcpClient.setReqTimer(transactionId, [timeout])
248
+ -------------------------------------------------------------
249
+
250
+ * **transactionId** <number>: Modbus reqest's transaction id for wich the timer is set.
251
+ * **timeout** <number>: Number of milliseconds to await for a response or fire timeout event.
252
+ * **Returns** <number>: Timer's id to be use on clearTimeout.
253
+
254
+ This functions store a timerId in the :ref:`request timers pool <Atribute: modbusTcpClient.reqTimersPool>` if the request exist in request pool.
255
+
256
+
257
+ Method: modbusTcpClient.clearReqTimer(transactionId)
258
+ -------------------------------------------------------------
259
+
260
+ * **transactionId** <number>: Modbus reqest's transaction id for wich the timer is set.
261
+
262
+
263
+ This functions call the build in clearTimeout function to avoid emit the'req-timeout' event, and remove the entry timerId from :ref:`request timers pool <Atribute: modbusTcpClient.reqTimersPool>`.
264
+
265
+
266
+ Method: modbusTcpClient.processResAdu(bufferAdu)
267
+ ---------------------------------------------------------
268
+
269
+ * **bufferAdu** <Buffer>: A modbus tcp adu response buffer.
270
+
271
+
272
+ This method is used to managed server response. It remove the request from :ref:`request Pool <Atribute: modbusTcpClient.reqPool>`, call
273
+ the :ref:`Method: modbusTcpClient.clearReqTimer(transactionId)` to avoid emit 'req-timeout' event and emit the 'transaction' event.
274
+
275
+
276
+
277
+ Method: modbusClient.getWordFromBuffer(targetBuffer, [offset])
278
+ --------------------------------------------------------------
279
+
280
+ * **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to read.
281
+ * **offset** <number>: A number with register's offset inside the buffer.
282
+ * **Return** <Buffer>: A two bytes length buffer.
283
+
284
+
285
+ 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
286
+
287
+
288
+ Method: modbusClient.setWordToBuffer(value, targetBuffer, [offset])
289
+ -------------------------------------------------------------------
290
+
291
+ * **value** <Buffer>: two bytes length buffer.
292
+ * **targetBuffer** <Buffer>: Buffer with the objetive 16 bits register to write.
293
+ * **offset** <number>: A number with register's offset inside the buffer.
294
+
295
+ This method write a 16 bits register inside a buffer. The offset is 16 bits aligned.
296
+