nodbus-plus 1.0.0 → 1.0.1

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 (73) hide show
  1. package/.readthedocs.yaml +34 -34
  2. package/LICENSE.md +21 -21
  3. package/README.md +167 -163
  4. package/docs/Makefile +20 -20
  5. package/docs/client/channel.rst +204 -209
  6. package/docs/client/nodbus_master_serial.rst +521 -559
  7. package/docs/client/nodbus_master_tcp.rst +529 -573
  8. package/docs/conf.py +41 -41
  9. package/docs/index.rst +30 -30
  10. package/docs/make.bat +35 -35
  11. package/docs/protocol/modbus_master.rst +355 -276
  12. package/docs/protocol/modbus_master_serial.rst +294 -290
  13. package/docs/protocol/modbus_master_tcp.rst +268 -296
  14. package/docs/protocol/modbus_server.rst +488 -469
  15. package/docs/protocol/modbus_server_serial.rst +392 -543
  16. package/docs/protocol/modbus_server_tcp.rst +197 -365
  17. package/docs/requirements.txt +3 -3
  18. package/docs/server/net_server.rst +226 -242
  19. package/docs/server/nodbus_serial_server.rst +339 -652
  20. package/docs/server/nodbus_tcp_server.rst +307 -505
  21. package/docs/starting.rst +220 -192
  22. package/package.json +46 -39
  23. package/samples/mb_serial_server.js +102 -77
  24. package/samples/mb_tcp_client.js +158 -114
  25. package/samples/mb_tcp_server.js +114 -58
  26. package/src/client/net/serialchannel.js +203 -194
  27. package/src/client/net/tcpchannel.js +228 -233
  28. package/src/client/net/udpchannel.js +227 -242
  29. package/src/client/nodbus_serial_client.js +575 -577
  30. package/src/client/nodbus_tcp_client.js +554 -542
  31. package/src/nodbus-plus.js +146 -157
  32. package/src/nodbus-plus.mjs +18 -0
  33. package/src/protocol/modbus_master.js +405 -298
  34. package/src/protocol/modbus_master_serial.js +260 -246
  35. package/src/protocol/modbus_master_tcp.js +253 -219
  36. package/src/protocol/modbus_server.js +1126 -936
  37. package/src/protocol/modbus_server_serial.js +431 -368
  38. package/src/protocol/modbus_server_tcp.js +178 -129
  39. package/src/protocol/utils.js +371 -296
  40. package/src/server/net/serialserver.js +188 -183
  41. package/src/server/net/tcpserver.js +291 -290
  42. package/src/server/net/udpserver.js +243 -241
  43. package/src/server/nodbus_serial_server.js +234 -238
  44. package/src/server/nodbus_tcp_server.js +250 -249
  45. package/test/client_net_serialchannel.test.js +137 -0
  46. package/test/client_net_tcpchannel.test.js +256 -0
  47. package/test/client_net_udpchannel.test.js +240 -0
  48. package/test/client_nodbus_serial_client.test.js +543 -0
  49. package/test/client_nodbus_tcp_client.test.js +280 -0
  50. package/test/nodbus_plus_common.test.js +111 -0
  51. package/test/nodbus_plus_ecma.test.mjs +115 -0
  52. package/test/protocol_modbus_master.test.js +325 -0
  53. package/test/protocol_modbus_master_serial.test.js +244 -0
  54. package/test/protocol_modbus_master_tcp.test.js +268 -0
  55. package/test/protocol_modbus_server.test.js +380 -0
  56. package/test/protocol_modbus_server_serial.test.js +303 -0
  57. package/test/protocol_modbus_server_tcp.test.js +173 -0
  58. package/test/protocol_utils.test.js +94 -0
  59. package/test/server_net_serialserver.test.js +202 -0
  60. package/test/server_net_tcpserver.test.js +223 -0
  61. package/test/server_net_udpserver.test.js +142 -0
  62. package/test/server_nodbus_serial_server.test.js +186 -0
  63. package/test/server_nodbus_tcp_server.test.js +170 -0
  64. package/test/modbus_master.test.js +0 -279
  65. package/test/modbus_master_serial.test.js +0 -124
  66. package/test/modbus_master_tcp.test.js +0 -178
  67. package/test/modbus_server.test.js +0 -506
  68. package/test/modbus_server_serial.test.js +0 -328
  69. package/test/modbus_server_tcp.test.js +0 -91
  70. package/test/nodbus_client_serial.test.js +0 -307
  71. package/test/nodbus_client_tcp.test.js +0 -334
  72. package/test/nodbus_server_serial.test.js +0 -255
  73. package/test/nodbus_server_tcp.test.js +0 -216
