@ya-modbus/transport 0.9.0 → 0.10.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 +68 -0
- package/dist/cjs/src/connection.d.ts +90 -0
- package/dist/cjs/src/connection.d.ts.map +1 -0
- package/dist/cjs/src/connection.js +217 -0
- package/dist/cjs/src/connection.js.map +1 -0
- package/dist/cjs/src/create-modbus-transport.d.ts +4 -2
- package/dist/cjs/src/create-modbus-transport.d.ts.map +1 -1
- package/dist/cjs/src/create-modbus-transport.js +29 -28
- package/dist/cjs/src/create-modbus-transport.js.map +1 -1
- package/dist/cjs/src/factory.d.ts +2 -2
- package/dist/cjs/src/factory.js +12 -12
- package/dist/cjs/src/factory.js.map +1 -1
- package/dist/cjs/src/index.d.ts +1 -0
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js +4 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/manager.d.ts +10 -0
- package/dist/cjs/src/manager.d.ts.map +1 -1
- package/dist/cjs/src/manager.js +18 -6
- package/dist/cjs/src/manager.js.map +1 -1
- package/dist/cjs/src/rtu-transport.d.ts +4 -0
- package/dist/cjs/src/rtu-transport.d.ts.map +1 -1
- package/dist/cjs/src/rtu-transport.js +19 -10
- package/dist/cjs/src/rtu-transport.js.map +1 -1
- package/dist/cjs/src/slave-transport.d.ts +10 -5
- package/dist/cjs/src/slave-transport.d.ts.map +1 -1
- package/dist/cjs/src/slave-transport.js +42 -39
- package/dist/cjs/src/slave-transport.js.map +1 -1
- package/dist/cjs/src/tcp-transport.d.ts +3 -0
- package/dist/cjs/src/tcp-transport.d.ts.map +1 -1
- package/dist/cjs/src/tcp-transport.js +14 -5
- package/dist/cjs/src/tcp-transport.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/connection.d.ts +90 -0
- package/dist/esm/src/connection.d.ts.map +1 -0
- package/dist/esm/src/connection.js +211 -0
- package/dist/esm/src/connection.js.map +1 -0
- package/dist/esm/src/create-modbus-transport.d.ts +4 -2
- package/dist/esm/src/create-modbus-transport.d.ts.map +1 -1
- package/dist/esm/src/create-modbus-transport.js +29 -28
- package/dist/esm/src/create-modbus-transport.js.map +1 -1
- package/dist/esm/src/factory.d.ts +2 -2
- package/dist/esm/src/factory.js +12 -12
- package/dist/esm/src/factory.js.map +1 -1
- package/dist/esm/src/index.d.ts +1 -0
- package/dist/esm/src/index.d.ts.map +1 -1
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/manager.d.ts +10 -0
- package/dist/esm/src/manager.d.ts.map +1 -1
- package/dist/esm/src/manager.js +18 -6
- package/dist/esm/src/manager.js.map +1 -1
- package/dist/esm/src/rtu-transport.d.ts +4 -0
- package/dist/esm/src/rtu-transport.d.ts.map +1 -1
- package/dist/esm/src/rtu-transport.js +19 -10
- package/dist/esm/src/rtu-transport.js.map +1 -1
- package/dist/esm/src/slave-transport.d.ts +10 -5
- package/dist/esm/src/slave-transport.d.ts.map +1 -1
- package/dist/esm/src/slave-transport.js +42 -39
- package/dist/esm/src/slave-transport.js.map +1 -1
- package/dist/esm/src/tcp-transport.d.ts +3 -0
- package/dist/esm/src/tcp-transport.d.ts.map +1 -1
- package/dist/esm/src/tcp-transport.js +14 -5
- package/dist/esm/src/tcp-transport.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
package/dist/esm/src/manager.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Mutex } from 'async-mutex';
|
|
2
2
|
import ModbusRTU from 'modbus-serial';
|
|
3
3
|
import { DEFAULT_TIMEOUT_MS } from './client-config.js';
|
|
4
|
+
import { ModbusConnection } from './connection.js';
|
|
4
5
|
import { SlaveTransport } from './slave-transport.js';
|
|
5
6
|
/**
|
|
6
7
|
* TransportManager pools modbus-serial clients and provides slave-specific transports.
|
|
@@ -12,6 +13,9 @@ import { SlaveTransport } from './slave-transport.js';
|
|
|
12
13
|
* - Returns SlaveTransport instances that set the slave ID before each operation
|
|
13
14
|
* - Multiple devices on the same connection share a single client and mutex
|
|
14
15
|
* - All operations are serialized using async-mutex to prevent race conditions
|
|
16
|
+
* - A lost connection (USB adapter unplugged, TCP connection closed) drops the client.
|
|
17
|
+
* The first request after a backoff opens a new client for all devices on the
|
|
18
|
+
* connection (see ModbusConnection)
|
|
15
19
|
*
|
|
16
20
|
* This architecture solves the multi-device problem:
|
|
17
21
|
* - Each device gets its own SlaveTransport with its own slave ID
|
|
@@ -45,9 +49,10 @@ export class TransportManager {
|
|
|
45
49
|
let entry = this.connections.get(key);
|
|
46
50
|
if (!entry) {
|
|
47
51
|
// Create new client for this physical connection
|
|
48
|
-
const
|
|
52
|
+
const open = isRTU ? () => this.createRTUClient(config) : () => this.createTCPClient(config);
|
|
53
|
+
const client = await open();
|
|
49
54
|
entry = {
|
|
50
|
-
client,
|
|
55
|
+
connection: new ModbusConnection(client, this.getTarget(config), open),
|
|
51
56
|
mutex: new Mutex(),
|
|
52
57
|
config,
|
|
53
58
|
isRTU,
|
|
@@ -55,7 +60,7 @@ export class TransportManager {
|
|
|
55
60
|
this.connections.set(key, entry);
|
|
56
61
|
}
|
|
57
62
|
// Return a new SlaveTransport for this specific slave
|
|
58
|
-
return new SlaveTransport(config.slaveId, entry.
|
|
63
|
+
return new SlaveTransport(config.slaveId, entry.connection, entry.mutex, config.maxRetries ?? 3, config.logger);
|
|
59
64
|
}
|
|
60
65
|
/**
|
|
61
66
|
* Get statistics about managed connections
|
|
@@ -77,9 +82,7 @@ export class TransportManager {
|
|
|
77
82
|
async closeAll() {
|
|
78
83
|
const closePromises = Array.from(this.connections.values()).map(async (entry) => {
|
|
79
84
|
try {
|
|
80
|
-
await
|
|
81
|
-
entry.client.close(resolve);
|
|
82
|
-
});
|
|
85
|
+
await entry.connection.close();
|
|
83
86
|
}
|
|
84
87
|
catch (error) {
|
|
85
88
|
// Log but don't throw - we want to close all connections
|
|
@@ -105,6 +108,15 @@ export class TransportManager {
|
|
|
105
108
|
// TCP config - safe to access host/port because isRTUConfig returned false
|
|
106
109
|
return `tcp:${config.host}:${config.port ?? 502}`;
|
|
107
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Describe a physical connection for error messages
|
|
113
|
+
*
|
|
114
|
+
* @param config - Transport configuration
|
|
115
|
+
* @returns Serial port path for RTU, host:port for TCP
|
|
116
|
+
*/
|
|
117
|
+
getTarget(config) {
|
|
118
|
+
return this.isRTUConfig(config) ? config.port : `${config.host}:${config.port ?? 502}`;
|
|
119
|
+
}
|
|
108
120
|
/**
|
|
109
121
|
* Create and configure an RTU modbus-serial client.
|
|
110
122
|
* Does NOT set slave ID - that's handled by SlaveTransport.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAqBrD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,gBAAgB;IAA7B;QACmB,gBAAW,GAAG,IAAI,GAAG,EAA2B,CAAA;IAoJnE,CAAC;IAlJC;;;;;;;OAOG;IACH,KAAK,CA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M,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,IAAI,GAAG,EAAE,CAAA;IACxF,CAAC;IAED;;;;;;OAMG;IACK,KAAK,CAAC,eAAe,CAAC,MAAiB;QAC7C,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAA;QAE9B,MAAM,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,IAAI,EAAE;YAC3C,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;SAC1B,CAAC,CAAA;QAEF,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,IAAI,kBAAkB,CAAC,CAAA;QAEvD,OAAO,MAAM,CAAA;IACf,CAAC;IAED;;;;;;OAMG;IACK,KAAK,CAAC,eAAe,CAAC,MAAiB;QAC7C,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAA;QAE9B,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC,CAAA;QAElE,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO,IAAI,kBAAkB,CAAC,CAAA;QAEvD,OAAO,MAAM,CAAA;IACf,CAAC;IAED;;;;;OAKG;IACK,WAAW,CAAC,MAAuB;QACzC,OAAO,MAAM,IAAI,MAAM,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,CAAA;IACnF,CAAC;CACF"}
|
|
@@ -26,6 +26,10 @@ export interface RTUConfig {
|
|
|
26
26
|
/**
|
|
27
27
|
* Create an RTU transport
|
|
28
28
|
*
|
|
29
|
+
* When the serial port is lost (for example the USB adapter is unplugged), requests
|
|
30
|
+
* reject with a ConnectionLostError and the port is reopened on a later request
|
|
31
|
+
* (see ModbusConnection).
|
|
32
|
+
*
|
|
29
33
|
* @param config - RTU configuration
|
|
30
34
|
* @returns Transport implementation for RTU
|
|
31
35
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rtu-transport.d.ts","sourceRoot":"","sources":["../../../src/rtu-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"rtu-transport.d.ts","sourceRoot":"","sources":["../../../src/rtu-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAM9F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,kDAAkD;IAClD,IAAI,EAAE,MAAM,CAAA;IACZ,gBAAgB;IAChB,QAAQ,EAAE,QAAQ,CAAA;IAClB,gBAAgB;IAChB,QAAQ,EAAE,QAAQ,CAAA;IAClB,aAAa;IACb,MAAM,EAAE,MAAM,CAAA;IACd,gBAAgB;IAChB,QAAQ,EAAE,QAAQ,CAAA;IAClB,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B,oEAAoE;IACpE,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,4DAA4D;IAC5D,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CACjC;AAED;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAkB9E"}
|
|
@@ -1,22 +1,31 @@
|
|
|
1
1
|
import ModbusRTU from 'modbus-serial';
|
|
2
2
|
import { configureClient } from './client-config.js';
|
|
3
|
+
import { ModbusConnection } from './connection.js';
|
|
3
4
|
import { createModbusTransport } from './create-modbus-transport.js';
|
|
4
5
|
/**
|
|
5
6
|
* Create an RTU transport
|
|
6
7
|
*
|
|
8
|
+
* When the serial port is lost (for example the USB adapter is unplugged), requests
|
|
9
|
+
* reject with a ConnectionLostError and the port is reopened on a later request
|
|
10
|
+
* (see ModbusConnection).
|
|
11
|
+
*
|
|
7
12
|
* @param config - RTU configuration
|
|
8
13
|
* @returns Transport implementation for RTU
|
|
9
14
|
*/
|
|
10
15
|
export async function createRTUTransport(config) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
const open = async () => {
|
|
17
|
+
const client = new ModbusRTU();
|
|
18
|
+
// Connect to serial port
|
|
19
|
+
await client.connectRTUBuffered(config.port, {
|
|
20
|
+
baudRate: config.baudRate,
|
|
21
|
+
dataBits: config.dataBits,
|
|
22
|
+
parity: config.parity,
|
|
23
|
+
stopBits: config.stopBits,
|
|
24
|
+
});
|
|
25
|
+
configureClient(client, config.slaveId, config.timeout);
|
|
26
|
+
return client;
|
|
27
|
+
};
|
|
28
|
+
const connection = new ModbusConnection(await open(), config.port, open);
|
|
29
|
+
return createModbusTransport(connection, config.maxRetries, config.logger);
|
|
21
30
|
}
|
|
22
31
|
//# sourceMappingURL=rtu-transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rtu-transport.js","sourceRoot":"","sources":["../../../src/rtu-transport.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AA2BpE
|
|
1
|
+
{"version":3,"file":"rtu-transport.js","sourceRoot":"","sources":["../../../src/rtu-transport.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AA2BpE;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,MAAiB;IACxD,MAAM,IAAI,GAAG,KAAK,IAAwB,EAAE;QAC1C,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAA;QAE9B,yBAAyB;QACzB,MAAM,MAAM,CAAC,kBAAkB,CAAC,MAAM,CAAC,IAAI,EAAE;YAC3C,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;SAC1B,CAAC,CAAA;QAEF,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;QACvD,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,MAAM,IAAI,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IACxE,OAAO,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;AAC5E,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Transport } from '@ya-modbus/driver-types';
|
|
2
2
|
import type { Mutex } from 'async-mutex';
|
|
3
3
|
import type ModbusRTU from 'modbus-serial';
|
|
4
|
+
import { type ModbusConnection } from './connection.js';
|
|
4
5
|
import { type RetryLogger } from './retry.js';
|
|
5
6
|
/**
|
|
6
7
|
* SlaveTransport wraps a shared ModbusRTU client with slave-specific addressing.
|
|
@@ -9,15 +10,19 @@ import { type RetryLogger } from './retry.js';
|
|
|
9
10
|
* each communicating with a different slave device. The slave ID is set before each operation
|
|
10
11
|
* to ensure requests are directed to the correct device.
|
|
11
12
|
*
|
|
13
|
+
* Requests on a lost connection reject with a ConnectionLostError. When the shared
|
|
14
|
+
* ModbusConnection can reopen its client, all devices on the connection use the new client.
|
|
15
|
+
*
|
|
12
16
|
* @example
|
|
13
17
|
* ```typescript
|
|
14
18
|
* const client = new ModbusRTU()
|
|
15
19
|
* await client.connectRTUBuffered('/dev/ttyUSB0', { baudRate: 9600 })
|
|
20
|
+
* const connection = new ModbusConnection(client, '/dev/ttyUSB0')
|
|
16
21
|
* const mutex = new Mutex()
|
|
17
22
|
*
|
|
18
23
|
* // Two devices on the same serial port
|
|
19
|
-
* const device1 = new SlaveTransport(1,
|
|
20
|
-
* const device2 = new SlaveTransport(2,
|
|
24
|
+
* const device1 = new SlaveTransport(1, connection, mutex, 3)
|
|
25
|
+
* const device2 = new SlaveTransport(2, connection, mutex, 3)
|
|
21
26
|
*
|
|
22
27
|
* // Each device talks to its own slave ID
|
|
23
28
|
* await device1.readHoldingRegisters(0, 10) // Reads from slave 1
|
|
@@ -26,18 +31,18 @@ import { type RetryLogger } from './retry.js';
|
|
|
26
31
|
*/
|
|
27
32
|
export declare class SlaveTransport implements Transport {
|
|
28
33
|
private readonly slaveId;
|
|
29
|
-
private readonly client;
|
|
30
34
|
private readonly mutex;
|
|
31
35
|
private readonly maxRetries;
|
|
32
36
|
private readonly logger?;
|
|
37
|
+
private readonly connection;
|
|
33
38
|
/**
|
|
34
39
|
* @param slaveId - Modbus slave/unit ID for this device
|
|
35
|
-
* @param client - Shared ModbusRTU client
|
|
40
|
+
* @param client - Shared connection, or a ModbusRTU client that is not shared with other transports
|
|
36
41
|
* @param mutex - Shared mutex for serializing operations on the bus
|
|
37
42
|
* @param maxRetries - Maximum retry attempts for failed operations (default: 3)
|
|
38
43
|
* @param logger - Optional callback to log retry attempts
|
|
39
44
|
*/
|
|
40
|
-
constructor(slaveId: number, client: ModbusRTU, mutex: Mutex, maxRetries?: number, logger?: RetryLogger | undefined);
|
|
45
|
+
constructor(slaveId: number, client: ModbusConnection | ModbusRTU, mutex: Mutex, maxRetries?: number, logger?: RetryLogger | undefined);
|
|
41
46
|
readHoldingRegisters(address: number, count: number): Promise<Buffer>;
|
|
42
47
|
readInputRegisters(address: number, count: number): Promise<Buffer>;
|
|
43
48
|
readCoils(address: number, count: number): Promise<Buffer>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slave-transport.d.ts","sourceRoot":"","sources":["../../../src/slave-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AAExD
|
|
1
|
+
{"version":3,"file":"slave-transport.d.ts","sourceRoot":"","sources":["../../../src/slave-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,KAAK,SAAS,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAgB,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AACrE,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,cAAe,YAAW,SAAS;IAW5C,OAAO,CAAC,QAAQ,CAAC,OAAO;IAExB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;IAd1B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAkB;IAE7C;;;;;;OAMG;gBAEgB,OAAO,EAAE,MAAM,EAChC,MAAM,EAAE,gBAAgB,GAAG,SAAS,EACnB,KAAK,EAAE,KAAK,EACZ,UAAU,GAAE,MAAU,EACtB,MAAM,CAAC,EAAE,WAAW,YAAA;IAKjC,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAerE,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAenE,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAe1D,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAenE,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAclE,sBAAsB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IActE,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAc/D,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAsBxE;;;;;;;;;OASG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAI7B"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toConnection } from './connection.js';
|
|
1
2
|
import { withRetry } from './retry.js';
|
|
2
3
|
/**
|
|
3
4
|
* SlaveTransport wraps a shared ModbusRTU client with slave-specific addressing.
|
|
@@ -6,15 +7,19 @@ import { withRetry } from './retry.js';
|
|
|
6
7
|
* each communicating with a different slave device. The slave ID is set before each operation
|
|
7
8
|
* to ensure requests are directed to the correct device.
|
|
8
9
|
*
|
|
10
|
+
* Requests on a lost connection reject with a ConnectionLostError. When the shared
|
|
11
|
+
* ModbusConnection can reopen its client, all devices on the connection use the new client.
|
|
12
|
+
*
|
|
9
13
|
* @example
|
|
10
14
|
* ```typescript
|
|
11
15
|
* const client = new ModbusRTU()
|
|
12
16
|
* await client.connectRTUBuffered('/dev/ttyUSB0', { baudRate: 9600 })
|
|
17
|
+
* const connection = new ModbusConnection(client, '/dev/ttyUSB0')
|
|
13
18
|
* const mutex = new Mutex()
|
|
14
19
|
*
|
|
15
20
|
* // Two devices on the same serial port
|
|
16
|
-
* const device1 = new SlaveTransport(1,
|
|
17
|
-
* const device2 = new SlaveTransport(2,
|
|
21
|
+
* const device1 = new SlaveTransport(1, connection, mutex, 3)
|
|
22
|
+
* const device2 = new SlaveTransport(2, connection, mutex, 3)
|
|
18
23
|
*
|
|
19
24
|
* // Each device talks to its own slave ID
|
|
20
25
|
* await device1.readHoldingRegisters(0, 10) // Reads from slave 1
|
|
@@ -24,82 +29,82 @@ import { withRetry } from './retry.js';
|
|
|
24
29
|
export class SlaveTransport {
|
|
25
30
|
/**
|
|
26
31
|
* @param slaveId - Modbus slave/unit ID for this device
|
|
27
|
-
* @param client - Shared ModbusRTU client
|
|
32
|
+
* @param client - Shared connection, or a ModbusRTU client that is not shared with other transports
|
|
28
33
|
* @param mutex - Shared mutex for serializing operations on the bus
|
|
29
34
|
* @param maxRetries - Maximum retry attempts for failed operations (default: 3)
|
|
30
35
|
* @param logger - Optional callback to log retry attempts
|
|
31
36
|
*/
|
|
32
37
|
constructor(slaveId, client, mutex, maxRetries = 3, logger) {
|
|
33
38
|
this.slaveId = slaveId;
|
|
34
|
-
this.client = client;
|
|
35
39
|
this.mutex = mutex;
|
|
36
40
|
this.maxRetries = maxRetries;
|
|
37
41
|
this.logger = logger;
|
|
42
|
+
this.connection = toConnection(client);
|
|
38
43
|
}
|
|
39
44
|
async readHoldingRegisters(address, count) {
|
|
40
45
|
return this.mutex.runExclusive(async () => {
|
|
41
|
-
return withRetry(async () => {
|
|
42
|
-
|
|
43
|
-
const result = await
|
|
46
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
47
|
+
client.setID(this.slaveId);
|
|
48
|
+
const result = await client.readHoldingRegisters(address, count);
|
|
44
49
|
return result.buffer;
|
|
45
|
-
}, this.maxRetries, this.logger);
|
|
50
|
+
}), this.maxRetries, this.logger);
|
|
46
51
|
});
|
|
47
52
|
}
|
|
48
53
|
async readInputRegisters(address, count) {
|
|
49
54
|
return this.mutex.runExclusive(async () => {
|
|
50
|
-
return withRetry(async () => {
|
|
51
|
-
|
|
52
|
-
const result = await
|
|
55
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
56
|
+
client.setID(this.slaveId);
|
|
57
|
+
const result = await client.readInputRegisters(address, count);
|
|
53
58
|
return result.buffer;
|
|
54
|
-
}, this.maxRetries, this.logger);
|
|
59
|
+
}), this.maxRetries, this.logger);
|
|
55
60
|
});
|
|
56
61
|
}
|
|
57
62
|
async readCoils(address, count) {
|
|
58
63
|
return this.mutex.runExclusive(async () => {
|
|
59
|
-
return withRetry(async () => {
|
|
60
|
-
|
|
61
|
-
const result = await
|
|
64
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
65
|
+
client.setID(this.slaveId);
|
|
66
|
+
const result = await client.readCoils(address, count);
|
|
62
67
|
return result.buffer;
|
|
63
|
-
}, this.maxRetries, this.logger);
|
|
68
|
+
}), this.maxRetries, this.logger);
|
|
64
69
|
});
|
|
65
70
|
}
|
|
66
71
|
async readDiscreteInputs(address, count) {
|
|
67
72
|
return this.mutex.runExclusive(async () => {
|
|
68
|
-
return withRetry(async () => {
|
|
69
|
-
|
|
70
|
-
const result = await
|
|
73
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
74
|
+
client.setID(this.slaveId);
|
|
75
|
+
const result = await client.readDiscreteInputs(address, count);
|
|
71
76
|
return result.buffer;
|
|
72
|
-
}, this.maxRetries, this.logger);
|
|
77
|
+
}), this.maxRetries, this.logger);
|
|
73
78
|
});
|
|
74
79
|
}
|
|
75
80
|
async writeSingleRegister(address, value) {
|
|
76
81
|
return this.mutex.runExclusive(async () => {
|
|
77
|
-
return withRetry(async () => {
|
|
78
|
-
|
|
79
|
-
await
|
|
80
|
-
}, this.maxRetries, this.logger);
|
|
82
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
83
|
+
client.setID(this.slaveId);
|
|
84
|
+
await client.writeRegister(address, value);
|
|
85
|
+
}), this.maxRetries, this.logger);
|
|
81
86
|
});
|
|
82
87
|
}
|
|
83
88
|
async writeMultipleRegisters(address, values) {
|
|
84
89
|
return this.mutex.runExclusive(async () => {
|
|
85
|
-
return withRetry(async () => {
|
|
86
|
-
|
|
87
|
-
await
|
|
88
|
-
}, this.maxRetries, this.logger);
|
|
90
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
91
|
+
client.setID(this.slaveId);
|
|
92
|
+
await client.writeRegisters(address, values);
|
|
93
|
+
}), this.maxRetries, this.logger);
|
|
89
94
|
});
|
|
90
95
|
}
|
|
91
96
|
async writeSingleCoil(address, value) {
|
|
92
97
|
return this.mutex.runExclusive(async () => {
|
|
93
|
-
return withRetry(async () => {
|
|
94
|
-
|
|
95
|
-
await
|
|
96
|
-
}, this.maxRetries, this.logger);
|
|
98
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
99
|
+
client.setID(this.slaveId);
|
|
100
|
+
await client.writeCoil(address, value);
|
|
101
|
+
}), this.maxRetries, this.logger);
|
|
97
102
|
});
|
|
98
103
|
}
|
|
99
104
|
async writeMultipleCoils(address, values) {
|
|
100
105
|
return this.mutex.runExclusive(async () => {
|
|
101
|
-
return withRetry(async () => {
|
|
102
|
-
|
|
106
|
+
return withRetry(() => this.connection.run(async (client) => {
|
|
107
|
+
client.setID(this.slaveId);
|
|
103
108
|
// Convert Buffer to boolean array
|
|
104
109
|
const bools = [];
|
|
105
110
|
for (let i = 0; i < values.length * 8; i++) {
|
|
@@ -108,8 +113,8 @@ export class SlaveTransport {
|
|
|
108
113
|
const byte = values[byteIndex];
|
|
109
114
|
bools.push((byte & (1 << bitIndex)) !== 0);
|
|
110
115
|
}
|
|
111
|
-
await
|
|
112
|
-
}, this.maxRetries, this.logger);
|
|
116
|
+
await client.writeCoils(address, bools);
|
|
117
|
+
}), this.maxRetries, this.logger);
|
|
113
118
|
});
|
|
114
119
|
}
|
|
115
120
|
/**
|
|
@@ -124,9 +129,7 @@ export class SlaveTransport {
|
|
|
124
129
|
*/
|
|
125
130
|
async close() {
|
|
126
131
|
// Don't use mutex for close operation
|
|
127
|
-
return
|
|
128
|
-
this.client.close(resolve);
|
|
129
|
-
});
|
|
132
|
+
return this.connection.close();
|
|
130
133
|
}
|
|
131
134
|
}
|
|
132
135
|
//# sourceMappingURL=slave-transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slave-transport.js","sourceRoot":"","sources":["../../../src/slave-transport.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAoB,MAAM,YAAY,CAAA;AAExD
|
|
1
|
+
{"version":3,"file":"slave-transport.js","sourceRoot":"","sources":["../../../src/slave-transport.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAyB,MAAM,iBAAiB,CAAA;AACrE,OAAO,EAAE,SAAS,EAAoB,MAAM,YAAY,CAAA;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,cAAc;IAGzB;;;;;;OAMG;IACH,YACmB,OAAe,EAChC,MAAoC,EACnB,KAAY,EACZ,aAAqB,CAAC,EACtB,MAAoB;QAJpB,YAAO,GAAP,OAAO,CAAQ;QAEf,UAAK,GAAL,KAAK,CAAO;QACZ,eAAU,GAAV,UAAU,CAAY;QACtB,WAAM,GAAN,MAAM,CAAc;QAErC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IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;gBACnC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;gBAC1B,kCAAkC;gBAClC,MAAM,KAAK,GAAc,EAAE,CAAA;gBAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;oBAC3C,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;oBACnC,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,CAAA;oBACtB,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAW,CAAA;oBACxC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA;gBAC5C,CAAC;gBACD,MAAM,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;YACzC,CAAC,CAAC,EACJ,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,MAAM,CACZ,CAAA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,KAAK;QACT,sCAAsC;QACtC,OAAO,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAA;IAChC,CAAC;CACF"}
|
|
@@ -20,6 +20,9 @@ export interface TCPConfig {
|
|
|
20
20
|
/**
|
|
21
21
|
* Create a TCP transport
|
|
22
22
|
*
|
|
23
|
+
* When the connection is lost, requests reject with a ConnectionLostError and the
|
|
24
|
+
* connection is reopened on a later request (see ModbusConnection).
|
|
25
|
+
*
|
|
23
26
|
* @param config - TCP configuration
|
|
24
27
|
* @returns Transport implementation for TCP
|
|
25
28
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tcp-transport.d.ts","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;
|
|
1
|
+
{"version":3,"file":"tcp-transport.d.ts","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAMxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAA;IACZ,8BAA8B;IAC9B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B,oEAAoE;IACpE,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC/B,4DAA4D;IAC5D,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;CACjC;AAED;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAc9E"}
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
import ModbusRTU from 'modbus-serial';
|
|
2
2
|
import { configureClient } from './client-config.js';
|
|
3
|
+
import { ModbusConnection } from './connection.js';
|
|
3
4
|
import { createModbusTransport } from './create-modbus-transport.js';
|
|
4
5
|
/**
|
|
5
6
|
* Create a TCP transport
|
|
6
7
|
*
|
|
8
|
+
* When the connection is lost, requests reject with a ConnectionLostError and the
|
|
9
|
+
* connection is reopened on a later request (see ModbusConnection).
|
|
10
|
+
*
|
|
7
11
|
* @param config - TCP configuration
|
|
8
12
|
* @returns Transport implementation for TCP
|
|
9
13
|
*/
|
|
10
14
|
export async function createTCPTransport(config) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const port = config.port ?? 502;
|
|
16
|
+
const open = async () => {
|
|
17
|
+
const client = new ModbusRTU();
|
|
18
|
+
// Connect to TCP server
|
|
19
|
+
await client.connectTCP(config.host, { port });
|
|
20
|
+
configureClient(client, config.slaveId, config.timeout);
|
|
21
|
+
return client;
|
|
22
|
+
};
|
|
23
|
+
const connection = new ModbusConnection(await open(), `${config.host}:${port}`, open);
|
|
24
|
+
return createModbusTransport(connection, config.maxRetries, config.logger);
|
|
16
25
|
}
|
|
17
26
|
//# sourceMappingURL=tcp-transport.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tcp-transport.js","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AAqBpE
|
|
1
|
+
{"version":3,"file":"tcp-transport.js","sourceRoot":"","sources":["../../../src/tcp-transport.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,eAAe,CAAA;AAErC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AAqBpE;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,MAAiB;IACxD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,IAAI,GAAG,CAAA;IAC/B,MAAM,IAAI,GAAG,KAAK,IAAwB,EAAE;QAC1C,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAA;QAE9B,wBAAwB;QACxB,MAAM,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;QAE9C,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;QACvD,OAAO,MAAM,CAAA;IACf,CAAC,CAAA;IAED,MAAM,UAAU,GAAG,IAAI,gBAAgB,CAAC,MAAM,IAAI,EAAE,EAAE,GAAG,MAAM,CAAC,IAAI,IAAI,IAAI,EAAE,EAAE,IAAI,CAAC,CAAA;IACrF,OAAO,qBAAqB,CAAC,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;AAC5E,CAAC"}
|