njs-modbus 3.0.2 → 3.1.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 CHANGED
@@ -125,6 +125,7 @@ All physical layers expose `open()` / `close()`, a `state` property, and events:
125
125
  | `TCP_SERVER` | `TcpServerPhysicalLayer` | `ListenOptions` |
126
126
  | `UDP_CLIENT` | `UdpClientPhysicalLayer` | `{ port, address }` |
127
127
  | `UDP_SERVER` | `UdpServerPhysicalLayer` | `BindOptions` |
128
+ | `CUSTOM` | *(user-provided)* | *(user-defined)* |
128
129
 
129
130
  ### Server options
130
131
 
@@ -162,19 +163,33 @@ new ModbusMaster({
162
163
  })
163
164
  ```
164
165
 
165
- RTU / ASCII framing options:
166
+ RTU framing options:
166
167
 
167
168
  ```typescript
168
169
  protocol: {
169
170
  type: 'RTU',
170
171
  opts: {
171
- intervalBetweenFrames: { unit: 'ms', value: 20 },
172
- interCharTimeout: { unit: 'bit', value: 10 },
172
+ // Either a bare number (milliseconds), or `{ unit: 'bit' | 'ms', value: N }`.
173
+ // Use `0` to disable the timer entirely (useful for lossless transports
174
+ // such as RTU-over-TCP).
175
+ intervalBetweenFrames: 20, // 20 ms
176
+ interCharTimeout: { unit: 'bit', value: 10 }, // bit-time, needs baudRate
173
177
  poolSize: 1024,
174
178
  },
175
179
  }
176
180
  ```
177
181
 
182
+ ASCII options:
183
+
184
+ ```typescript
185
+ protocol: {
186
+ type: 'ASCII',
187
+ opts: {
188
+ lenientHex: true, // accept lowercase hex (a-f). Default: false (strict uppercase per spec)
189
+ },
190
+ }
191
+ ```
192
+
178
193
  ### Methods
179
194
 
180
195
  | Method | Description |
@@ -309,6 +324,58 @@ slave.add({
309
324
  });
310
325
  ```
311
326
 