package/.readthedocs.yaml CHANGED
@@ -1,35 +1,35 @@
1
- # .readthedocs.yaml
2
- # Read the Docs configuration file
3
- # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4
-
5
- # Required
6
- version: 2
7
-
8
- # Set the OS, Python version and other tools you might need
9
- build:
10
- os: ubuntu-22.04
11
- tools:
12
- python: "3.11"
13
- # You can also specify other tool versions:
14
- # nodejs: "19"
15
- # rust: "1.64"
16
- # golang: "1.19"
17
-
18
- # Build documentation in the "docs/" directory with Sphinx
19
- sphinx:
20
- configuration: docs/conf.py
21
-
22
- # Optionally build your docs in additional formats such as PDF and ePub
23
- # formats:
24
- # - pdf
25
- # - epub
26
-
27
- # Optional but recommended, declare the Python requirements required
28
- # to build your documentation
29
- # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
30
-
31
- # Optionally declare the Python requirements required to build your docs
32
- python:
33
- install:
34
- - requirements: docs/requirements.txt
1
+ # .readthedocs.yaml
2
+ # Read the Docs configuration file
3
+ # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4
+
5
+ # Required
6
+ version: 2
7
+
8
+ # Set the OS, Python version and other tools you might need
9
+ build:
10
+ os: ubuntu-22.04
11
+ tools:
12
+ python: "3.11"
13
+ # You can also specify other tool versions:
14
+ # nodejs: "19"
15
+ # rust: "1.64"
16
+ # golang: "1.19"
17
+
18
+ # Build documentation in the "docs/" directory with Sphinx
19
+ sphinx:
20
+ configuration: docs/conf.py
21
+
22
+ # Optionally build your docs in additional formats such as PDF and ePub
23
+ # formats:
24
+ # - pdf
25
+ # - epub
26
+
27
+ # Optional but recommended, declare the Python requirements required
28
+ # to build your documentation
29
+ # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
30
+
31
+ # Optionally declare the Python requirements required to build your docs
32
+ python:
33
+ install:
34
+ - requirements: docs/requirements.txt
35
35
 
