njs-modbus 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/README.md +188 -0
- package/dist/index.cjs +1872 -0
- package/dist/index.d.ts +339 -0
- package/dist/index.mjs +1858 -0
- package/dist/src/error-code.d.ts +13 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/layers/application/abstract-application-layer.d.ts +12 -0
- package/dist/src/layers/application/ascii-application-layer.d.ts +13 -0
- package/dist/src/layers/application/index.d.ts +4 -0
- package/dist/src/layers/application/rtu-application-layer.d.ts +16 -0
- package/dist/src/layers/application/tcp-application-layer.d.ts +11 -0
- package/dist/src/layers/physical/abstract-physical-layer.d.ts +16 -0
- package/dist/src/layers/physical/index.d.ts +5 -0
- package/dist/src/layers/physical/serial-physical-layer.d.ts +38 -0
- package/dist/src/layers/physical/tcp-client-physical-layer.d.ts +15 -0
- package/dist/src/layers/physical/tcp-server-physical-layer.d.ts +16 -0
- package/dist/src/layers/physical/udp-physical-layer.d.ts +27 -0
- package/dist/src/master/index.d.ts +1 -0
- package/dist/src/master/master.d.ts +82 -0
- package/dist/src/slave/index.d.ts +2 -0
- package/dist/src/slave/slave.d.ts +72 -0
- package/dist/src/types.d.ts +23 -0
- package/dist/src/utils/checkRange.d.ts +2 -0
- package/dist/src/utils/crc.d.ts +1 -0
- package/dist/src/utils/getThreePointFiveT.d.ts +7 -0
- package/dist/src/utils/index.d.ts +4 -0
- package/dist/src/utils/lrc.d.ts +1 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# njs-modbus
|
|
2
|
+
|
|
3
|
+
A pure JavaScript implementation of MODBUS for NodeJS.
|
|
4
|
+
|
|
5
|
+
<div>
|
|
6
|
+
|
|
7
|
+
<!-- prettier-ignore-start -->
|
|
8
|
+
[](http://www.npm-stats.com/~packages/njs-modbus)
|
|
9
|
+
[](https://www.npmjs.com/package/njs-modbus)
|
|
10
|
+
[](https://bundlephobia.com/package/njs-modbus)
|
|
11
|
+
<!-- prettier-ignore-end -->
|
|
12
|
+
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
## Introduction
|
|
16
|
+
|
|
17
|
+
`njs-modbus` is designed as a layered architecture, including the physical layer and the application layer:
|
|
18
|
+
|
|
19
|
+
- Physical layer implements Serial Port, TCP/IP and UDP/IP.
|
|
20
|
+
- Application layer implements RTU, ASCII and TCP.
|
|
21
|
+
|
|
22
|
+
`njs-modbus` provide both client and server.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- Full modbus standard protocol implementation
|
|
27
|
+
- Support for custom function codes
|
|
28
|
+
- Support broadcasting
|
|
29
|
+
- Very lightweight project
|
|
30
|
+
- Full typescript
|
|
31
|
+
|
|
32
|
+
### Supported function codes
|
|
33
|
+
|
|
34
|
+
| Code | |
|
|
35
|
+
| ----- | ----------------------------- |
|
|
36
|
+
| 01 | Read Coils |
|
|
37
|
+
| 02 | Read Discrete Inputs |
|
|
38
|
+
| 03 | Read Holding Registers |
|
|
39
|
+
| 04 | Read Input Register |
|
|
40
|
+
| 05 | Write Single Coil |
|
|
41
|
+
| 06 | Write Single Register |
|
|
42
|
+
| 15 | Write Multiple Coils |
|
|
43
|
+
| 16 | Write Multiple Registers |
|
|
44
|
+
| 17 | Report Server ID |
|
|
45
|
+
| 22 | Mask Write Register |
|
|
46
|
+
| 23 | Read/Write Multiple Registers |
|
|
47
|
+
| 43/14 | Read device Identification |
|
|
48
|
+
|
|
49
|
+
### Supported protocols
|
|
50
|
+
|
|
51
|
+
- Modbus RTU
|
|
52
|
+
- Modbus ASCII
|
|
53
|
+
- Modbus TCP/IP
|
|
54
|
+
- Modbus UDP/IP
|
|
55
|
+
- Modbus RTU/ASCII Over TCP/IP
|
|
56
|
+
- Modbus RTU/ASCII Over UDP/IP
|
|
57
|
+
|
|
58
|
+
#### Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm install njs-modbus
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Examples
|
|
65
|
+
|
|
66
|
+
### Modbus RTU Master
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { SerialPhysicalLayer, RtuApplicationLayer, ModbusMaster } from 'njs-modbus';
|
|
70
|
+
|
|
71
|
+
const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
|
|
72
|
+
const applicationLayer = new RtuApplicationLayer(physicalLayer, { baudRate: 9600 });
|
|
73
|
+
|
|
74
|
+
const modbusMaster = new ModbusMaster(applicationLayer, physicalLayer);
|
|
75
|
+
|
|
76
|
+
modbusMaster
|
|
77
|
+
.open()
|
|
78
|
+
.then(() => {
|
|
79
|
+
console.log('opened');
|
|
80
|
+
modbusMaster.readHoldingRegisters(1, 0, 10).then((res) => {
|
|
81
|
+
console.log(res);
|
|
82
|
+
});
|
|
83
|
+
})
|
|
84
|
+
.catch((error) => {
|
|
85
|
+
console.log(error);
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Modbus RTU Slave
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { SerialPhysicalLayer, RtuApplicationLayer, ModbusSlave } from 'njs-modbus';
|
|
93
|
+
|
|
94
|
+
const MB_SERVER = {
|
|
95
|
+
discreteInputs: new Map<number, boolean>(),
|
|
96
|
+
coils: new Map<number, boolean>(),
|
|
97
|
+
inputRegisters: new Map<number, number>(),
|
|
98
|
+
holdingRegisters: new Map<number, number>(),
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const physicalLayer = new SerialPhysicalLayer({ path: 'COM1', baudRate: 9600, dataBits: 8, parity: 'none', stopBits: 1 });
|
|
102
|
+
const applicationLayer = new RtuApplicationLayer(physicalLayer, { baudRate: 9600 });
|
|
103
|
+
|
|
104
|
+
const modbusSlave = new ModbusSlave(
|
|
105
|
+
{
|
|
106
|
+
readDiscreteInputs: (address, length) => {
|
|
107
|
+
return Array.from({ length }).map((_, i) => {
|
|
108
|
+
const discreteInput = MB_SERVER.discreteInputs.get(address + i);
|
|
109
|
+
if (typeof discreteInput === 'undefined') {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
return discreteInput;
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
readCoils: (address, length) => {
|
|
117
|
+
return Array.from({ length }).map((_, i) => {
|
|
118
|
+
const coil = MB_SERVER.coils.get(address + i);
|
|
119
|
+
if (typeof coil === 'undefined') {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
return coil;
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
writeSingleCoil: (address, value) => {
|
|
126
|
+
MB_SERVER.coils.set(address, value);
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
readInputRegisters: (address, length) => {
|
|
130
|
+
return Array.from({ length }).map((_, i) => {
|
|
131
|
+
const inputRegister = MB_SERVER.inputRegisters.get(address + i);
|
|
132
|
+
if (typeof inputRegister === 'undefined') {
|
|
133
|
+
return 0;
|
|
134
|
+
}
|
|
135
|
+
return inputRegister;
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
readHoldingRegisters: (address, length) => {
|
|
140
|
+
return Array.from({ length }).map((_, i) => {
|
|
141
|
+
const holdingRegister = MB_SERVER.holdingRegisters.get(address + i);
|
|
142
|
+
if (typeof holdingRegister === 'undefined') {
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
return holdingRegister;
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
writeSingleRegister: (address, value) => {
|
|
149
|
+
MB_SERVER.holdingRegisters.set(address, value);
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
reportServerId: () => ({ additionalData: [1, 2, 3] }),
|
|
153
|
+
|
|
154
|
+
readDeviceIdentification: () => ({
|
|
155
|
+
0x00: 'Basic:VendorName',
|
|
156
|
+
0x01: 'Basic:ProductCode',
|
|
157
|
+
0x02: 'Basic:MajorMinorRevision',
|
|
158
|
+
0x03: 'Regular:VendorUrl',
|
|
159
|
+
0x04: 'Regular:ProductName',
|
|
160
|
+
0x05: 'Regular:ModelName',
|
|
161
|
+
0x06: 'Regular:UserApplicationName',
|
|
162
|
+
0x80: 'Extended:Extended',
|
|
163
|
+
0xff: 'Extended:Extended',
|
|
164
|
+
}),
|
|
165
|
+
},
|
|
166
|
+
applicationLayer,
|
|
167
|
+
physicalLayer,
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
modbusSlave
|
|
171
|
+
.open()
|
|
172
|
+
.then(() => {
|
|
173
|
+
console.log('opened');
|
|
174
|
+
})
|
|
175
|
+
.catch((error) => {
|
|
176
|
+
console.log(error);
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
For more advanced examples, check out [examples](/examples) included in the repository. If you have created any utilities that meet a specific need, feel free to submit them so others can benefit.
|
|
181
|
+
|
|
182
|
+
## Contributing
|
|
183
|
+
|
|
184
|
+
Please read our [contributing guide](/CONTRIBUTING.md) first.
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
[](/LICENSE)
|