327
+ ## Performance
328
+
329
+ Benchmarked against [jsmodbus](https://github.com/Cloud-Automation/node-modbus) and [modbus-serial](https://github.com/yaacov/node-modbus-serial).
330
+
331
+ | Metric | njs-modbus | jsmodbus | modbus-serial |
332
+ |--------|-----------|----------|---------------|
333
+ | TCP Throughput | **5,527 ops/sec** | 3,239 (0.59x) | 371 (0.07x) |
334
+ | TCP P99 Latency | **2.71 ms** | 4.73 ms (1.75x) | 12.48 ms (4.61x) |
335
+ | TCP CPU Efficiency | **1,116 µs/op** | 1,950 (1.75x) | 16,715 (14.98x) |
336
+ | Concurrent (8 conn) | **5,274 ops/sec** | 3,416 (0.65x) | 1,815 (0.34x) |
337
+ | RTU CPU Efficiency | **1,762 µs/op** | 1,760 (1.00x) | 2,144 (1.22x) |
338
+ | TCP Res Encode | **2.04M ops/sec** | 373K (0.18x) | 411K (0.20x) |
339
+ | TCP Res Decode | **1.91M ops/sec** | 538K (0.28x) | 231K (0.12x) |
340
+
341
+ <details>
342
+ <summary>Full benchmark results</summary>
343
+
344
+ Node.js v24.15.0 · linux x64 · 3 runs × 300 s · [full report](./benchmark/RESULTS.md)
345
+
346
+ ### TCP Throughput (Single Connection)
347
+
348
+ ```
349
+ njs-modbus │ 5,527 ops/sec 🏆 CPU: 1,116 µs/op
350
+ jsmodbus │ 3,239 ops/sec (0.59x) CPU: 1,950 µs/op
351
+ modbus-serial │ 371 ops/sec (0.07x) CPU: 16,715 µs/op
352
+ ```
353
+
354
+ ### Concurrent (8 Connections)
355
+
356
+ ```
357
+ njs-modbus │ 5,274 ops/sec 🏆 CPU: 1,392 µs/op
358
+ jsmodbus │ 3,416 ops/sec (0.65x) CPU: 2,131 µs/op
359
+ modbus-serial │ 1,815 ops/sec (0.34x) CPU: 3,962 µs/op
360
+ ```
361
+
362
+ ### RTU Serial (115200 baud simulated)
363
+
364
+ ```
365
+ njs-modbus │ 44 ops/sec CPU: 1,762 µs/op
366
+ jsmodbus │ 44 ops/sec CPU: 1,760 µs/op
367
+ modbus-serial │ 44 ops/sec CPU: 2,144 µs/op
368
+ ```
369
+
370
+ ### Encode / Decode (CPU Micro-benchmark)
371
+
372
+ ```
373
+ tcpResEncode: njs-modbus 2.04M ops/sec (jsmodbus 0.18x, modbus-serial 0.20x)
374
+ tcpResDecode: njs-modbus 1.91M ops/sec (jsmodbus 0.28x, modbus-serial 0.12x)
375
+ ```
376
+
377
+ </details>
378
+
312
379
  ## License
313
380
 
314
381
  [![license](https://img.shields.io/github/license/xiejay97/njs-modbus?style=flat-square)](/LICENSE)
package/README.zh-CN.md CHANGED
@@ -125,6 +125,7 @@ const master = new ModbusMaster({
125
125
  | `TCP_SERVER` | `TcpServerPhysicalLayer` | `ListenOptions` |
126
126
  | `UDP_CLIENT` | `UdpClientPhysicalLayer` | `{ port, address }` |
127
127
  | `UDP_SERVER` | `UdpServerPhysicalLayer` | `BindOptions` |
128
+ | `CUSTOM` | *(用户自定义)* | *(自定义)* |
128
129
 
129
130
  ### 服务端配置
130
131
 
@@ -162,19 +163,32 @@ new ModbusMaster({
162
163
  })
163
164
  ```
164
165
 
165
- RTU / ASCII 帧选项:
166
+ RTU 帧选项:
166
167
 
167
168
  ```typescript
168
169
  protocol: {
169
170
  type: 'RTU',
170
171
  opts: {
171
- intervalBetweenFrames: { unit: 'ms', value: 20 },
172
- interCharTimeout: { unit: 'bit', value: 10 },
172
+ // 既可以传裸数字(毫秒),也可以传 `{ unit: 'bit' | 'ms', value: N }`。
173
+ // `0` 显式禁用该定时器(适用于 RTU-over-TCP 等无丢包传输)。
174
+ intervalBetweenFrames: 20, // 20 毫秒
175
+ interCharTimeout: { unit: 'bit', value: 10 }, // 按 bit-time,需 baudRate
173
176
  poolSize: 1024,
174
177
  },
175
178
  }
176
179
  ```
177
180
 
181
+ ASCII 选项:
182
+
183
+ ```typescript
184
+ protocol: {
185
+ type: 'ASCII',
186
+ opts: {
187
+ lenientHex: true, // 接受小写十六进制 (a-f)。默认:false(按规范仅大写)
188
+ },
189
+ }
190
+ ```
191
+
178
192
  ### 方法
179
193
 
180
194
  | 方法 | 说明 |
@@ -309,6 +323,58 @@ slave.add({
309
323
  });
310
324
  ```
311
325
 
326
+ ## 性能
327
+
328
+ 与 [jsmodbus](https://github.com/Cloud-Automation/node-modbus) 和 [modbus-serial](https://github.com/yaacov/node-modbus-serial) 的对比。
329
+
330
+ | 指标 | njs-modbus | jsmodbus | modbus-serial |
331
+ |------|-----------|----------|---------------|
332
+ | TCP 吞吐量 | **5,527 ops/sec** | 3,239 (0.59x) | 371 (0.07x) |
333
+ | TCP P99 延迟 | **2.71 ms** | 4.73 ms (1.75x) | 12.48 ms (4.61x) |
334
+ | TCP CPU 效率 | **1,116 µs/op** | 1,950 (1.75x) | 16,715 (14.98x) |
335
+ | 并发 (8 连接) | **5,274 ops/sec** | 3,416 (0.65x) | 1,815 (0.34x) |
336
+ | RTU CPU 效率 | **1,762 µs/op** | 1,760 (1.00x) | 2,144 (1.22x) |
337
+ | TCP 响应编码 | **2.04M ops/sec** | 373K (0.18x) | 411K (0.20x) |
338
+ | TCP 响应解码 | **1.91M ops/sec** | 538K (0.28x) | 231K (0.12x) |
339
+
340
+ <details>
341
+ <summary>完整基准测试结果</summary>
342
+
343
+ Node.js v24.15.0 · linux x64 · 3 次运行 × 300 秒 · [完整报告](./benchmark/RESULTS.md)
344
+
345
+ ### TCP 吞吐量(单连接)
346
+
347
+ ```
348
+ njs-modbus │ 5,527 ops/sec 🏆 CPU: 1,116 µs/op
349
+ jsmodbus │ 3,239 ops/sec (0.59x) CPU: 1,950 µs/op
350
+ modbus-serial │ 371 ops/sec (0.07x) CPU: 16,715 µs/op
351
+ ```
352
+
353
+ ### 并发(8 连接)
354
+
355
+ ```
356
+ njs-modbus │ 5,274 ops/sec 🏆 CPU: 1,392 µs/op
357
+ jsmodbus │ 3,416 ops/sec (0.65x) CPU: 2,131 µs/op
358
+ modbus-serial │ 1,815 ops/sec (0.34x) CPU: 3,962 µs/op
359
+ ```
360
+
361
+ ### RTU 串口(模拟 115200 波特率)
362
+
363
+ ```
364
+ njs-modbus │ 44 ops/sec CPU: 1,762 µs/op
365
+ jsmodbus │ 44 ops/sec CPU: 1,760 µs/op
366
+ modbus-serial │ 44 ops/sec CPU: 2,144 µs/op
367
+ ```
368
+
369
+ ### 编码 / 解码(CPU 微基准测试)
370
+
371
+ ```
372
+ tcpResEncode: njs-modbus 2.04M ops/sec (jsmodbus 0.18x, modbus-serial 0.20x)
373
+ tcpResDecode: njs-modbus 1.91M ops/sec (jsmodbus 0.28x, modbus-serial 0.12x)
374
+ ```
375
+
376
+ </details>
377
+
312
378
  ## 许可证
313
379
 
314
380
  [![license](https://img.shields.io/github/license/xiejay97/njs-modbus?style=flat-square)](/LICENSE)