package/LICENSE.md CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2017 Héctor Enrique Socarrás Cabrera
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Héctor Enrique Socarrás Cabrera
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,163 +1,167 @@
1
- # Nodbus-Plus
2
-
3
- A Modbus protocol library for Node.js, written entirely in JavaScript. It supports both Modbus Serial and Modbus TCP.
4
-
5
- ## Introduction
6
-
7
- Nodbus Plus has been designed as a stack for the Modbus protocol. It can be used to create both your own Modbus client and server using its API, or you can use its build in client or server and have an application with Modbus communication in just minutes.
8
-
9
- ## Installation
10
-
11
- ```console
12
- $ npm install nodbus-plus
13
- ```
14
-
15
- ## Basic Usage:
16
-
17
- ### Create a modbus server.
18
-
19
- ```javascript
20
- const nodbus = require('nodbus-plus');
21
-
22
- //Basic config for tcp server. Default values.
23
- const cfg = {
24
- inputs : 2048, //total inputs
25
- coils : 2048, //total coils
26
- holdingRegisters : 2048, //total holding register
27
- inputRegisters : 2048, //total input register
28
- port : 502, //port to listen to
29
- }
30
-
31
- let server = nodbus.createTcpServer('tcp', cfg);
32
- ```
33
- The first argument for createTcpServer function is the type of transport layer used. The nodbus-plus tcp server suport 'tcp', 'udp4' and 'udp6'. To create a
34
- serial server the procedure is the same, but calling createSerialServer function instead.
35
-
36
- ```javascript
37
- //Basic config for tcp server. Default values.
38
- let cfg = {
39
- address : 1,
40
- transmitionMode: 0, //0-rtu, 1 - ascii
41
- inputs : 2048,
42
- coils : 2048,
43
- holdingRegisters : 2048,
44
- inputRegisters : 2048,
45
- port : 502,
46
- }
47
-
48
- let server = nodbus.createSerialServer('tcp', cfg);
49
- ```
50
- The createSerialServer function can take the value 'serial' as fist argument as well, however the property port mus be a string
51
- with the path to a serial port and adicional port configuration property may be needed. See nodbus-plus api for more details.
52
-
53
- Add listeners for srever's events
54
-
55
- ```javascript
56
- //listenning event
57
- server.on('listening', function(port){
58
- console.log('Server listening on: ' + port);
59
- });
60
-
61
- //event emited when a request are received
62
- server.on('request', function(sock, req){
63
- console.log('Request received')
64
- console.log(req)
65
- });
66
-
67
- //Event emited when server send a response to client
68
- server.on('response', function(sock, res){
69
- console.log('Responding')
70
- console.log(res)
71
- });
72
-
73
- server.on('error', function(err){
74
- console.log(err)
75
- });
76
- ```
77
- Finally the server must be started.
78
-
79
- ```javascript
80
- server.start();
81
- ```
82
- ### Create a modbus client.
83
-
84
- To create a modbus client use the functions createTcpClient or createSerialClient.
85
-
86
- ```javascript
87
- const nodbus = require('nodbus-plus');
88
- let client = nodbus.createSerialClient();
89
-
90
- //emitted when the client stablish connection with the server
91
- client.on('connection', (id)=>{
92
- console.log('connection stablish')
93
- })
94
-
95
- //emited when error occurs
96
- client.on('error', (e)=>{
97
- console.log(e)
98
- })
99
-
100
- //emitted when a request is sended to server
101
- client.on('request', (id, req)=>{
102
- console.log('request sended to device: ' + id);
103
- })
104
-
105
- //emited when no response is received
106
- client.on('req-timeout', (id, adu)=>{
107
- console.log('timeout')
108
- })
109
-
110
- //emited when a response is received
111
- client.on('response', (id, res)=>{
112
- console.log(res)
113
- })
114
- ```
115
-
116
- 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.
117
-
118
- ```javascript
119
-
120
- //channel
121
- channelCfg = {
122
- ip:'127.0.0.1',
123
- port:502,
124
- timeout:250,
125
- }
126
-
127
- client.addChannel('device', 'tcp1', channelCfg);
128
- client.connect('device')
129
-
130
- ```
131
-
132
- Once the client is connected, and event listener configured, data can be exchange using availables modbus function.
133
-
134
- ```javascript
135
-
136
- //reading from cannel 'device', modbus address 1, two coils from 0 coil's address
137
- client.readCoils('device', 1, 0, 2);
138
-
139
- ```
140
-
141
- ### Official Documentation
142
-
143
- For comprehensive information on how to use NodbusPlus, refer to the [official documentation](https://nodbus-plus.readthedocs.io/en/latest/).
144
-
145
- ### Getting Started
146
-
147
- If you're new to NodbusPlus, the [Getting Started Guide](https://nodbus-plus.readthedocs.io/en/latest/starting.html) is a great place to begin.
148
- It will walk you through the installation process and provide a basic usage example.
149
-
150
-
151
- ### Examples
152
-
153
- Inside the root folder of Nodbus plus, there is a directory './samples' that contains three example programs that make use of the library.
154
-
155
-
156
- ## Contributing
157
-
158
- If you have a suggestion or found a issue, let us known and [create an issue](https://github.com/hsocarras/nodbus-plus/issues)
159
-
160
-
161
- ## License
162
-
163
- This project is licensed under the MIT License - see the LICENSE.md file for details
1
+ # Nodbus-Plus
2
+
3
+ A Modbus protocol library for Node.js, written entirely in JavaScript. It supports both Modbus Serial and Modbus TCP.
4
+
5
+ ## Introduction
6
+
7
+ Nodbus-Plus is a Modbus protocol stack for Node.js. You can use its API to create your own Modbus client and server, or use its built-in client and server for quick Modbus communication in just minutes.
8
+
9
+ ## Installation
10
+
11
+ ```console
12
+ npm install nodbus-plus
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ### Creating a Modbus Server
18
+
19
+ ```javascript
20
+ const nodbus = require('nodbus-plus');
21
+
22
+ // TCP server configuration (uses default values if not specified)
23
+ const cfg = {
24
+ inputs: 2048, // Number of discrete inputs
25
+ coils: 2048, // Number of coils
26
+ holdingRegisters: 2048, // Number of holding registers
27
+ inputRegisters: 2048, // Number of input registers
28
+ port: 502 // Port on which to listen
29
+ };
30
+
31
+ let server = nodbus.createTcpServer('tcp', cfg);
32
+ ```
33
+
34
+ The first argument specifies the transport layer type. Supported options are `'tcp'`, `'udp4'`, and `'udp6'`.
35
+ To create a serial server instead, use `createSerialServer`:
36
+
37
+
38
+ ```javascript
39
+ // Serial server configuration
40
+ const cfg = {
41
+ address: 1, // Modbus slave address (1–247)
42
+ transmissionMode: 0, // 0 = RTU, 1 = ASCII
43
+ inputs: 2048, // Number of discrete inputs
44
+ coils: 2048, // Number of coils
45
+ holdingRegisters: 2048, // Number of holding registers
46
+ inputRegisters: 2048, // Number of input registers
47
+ port: 'COM1' // Serial port path (e.g., 'COM1' on Windows, '/dev/ttyUSB0' on Linux)
48
+ };
49
+
50
+ let server = nodbus.createSerialServer('serial', cfg);
51
+ ```
52
+ #### Listen for Server Events
53
+
54
+ ```javascript
55
+ // Emitted when server starts listening
56
+ server.on('listening', (port) => {
57
+ console.log('Server listening on port:', port);
58
+ });
59
+
60
+ // Emitted when a request is received from a client
61
+ server.on('request', (socket, req) => {
62
+ console.log('Request received:', req);
63
+ });
64
+
65
+ // Emitted before sending a response to client
66
+ server.on('response', (socket, res) => {
67
+ console.log('Response:', res);
68
+ });
69
+
70
+ // Emitted when an error occurs
71
+ server.on('error', (err) => {
72
+ console.error('Error:', err);
73
+ });
74
+ ```
75
+
76
+ Finally, start the server:
77
+
78
+ ```javascript
79
+ server.start();
80
+ ```
81
+
82
+ ### Creating a Modbus Client
83
+
84
+ To create a Modbus client, use `createTcpClient` or `createSerialClient`:
85
+
86
+ ```javascript
87
+ const nodbus = require('nodbus-plus');
88
+
89
+ let client = nodbus.createSerialClient();
90
+
91
+ // Emitted when client establishes connection with the server
92
+ client.on('connection', (id) => {
93
+ console.log('Connection established:', id);
94
+ });
95
+
96
+ // Emitted when an error occurs
97
+ client.on('error', (err) => {
98
+ console.error('Error:', err);
99
+ });
100
+
101
+ // Emitted when a request is sent to server
102
+ client.on('request', (id, req) => {
103
+ console.log('Request sent to device:', id);
104
+ });
105
+
106
+ // Emitted when a response timeout occurs
107
+ client.on('req-timeout', (id, adu) => {
108
+ console.error('Request timeout for device:', id);
109
+ });
110
+
111
+ // Emitted when a response is received
112
+ client.on('response', (id, res) => {
113
+ console.log('Response from device:', id, res);
114
+ });
115
+ ```
116
+
117
+ Add channels to the client. Each channel represents a connection to a Modbus device:
118
+
119
+ ```javascript
120
+ // Channel configuration
121
+ const channelCfg = {
122
+ ip: '127.0.0.1', // Target device IP address
123
+ port: 502, // Target device port
124
+ timeout: 250 // Request timeout in milliseconds
125
+ };
126
+
127
+ client.addChannel('device1', 'tcp1', channelCfg);
128
+ client.connect('device1');
129
+ ```
130
+
131
+ Once connected, use available Modbus functions to exchange data:
132
+
133
+ ```javascript
134
+ // Read 2 coils starting at address 0 from device at Modbus address 1
135
+ client.readCoils('device1', 1, 0, 2);
136
+ ```
137
+
138
+ ## Documentation
139
+
140
+ For comprehensive API documentation, visit the [official documentation](https://nodbus-plus.readthedocs.io/en/latest/).
141
+
142
+ ## Getting Started
143
+
144
+ New to Nodbus-Plus? The [Getting Started Guide](https://nodbus-plus.readthedocs.io/en/latest/starting.html) provides installation instructions and basic usage examples.
145
+
146
+ ## Examples
147
+
148
+ See the `./samples` directory for example programs demonstrating library usage.
149
+
150
+
151
+
152
+ ## Features
153
+
154
+ - **Full Modbus Protocol Support**: Read/write coils, discrete inputs, holding registers, and input registers.
155
+ - **Modbus Serial (RTU/ASCII)**: Serial communication with RTU and ASCII transmission modes.
156
+ - **Modbus TCP**: TCP/IP network communication with standard Modbus TCP protocol.
157
+ - **Pure JavaScript**: No native dependencies; works on any Node.js environment.
158
+ - **Client & Server**: Create both Modbus clients and servers with the same library.
159
+ - **Event-Driven**: Asynchronous event-based API for responsive applications.
160
+
161
+ ## Contributing
162
+
163
+ Contributions are welcome! If you find a bug or have a feature suggestion, please [open an issue](https://github.com/hsocarras/nodbus-plus/issues).
164
+
165
+ ## License
166
+
167
+ MIT License. See `LICENSE.md` for details.
package/docs/Makefile CHANGED
@@ -1,20 +1,20 @@
1
- # Minimal makefile for Sphinx documentation
2
- #
3
-
4
- # You can set these variables from the command line, and also
5
- # from the environment for the first two.
6
- SPHINXOPTS ?=
7
- SPHINXBUILD ?= sphinx-build
8
- SOURCEDIR = source
9
- BUILDDIR = build
10
-
11
- # Put it first so that "make" without argument is like "make help".
12
- help:
13
- @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14
-
15
- .PHONY: help Makefile
16
-
17
- # Catch-all target: route all unknown targets to Sphinx using the new
18
- # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19
- %: Makefile
20
- @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
1
+ # Minimal makefile for Sphinx documentation
2
+ #
3
+
4
+ # You can set these variables from the command line, and also
5
+ # from the environment for the first two.
6
+ SPHINXOPTS ?=
7
+ SPHINXBUILD ?= sphinx-build
8
+ SOURCEDIR = source
9
+ BUILDDIR = build
10
+
11
+ # Put it first so that "make" without argument is like "make help".
12
+ help:
13
+ @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
14
+
15
+ .PHONY: help Makefile
16
+
17
+ # Catch-all target: route all unknown targets to Sphinx using the new
18
+ # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
19
+ %: Makefile
20
+ @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)