@ya-modbus/emulator 0.7.1 → 0.9.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/CHANGELOG.md +12 -0
- package/README.md +97 -5
- package/dist/cjs/src/emulator.d.ts.map +1 -1
- package/dist/cjs/src/emulator.js +14 -1
- package/dist/cjs/src/emulator.js.map +1 -1
- package/dist/cjs/src/transports/rtu.d.ts +12 -15
- package/dist/cjs/src/transports/rtu.d.ts.map +1 -1
- package/dist/cjs/src/transports/rtu.js +172 -78
- package/dist/cjs/src/transports/rtu.js.map +1 -1
- package/dist/cjs/src/transports/tcp.d.ts +38 -0
- package/dist/cjs/src/transports/tcp.d.ts.map +1 -0
- package/dist/cjs/src/transports/tcp.js +209 -0
- package/dist/cjs/src/transports/tcp.js.map +1 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/emulator.d.ts.map +1 -1
- package/dist/esm/src/emulator.js +14 -1
- package/dist/esm/src/emulator.js.map +1 -1
- package/dist/esm/src/transports/rtu.d.ts +12 -15
- package/dist/esm/src/transports/rtu.d.ts.map +1 -1
- package/dist/esm/src/transports/rtu.js +172 -78
- package/dist/esm/src/transports/rtu.js.map +1 -1
- package/dist/esm/src/transports/tcp.d.ts +38 -0
- package/dist/esm/src/transports/tcp.d.ts.map +1 -0
- package/dist/esm/src/transports/tcp.js +205 -0
- package/dist/esm/src/transports/tcp.js.map +1 -0
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.9.0](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/emulator@0.8.0...@ya-modbus/emulator@0.9.0) (2026-02-07)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **emulator:** implement TCP transport ([#252](https://github.com/groupsky/ya-modbus/issues/252)) ([3df8fda](https://github.com/groupsky/ya-modbus/commit/3df8fdac2c279ca13fb314867daa2ffc32194481)), closes [#244](https://github.com/groupsky/ya-modbus/issues/244)
|
|
11
|
+
|
|
12
|
+
# [0.8.0](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/emulator@0.7.1...@ya-modbus/emulator@0.8.0) (2026-02-07)
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
- **emulator:** implement RTU transport using modbus-serial ([#243](https://github.com/groupsky/ya-modbus/issues/243)) ([71d0757](https://github.com/groupsky/ya-modbus/commit/71d0757bbbab9ae4e611c7e58baea85f2ca09501)), closes [#244](https://github.com/groupsky/ya-modbus/issues/244) [#245](https://github.com/groupsky/ya-modbus/issues/245) [#246](https://github.com/groupsky/ya-modbus/issues/246) [#247](https://github.com/groupsky/ya-modbus/issues/247) [#248](https://github.com/groupsky/ya-modbus/issues/248) [#249](https://github.com/groupsky/ya-modbus/issues/249) [#250](https://github.com/groupsky/ya-modbus/issues/250)
|
|
17
|
+
|
|
6
18
|
## [0.7.1](https://github.com/groupsky/ya-modbus/compare/@ya-modbus/emulator@0.7.0...@ya-modbus/emulator@0.7.1) (2026-02-06)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @ya-modbus/emulator
|
package/README.md
CHANGED
|
@@ -18,13 +18,14 @@ npm install @ya-modbus/emulator
|
|
|
18
18
|
|
|
19
19
|
## Quick Start
|
|
20
20
|
|
|
21
|
+
### Memory Transport (fastest for unit tests)
|
|
22
|
+
|
|
21
23
|
```typescript
|
|
22
24
|
import { ModbusEmulator } from '@ya-modbus/emulator'
|
|
23
25
|
|
|
24
|
-
// Create emulator with
|
|
26
|
+
// Create emulator with in-memory transport
|
|
25
27
|
const emulator = new ModbusEmulator({
|
|
26
|
-
transport: '
|
|
27
|
-
port: 5502,
|
|
28
|
+
transport: 'memory',
|
|
28
29
|
})
|
|
29
30
|
|
|
30
31
|
// Add a device
|
|
@@ -48,6 +49,83 @@ await emulator.start()
|
|
|
48
49
|
await emulator.stop()
|
|
49
50
|
```
|
|
50
51
|
|
|
52
|
+
### TCP Transport (network)
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { ModbusEmulator } from '@ya-modbus/emulator'
|
|
56
|
+
|
|
57
|
+
// Create emulator with TCP transport
|
|
58
|
+
const emulator = new ModbusEmulator({
|
|
59
|
+
transport: 'tcp',
|
|
60
|
+
host: '0.0.0.0', // Listen on all interfaces
|
|
61
|
+
port: 502, // Standard Modbus TCP port
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
// Add devices
|
|
65
|
+
emulator.addDevice({
|
|
66
|
+
slaveId: 1,
|
|
67
|
+
registers: {
|
|
68
|
+
holding: { 0: 230, 1: 52 },
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
emulator.addDevice({
|
|
73
|
+
slaveId: 2,
|
|
74
|
+
registers: {
|
|
75
|
+
holding: { 0: 500, 1: 100 },
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
// Start emulator
|
|
80
|
+
await emulator.start()
|
|
81
|
+
|
|
82
|
+
// Use with your driver tests (clients connect to localhost:502)
|
|
83
|
+
// ...
|
|
84
|
+
|
|
85
|
+
// Stop emulator
|
|
86
|
+
await emulator.stop()
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### RTU Transport (serial port)
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { ModbusEmulator } from '@ya-modbus/emulator'
|
|
93
|
+
|
|
94
|
+
// Create emulator with RTU transport
|
|
95
|
+
const emulator = new ModbusEmulator({
|
|
96
|
+
transport: 'rtu',
|
|
97
|
+
port: '/dev/ttyUSB0', // or 'COM3' on Windows
|
|
98
|
+
baudRate: 9600,
|
|
99
|
+
parity: 'none',
|
|
100
|
+
dataBits: 8,
|
|
101
|
+
stopBits: 1,
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// Add devices
|
|
105
|
+
emulator.addDevice({
|
|
106
|
+
slaveId: 1,
|
|
107
|
+
registers: {
|
|
108
|
+
holding: { 0: 230, 1: 52 },
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
emulator.addDevice({
|
|
113
|
+
slaveId: 2,
|
|
114
|
+
registers: {
|
|
115
|
+
holding: { 0: 500, 1: 100 },
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// Start emulator
|
|
120
|
+
await emulator.start()
|
|
121
|
+
|
|
122
|
+
// Use with your driver tests
|
|
123
|
+
// ...
|
|
124
|
+
|
|
125
|
+
// Stop emulator
|
|
126
|
+
await emulator.stop()
|
|
127
|
+
```
|
|
128
|
+
|
|
51
129
|
## CLI Usage
|
|
52
130
|
|
|
53
131
|
The emulator can be used from the command line for quick testing:
|
|
@@ -84,6 +162,22 @@ Options:
|
|
|
84
162
|
|
|
85
163
|
Create a YAML or JSON configuration file to define devices and behaviors:
|
|
86
164
|
|
|
165
|
+
**Basic TCP Example** (`basic-tcp.yaml`):
|
|
166
|
+
|
|
167
|
+
```yaml
|
|
168
|
+
transport:
|
|
169
|
+
type: tcp
|
|
170
|
+
host: 0.0.0.0
|
|
171
|
+
port: 502
|
|
172
|
+
|
|
173
|
+
devices:
|
|
174
|
+
- slaveId: 1
|
|
175
|
+
registers:
|
|
176
|
+
holding:
|
|
177
|
+
0: 230 # Voltage * 10 = 23.0V
|
|
178
|
+
1: 52 # Current * 10 = 5.2A
|
|
179
|
+
```
|
|
180
|
+
|
|
87
181
|
**Basic RTU Example** (`basic-rtu.yaml`):
|
|
88
182
|
|
|
89
183
|
```yaml
|
|
@@ -145,8 +239,6 @@ See `examples/config-files/` for more examples.
|
|
|
145
239
|
|
|
146
240
|
### Testing with Virtual Serial Ports
|
|
147
241
|
|
|
148
|
-
> **⚠️ Note**: RTU transport is currently a placeholder for v0.1.0. Serial port communication will be implemented in v0.2.0. Use the memory transport for testing in this version.
|
|
149
|
-
|
|
150
242
|
For testing without physical hardware, create virtual serial port pairs:
|
|
151
243
|
|
|
152
244
|
**Linux/macOS** (using socat):
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emulator.d.ts","sourceRoot":"","sources":["../../../src/emulator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;
|
|
1
|
+
{"version":3,"file":"emulator.d.ts","sourceRoot":"","sources":["../../../src/emulator.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAIzD,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErE,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAyC;IACxD,OAAO,CAAC,SAAS,CAAe;IAChC,OAAO,CAAC,OAAO,CAAQ;gBAEX,MAAM,EAAE,cAAc;IA0C5B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YAQb,aAAa;IA2B3B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAyB1B,YAAY,IAAI,aAAa;IAI7B,SAAS,CAAC,MAAM,EAAE,YAAY,GAAG,cAAc;IAU/C,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAOnC,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;CAGvD"}
|
package/dist/cjs/src/emulator.js
CHANGED
|
@@ -40,6 +40,7 @@ exports.ModbusEmulator = void 0;
|
|
|
40
40
|
const device_js_1 = require("./device.js");
|
|
41
41
|
const memory_js_1 = require("./transports/memory.js");
|
|
42
42
|
const rtu_js_1 = require("./transports/rtu.js");
|
|
43
|
+
const tcp_js_1 = require("./transports/tcp.js");
|
|
43
44
|
class ModbusEmulator {
|
|
44
45
|
constructor(config) {
|
|
45
46
|
this.devices = new Map();
|
|
@@ -62,8 +63,20 @@ class ModbusEmulator {
|
|
|
62
63
|
};
|
|
63
64
|
this.transport = new rtu_js_1.RtuTransport(rtuConfig);
|
|
64
65
|
}
|
|
66
|
+
else if (config.transport === 'tcp') {
|
|
67
|
+
if (!config.host) {
|
|
68
|
+
throw new Error('TCP transport requires host');
|
|
69
|
+
}
|
|
70
|
+
if (!config.port || typeof config.port !== 'number') {
|
|
71
|
+
throw new Error('TCP transport requires port');
|
|
72
|
+
}
|
|
73
|
+
this.transport = new tcp_js_1.TcpTransport({
|
|
74
|
+
host: config.host,
|
|
75
|
+
port: config.port,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
65
78
|
else {
|
|
66
|
-
throw new Error(`Unsupported transport: ${config.transport}`);
|
|
79
|
+
throw new Error(`Unsupported transport: ${String(config.transport)}`);
|
|
67
80
|
}
|
|
68
81
|
// Set up request handler
|
|
69
82
|
this.transport.onRequest(this.handleRequest.bind(this));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emulator.js","sourceRoot":"","sources":["../../../src/emulator.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,2CAA4C;AAE5C,sDAAwD;AACxD,gDAAkD;AAGlD,MAAa,cAAc;IAKzB,YAAY,MAAsB;QAJ1B,YAAO,GAAgC,IAAI,GAAG,EAAE,CAAA;QAEhD,YAAO,GAAG,KAAK,CAAA;QAGrB,mCAAmC;QACnC,IAAI,MAAM,CAAC,SAAS,KAAK,QAAQ,EAAE,CAAC;YAClC,IAAI,CAAC,SAAS,GAAG,IAAI,2BAAe,EAAE,CAAA;QACxC,CAAC;aAAM,IAAI,MAAM,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C,MAAoB;QAC5B,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,wBAAwB,MAAM,CAAC,OAAO,iBAAiB,CAAC,CAAA;QAC1E,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,0BAAc,CAAC,MAAM,CAAC,CAAA;QACzC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;QACxC,OAAO,MAAM,CAAA;IACf,CAAC;IAED,YAAY,CAAC,OAAe;QAC1B,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,wBAAwB,OAAO,YAAY,CAAC,CAAA;QAC9D,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IAC9B,CAAC;IAED,SAAS,CAAC,OAAe;QACvB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;CACF;AA9ID,wCA8IC"}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* RTU (serial) transport for Modbus emulator
|
|
3
3
|
*
|
|
4
|
-
* This implementation uses modbus-serial for protocol handling.
|
|
5
|
-
* Serial port communication will be added in future iterations.
|
|
4
|
+
* This implementation uses modbus-serial ServerSerial for protocol handling.
|
|
6
5
|
*/
|
|
7
6
|
import { BaseTransport } from './base.js';
|
|
8
7
|
export interface RtuTransportConfig {
|
|
@@ -13,32 +12,30 @@ export interface RtuTransportConfig {
|
|
|
13
12
|
stopBits?: 1 | 2;
|
|
14
13
|
}
|
|
15
14
|
export declare class RtuTransport extends BaseTransport {
|
|
15
|
+
private config;
|
|
16
16
|
private requestHandler?;
|
|
17
|
+
private server?;
|
|
17
18
|
private started;
|
|
18
|
-
constructor(
|
|
19
|
+
constructor(config: RtuTransportConfig);
|
|
19
20
|
start(): Promise<void>;
|
|
20
21
|
stop(): Promise<void>;
|
|
21
22
|
send(_slaveId: number, _response: Buffer): Promise<void>;
|
|
22
23
|
onRequest(handler: (slaveId: number, request: Buffer) => Promise<Buffer>): void;
|
|
23
24
|
/**
|
|
24
|
-
* Handle
|
|
25
|
-
* @internal
|
|
25
|
+
* Handle register read operations
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
private handleRegisterRead;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* Returns CRC as a 16-bit value that can be written with writeUInt16LE
|
|
29
|
+
* Handle register write operations
|
|
32
30
|
*/
|
|
33
|
-
private
|
|
31
|
+
private handleRegisterWrite;
|
|
34
32
|
/**
|
|
35
|
-
*
|
|
33
|
+
* Handle coil read operations
|
|
36
34
|
*/
|
|
37
|
-
private
|
|
35
|
+
private handleCoilRead;
|
|
38
36
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @internal
|
|
37
|
+
* Handle coil write operations
|
|
41
38
|
*/
|
|
42
|
-
|
|
39
|
+
private handleCoilWrite;
|
|
43
40
|
}
|
|
44
41
|
//# sourceMappingURL=rtu.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rtu.d.ts","sourceRoot":"","sources":["../../../../src/transports/rtu.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"rtu.d.ts","sourceRoot":"","sources":["../../../../src/transports/rtu.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAEzC,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAA;IAChC,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;IAChB,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;CACjB;AAED,qBAAa,YAAa,SAAQ,aAAa;IAC7C,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,cAAc,CAAC,CAAuD;IAC9E,OAAO,CAAC,MAAM,CAAC,CAAc;IAC7B,OAAO,CAAC,OAAO,CAAQ;gBAEX,MAAM,EAAE,kBAAkB;IAKhC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAuEtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAuB3B,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASxD,SAAS,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI;IAI/E;;OAEG;YACW,kBAAkB;IAmChC;;OAEG;YACW,mBAAmB;IAqCjC;;OAEG;YACW,cAAc;IAiC5B;;OAEG;YACW,eAAe;CAoB9B"}
|
|
@@ -2,122 +2,216 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* RTU (serial) transport for Modbus emulator
|
|
4
4
|
*
|
|
5
|
-
* This implementation uses modbus-serial for protocol handling.
|
|
6
|
-
* Serial port communication will be added in future iterations.
|
|
5
|
+
* This implementation uses modbus-serial ServerSerial for protocol handling.
|
|
7
6
|
*/
|
|
8
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
8
|
exports.RtuTransport = void 0;
|
|
9
|
+
const modbus_serial_1 = require("modbus-serial");
|
|
10
10
|
const base_js_1 = require("./base.js");
|
|
11
11
|
class RtuTransport extends base_js_1.BaseTransport {
|
|
12
|
-
constructor(
|
|
12
|
+
constructor(config) {
|
|
13
13
|
super();
|
|
14
14
|
this.started = false;
|
|
15
|
-
|
|
15
|
+
this.config = config;
|
|
16
16
|
}
|
|
17
|
-
start() {
|
|
18
|
-
this.started
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
async start() {
|
|
18
|
+
if (this.started) {
|
|
19
|
+
throw new Error('Transport already started');
|
|
20
|
+
}
|
|
21
|
+
// Create service vector that bridges modbus-serial callbacks to our request handler
|
|
22
|
+
const serviceVector = {
|
|
23
|
+
getHoldingRegister: async (addr, unitID) => {
|
|
24
|
+
return this.handleRegisterRead(unitID, 0x03, addr, 1);
|
|
25
|
+
},
|
|
26
|
+
getInputRegister: async (addr, unitID) => {
|
|
27
|
+
return this.handleRegisterRead(unitID, 0x04, addr, 1);
|
|
28
|
+
},
|
|
29
|
+
getMultipleHoldingRegisters: async (addr, length, unitID) => {
|
|
30
|
+
return this.handleRegisterRead(unitID, 0x03, addr, length);
|
|
31
|
+
},
|
|
32
|
+
getMultipleInputRegisters: async (addr, length, unitID) => {
|
|
33
|
+
return this.handleRegisterRead(unitID, 0x04, addr, length);
|
|
34
|
+
},
|
|
35
|
+
setRegister: async (addr, value, unitID) => {
|
|
36
|
+
return this.handleRegisterWrite(unitID, 0x06, addr, [value]);
|
|
37
|
+
},
|
|
38
|
+
setRegisterArray: async (addr, values, unitID) => {
|
|
39
|
+
return this.handleRegisterWrite(unitID, 0x10, addr, values);
|
|
40
|
+
},
|
|
41
|
+
getCoil: async (addr, unitID) => {
|
|
42
|
+
return this.handleCoilRead(unitID, 0x01, addr, 1);
|
|
43
|
+
},
|
|
44
|
+
getDiscreteInput: async (addr, unitID) => {
|
|
45
|
+
return this.handleCoilRead(unitID, 0x02, addr, 1);
|
|
46
|
+
},
|
|
47
|
+
setCoil: async (addr, value, unitID) => {
|
|
48
|
+
return this.handleCoilWrite(unitID, 0x05, addr, value);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
// Build options with only defined serial port parameters
|
|
52
|
+
// Filter out undefined values to satisfy exactOptionalPropertyTypes
|
|
53
|
+
const options = Object.fromEntries(Object.entries({
|
|
54
|
+
path: this.config.port,
|
|
55
|
+
unitID: 255, // Listen to all unit IDs
|
|
56
|
+
baudRate: this.config.baudRate,
|
|
57
|
+
parity: this.config.parity,
|
|
58
|
+
dataBits: this.config.dataBits,
|
|
59
|
+
stopBits: this.config.stopBits,
|
|
60
|
+
}).filter(([, value]) => value !== undefined));
|
|
61
|
+
// Create and start server
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
this.server = new modbus_serial_1.ServerSerial(serviceVector, {
|
|
64
|
+
...options,
|
|
65
|
+
openCallback: (err) => {
|
|
66
|
+
if (err) {
|
|
67
|
+
reject(err);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
this.started = true;
|
|
71
|
+
resolve();
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
// Handle errors
|
|
76
|
+
this.server.on('error', (err) => {
|
|
77
|
+
// Log error but don't stop server
|
|
78
|
+
console.error('RTU transport error:', err);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
22
81
|
}
|
|
23
|
-
stop() {
|
|
24
|
-
if (!this.started) {
|
|
25
|
-
return
|
|
82
|
+
async stop() {
|
|
83
|
+
if (!this.started || !this.server) {
|
|
84
|
+
return;
|
|
26
85
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
86
|
+
return new Promise((resolve, reject) => {
|
|
87
|
+
if (!this.server) {
|
|
88
|
+
resolve();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.server.close((err) => {
|
|
92
|
+
if (err) {
|
|
93
|
+
reject(err);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.started = false;
|
|
97
|
+
delete this.server;
|
|
98
|
+
resolve();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
30
102
|
}
|
|
31
103
|
send(_slaveId, _response) {
|
|
32
104
|
if (!this.started) {
|
|
33
105
|
return Promise.reject(new Error('Transport not started'));
|
|
34
106
|
}
|
|
35
|
-
//
|
|
107
|
+
// Responses are sent automatically by modbus-serial server
|
|
108
|
+
// This method is kept for interface compatibility
|
|
36
109
|
return Promise.resolve();
|
|
37
110
|
}
|
|
38
111
|
onRequest(handler) {
|
|
39
112
|
this.requestHandler = handler;
|
|
40
113
|
}
|
|
41
114
|
/**
|
|
42
|
-
* Handle
|
|
43
|
-
* @internal
|
|
115
|
+
* Handle register read operations
|
|
44
116
|
*/
|
|
45
|
-
async
|
|
46
|
-
// Minimum frame: slave_id + function_code + CRC (4 bytes)
|
|
47
|
-
if (frame.length < 4) {
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
// Verify CRC
|
|
51
|
-
if (!this.verifyCRC(frame)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
// Extract slave ID (safe after length check)
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
56
|
-
const slaveId = frame[0];
|
|
57
|
-
// Remove CRC to get request data
|
|
58
|
-
const request = frame.subarray(0, frame.length - 2);
|
|
117
|
+
async handleRegisterRead(unitID, functionCode, addr, length) {
|
|
59
118
|
if (!this.requestHandler) {
|
|
60
|
-
|
|
119
|
+
throw new Error('No request handler set');
|
|
61
120
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
121
|
+
// Build Modbus request buffer
|
|
122
|
+
const request = Buffer.alloc(6);
|
|
123
|
+
request[0] = unitID;
|
|
124
|
+
request[1] = functionCode;
|
|
125
|
+
request.writeUInt16BE(addr, 2);
|
|
126
|
+
request.writeUInt16BE(length, 4);
|
|
127
|
+
// Call handler
|
|
128
|
+
const response = await this.requestHandler(unitID, request);
|
|
129
|
+
// Parse response
|
|
130
|
+
const byteCount = response[2];
|
|
131
|
+
if (byteCount === undefined || response.length < 3 + byteCount) {
|
|
132
|
+
throw new Error('Invalid response');
|
|
69
133
|
}
|
|
70
|
-
|
|
71
|
-
|
|
134
|
+
// Extract register values
|
|
135
|
+
const values = [];
|
|
136
|
+
for (let i = 0; i < byteCount / 2; i++) {
|
|
137
|
+
values.push(response.readUInt16BE(3 + i * 2));
|
|
72
138
|
}
|
|
139
|
+
return values;
|
|
73
140
|
}
|
|
74
141
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* Returns CRC as a 16-bit value that can be written with writeUInt16LE
|
|
142
|
+
* Handle register write operations
|
|
78
143
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
144
|
+
async handleRegisterWrite(unitID, functionCode, addr, values) {
|
|
145
|
+
if (!this.requestHandler) {
|
|
146
|
+
throw new Error('No request handler set');
|
|
147
|
+
}
|
|
148
|
+
// Build Modbus request buffer
|
|
149
|
+
let request;
|
|
150
|
+
if (functionCode === 0x06) {
|
|
151
|
+
// Write single register
|
|
152
|
+
request = Buffer.alloc(6);
|
|
153
|
+
request[0] = unitID;
|
|
154
|
+
request[1] = functionCode;
|
|
155
|
+
request.writeUInt16BE(addr, 2);
|
|
156
|
+
request.writeUInt16BE(values[0] ?? 0, 4);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
// Write multiple registers
|
|
160
|
+
const byteCount = values.length * 2;
|
|
161
|
+
request = Buffer.alloc(7 + byteCount);
|
|
162
|
+
request[0] = unitID;
|
|
163
|
+
request[1] = functionCode;
|
|
164
|
+
request.writeUInt16BE(addr, 2);
|
|
165
|
+
request.writeUInt16BE(values.length, 4);
|
|
166
|
+
request[6] = byteCount;
|
|
167
|
+
for (let i = 0; i < values.length; i++) {
|
|
168
|
+
request.writeUInt16BE(values[i] ?? 0, 7 + i * 2);
|
|
91
169
|
}
|
|
92
170
|
}
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
return ((crc & 0xff) << 8) | ((crc >> 8) & 0xff);
|
|
171
|
+
// Call handler and ignore response
|
|
172
|
+
await this.requestHandler(unitID, request);
|
|
96
173
|
}
|
|
97
174
|
/**
|
|
98
|
-
*
|
|
175
|
+
* Handle coil read operations
|
|
99
176
|
*/
|
|
100
|
-
|
|
101
|
-
if (
|
|
102
|
-
|
|
177
|
+
async handleCoilRead(unitID, functionCode, addr, length) {
|
|
178
|
+
if (!this.requestHandler) {
|
|
179
|
+
throw new Error('No request handler set');
|
|
180
|
+
}
|
|
181
|
+
// Build Modbus request buffer
|
|
182
|
+
const request = Buffer.alloc(6);
|
|
183
|
+
request[0] = unitID;
|
|
184
|
+
request[1] = functionCode;
|
|
185
|
+
request.writeUInt16BE(addr, 2);
|
|
186
|
+
request.writeUInt16BE(length, 4);
|
|
187
|
+
// Call handler
|
|
188
|
+
const response = await this.requestHandler(unitID, request);
|
|
189
|
+
// Parse response - get first bit from first byte of coil data
|
|
190
|
+
const byteCount = response[2];
|
|
191
|
+
if (byteCount === undefined || response.length < 4) {
|
|
192
|
+
throw new Error('Invalid response');
|
|
103
193
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return receivedCRC === calculatedCRC;
|
|
194
|
+
const coilByte = response[3];
|
|
195
|
+
if (coilByte === undefined) {
|
|
196
|
+
throw new Error('Invalid response');
|
|
197
|
+
}
|
|
198
|
+
return (coilByte & 0x01) === 1;
|
|
110
199
|
}
|
|
111
200
|
/**
|
|
112
|
-
*
|
|
113
|
-
* @internal
|
|
201
|
+
* Handle coil write operations
|
|
114
202
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
203
|
+
async handleCoilWrite(unitID, functionCode, addr, value) {
|
|
204
|
+
if (!this.requestHandler) {
|
|
205
|
+
throw new Error('No request handler set');
|
|
206
|
+
}
|
|
207
|
+
// Build Modbus request buffer
|
|
208
|
+
const request = Buffer.alloc(6);
|
|
209
|
+
request[0] = unitID;
|
|
210
|
+
request[1] = functionCode;
|
|
211
|
+
request.writeUInt16BE(addr, 2);
|
|
212
|
+
request.writeUInt16BE(value ? 0xff00 : 0x0000, 4);
|
|
213
|
+
// Call handler and ignore response
|
|
214
|
+
await this.requestHandler(unitID, request);
|
|
121
215
|
}
|
|
122
216
|
}
|
|
123
217
|
exports.RtuTransport = RtuTransport;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rtu.js","sourceRoot":"","sources":["../../../../src/transports/rtu.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"rtu.js","sourceRoot":"","sources":["../../../../src/transports/rtu.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,iDAA4C;AAG5C,uCAAyC;AAUzC,MAAa,YAAa,SAAQ,uBAAa;IAM7C,YAAY,MAA0B;QACpC,KAAK,EAAE,CAAA;QAHD,YAAO,GAAG,KAAK,CAAA;QAIrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAA;QAC9C,CAAC;QAED,oFAAoF;QACpF,MAAM,aAAa,GAAmB;YACpC,kBAAkB,EAAE,KAAK,EAAE,IAAY,EAAE,MAAc,EAAE,EAAE;gBACzD,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;YACvD,CAAC;YACD,gBAAgB,EAAE,KAAK,EAAE,IAAY,EAAE,MAAc,EAAE,EAAE;gBACvD,OAAO,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;YACvD,CAAC;YACD,2BAA2B,EAAE,KAAK,EAAE,IAAY,EAAE,MAAc,EAAE,MAAc,EAAE,EAAE;gBAClF,OAAO,IAAI,CAAC,kBAAkB,CAA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Ac,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC5C,CAAC;CACF;AA/PD,oCA+PC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TCP transport for Modbus emulator
|
|
3
|
+
*
|
|
4
|
+
* This implementation uses modbus-serial ServerTCP for protocol handling.
|
|
5
|
+
*/
|
|
6
|
+
import { BaseTransport } from './base.js';
|
|
7
|
+
export interface TcpTransportConfig {
|
|
8
|
+
host: string;
|
|
9
|
+
port: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class TcpTransport extends BaseTransport {
|
|
12
|
+
private config;
|
|
13
|
+
private requestHandler?;
|
|
14
|
+
private server?;
|
|
15
|
+
private started;
|
|
16
|
+
constructor(config: TcpTransportConfig);
|
|
17
|
+
start(): Promise<void>;
|
|
18
|
+
stop(): Promise<void>;
|
|
19
|
+
send(_slaveId: number, _response: Buffer): Promise<void>;
|
|
20
|
+
onRequest(handler: (slaveId: number, request: Buffer) => Promise<Buffer>): void;
|
|
21
|
+
/**
|
|
22
|
+
* Handle register read operations
|
|
23
|
+
*/
|
|
24
|
+
private handleRegisterRead;
|
|
25
|
+
/**
|
|
26
|
+
* Handle register write operations
|
|
27
|
+
*/
|
|
28
|
+
private handleRegisterWrite;
|
|
29
|
+
/**
|
|
30
|
+
* Handle coil read operations
|
|
31
|
+
*/
|
|
32
|
+
private handleCoilRead;
|
|
33
|
+
/**
|
|
34
|
+
* Handle coil write operations
|
|
35
|
+
*/
|
|
36
|
+
private handleCoilWrite;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=tcp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tcp.d.ts","sourceRoot":"","sources":["../../../../src/transports/tcp.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAEzC,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;CACb;AAED,qBAAa,YAAa,SAAQ,aAAa;IAC7C,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,cAAc,CAAC,CAAuD;IAC9E,OAAO,CAAC,MAAM,CAAC,CAAW;IAC1B,OAAO,CAAC,OAAO,CAAQ;gBAEX,MAAM,EAAE,kBAAkB;IAKhC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA8DtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAuB3B,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASxD,SAAS,CAAC,OAAO,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,IAAI;IAI/E;;OAEG;YACW,kBAAkB;IAmChC;;OAEG;YACW,mBAAmB;IAqCjC;;OAEG;YACW,cAAc;IAiC5B;;OAEG;YACW,eAAe;CAoB9B"}
|