Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTU over socket (second PR) #514

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 18.x]
node-version: [14.x, 18.x]

steps:
- uses: actions/checkout@v2
Expand Down
7 changes: 5 additions & 2 deletions ModbusRTU.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class ModbusRTU {
linkTcpRTUBuffered(socket: Socket, options: TcpRTUPortOptions): Promise<void>;
linkTelnet(socket: Socket, options: TelnetPortOptions, next: Function): void;
linkTelnet(socket: Socket, options: TelnetPortOptions): Promise<void>;
connectRTUSocket(socket: Socket, next: Function): void;
connectRTUSocket(socket: Socket): Promise<void>;
connectRTUSocket(socket: Socket, options: SocketOptions, next: Function): void;
connectRTUSocket(socket: Socket, options: SocketOptions): Promise<void>;

// Promise API
setID(id: number): void;
Expand Down Expand Up @@ -152,6 +152,9 @@ export interface TcpRTUPortOptions {
family?: number;
}

export interface SocketOptions {
}

export interface TelnetPortOptions {
port?: number;
}
Expand Down
99 changes: 93 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ This module has not been tested on every single version of NodeJS. For best resu
|-------|----------|
| FC1 "Read Coil Status" | `readCoils(coil, len)` |
| FC2 "Read Input Status" | `readDiscreteInputs(addr, arg)` |
| FC3 "Read Holding Registers" | `readHoldingRegisters(addr, len) ` |
| FC3 "Read Holding Registers" | `readHoldingRegisters(addr, len)`, `readRegistersEnron(addr, len)`* |
| FC4 "Read Input Registers" | `readInputRegisters(addr, len) ` |
| FC5 "Force Single Coil" | `writeCoil(coil, binary) //NOT setCoil` |
| FC6 "Preset Single Register" | `writeRegister(addr, value)` |
| FC15 "Force Multiple Coil" | `writeCoils(addr, valueAry)` |
| FC16 "Preset Multiple Registers" | `writeRegisters(addr, valueAry)` |
| FC15 "Force Multiple Coil" | `writeCoils(addr, valueAry)` |
| FC16 "Preset Multiple Registers" | `writeRegisters(addr, valueAry)`, `writeRegistersEnron(addr, valueAry)`* |
| FC43/14 "Read Device Identification" (supported ports: TCP, RTU) | `readDeviceIdentification(id, obj)` |

\* See examples/server_enron.js for enron configuration example.
###### Client Serial:

* modbus-RTU (SerialPort): Over serial line [require node serialport].
Expand All @@ -84,8 +85,9 @@ This module has not been tested on every single version of NodeJS. For best resu

###### Server

* modbus-TCP (ServerTCP): Over TCP/IP line.

* modbus-TCP ServerTCP(): Encapsulated TCP/IP.
* modbus-RTU Server(): RTU over TCP/IP Socket.
* modbus-Serial ServerSerial(): Over serial.

#### Examples

Expand Down Expand Up @@ -227,7 +229,7 @@ setInterval(function() {
}, 1000);
```
----
###### ModbusTCP Server
###### Modbus TCP Server
``` javascript
// create an empty modbus client
const ModbusRTU = require("modbus-serial");
Expand Down Expand Up @@ -283,6 +285,91 @@ serverTCP.on("socketError", function(err){
});
```
----
###### Modbus Serial Server
``` javascript
const ModbusRTU = require("..");

const holdingRegisters = {};
const coils = {};
const inputRegisters = {};
const discreteInputs = {};

const vector = {
getInputRegister: function(addr) {
return inputRegisters[addr];
},
getMultipleInputRegisters: function(startAddr, length) {
const values = [];
for (let i = 0; i < length; i++) {
values[i] = inputRegisters[startAddr + i];
}
return values;
},
getDiscreteInput: function(addr) {
return discreteInputs[addr];
},
getHoldingRegister: function(addr) {
return holdingRegisters[addr];
},
setRegister: function(addr, value) {
holdingRegisters[addr] = value;
return;
},
getMultipleHoldingRegisters: function(startAddr, length) {
const values = [];
for (let i = 0; i < length; i++) {
values[i] = holdingRegisters[startAddr + i];
}
return values;
},
getCoil: function(addr) {
return coils[addr];
},
setCoil: function(addr, value) {
coils[addr] = value;
return coils[addr];
},
readDeviceIdentification: function() {
return {
0x00: "MyVendorName",
0x01: "MyProductCode",
0x02: "MyMajorMinorRevision",
0x05: "MyModelName",
0x97: "MyExtendedObject1",
0xab: "MyExtendedObject2"
};
}
};

// set the server to answer for modbus requests
const serverSerial = new ModbusRTU.ServerSerial(
vector,
{
port: "/tmp/ttyp0",
debug: true,
unitID: 1
// enron: true,
// enronTables: {
// booleanRange: [1001, 1999],
// shortRange: [3001, 3999],
// longRange: [5001, 5999],
// floatRange: [7001, 7999]
// }
},
{
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: "even"
}
);

serverSerial.on("error", function(err) {
// Handle socket error if needed, can be ignored
console.error(err);
});
```
----
###### Read and Write Modbus ASCII
``` javascript
// create an empty modbus client
Expand Down
22 changes: 21 additions & 1 deletion apis/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const addConnctionAPI = function(Modbus) {
if (options) {
this._enron = options.enron;
this._enronTables = options.enronTables;
this._encapsulatedRTU = options.encapsulatedRTU;
}

// check if we have options
Expand Down Expand Up @@ -97,6 +98,7 @@ const addConnctionAPI = function(Modbus) {
if (options) {
this._enron = options.enron;
this._enronTables = options.enronTables;
this._encapsulatedRTU = options.encapsulatedRTU;
}

// check if we have options
Expand Down Expand Up @@ -325,6 +327,7 @@ const addConnctionAPI = function(Modbus) {
if (options) {
this._enron = options.enron;
this._enronTables = options.enronTables;
this._encapsulatedRTU = options.encapsulatedRTU;
}

// check if we have options
Expand Down Expand Up @@ -382,7 +385,24 @@ const addConnctionAPI = function(Modbus) {
* @param {socket} socket the socket to connect to - required.
* @param {Function} next the function to call next.
*/
cl.connectRTUSocket = function(socket, next) {
cl.connectRTUSocket = function(socket, options, next) {
if (options) {
this._enron = options.enron;
this._enronTables = options.enronTables;
this._encapsulatedRTU = options.encapsulatedRTU;
}

// check if we have options
if (typeof next === "undefined" && typeof options === "function") {
next = options;
options = {};
}

// check if we have options
if (typeof options === "undefined") {
options = {};
}

const thisModbus = this;
this._port = socket;
this._port.open = function(callback) {
Expand Down
81 changes: 81 additions & 0 deletions examples/server_serial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const ModbusRTU = require("..");

const holdingRegisters = {};
const coils = {};
const inputRegisters = {};
const discreteInputs = {};

const vector = {
getInputRegister: function(addr) {
return inputRegisters[addr];
},
getMultipleInputRegisters: function(startAddr, length) {
const values = [];
for (let i = 0; i < length; i++) {
values[i] = inputRegisters[startAddr + i];
}
return values;
},
getDiscreteInput: function(addr) {
return discreteInputs[addr];
},
getHoldingRegister: function(addr) {
return holdingRegisters[addr];
},
setRegister: function(addr, value) {
holdingRegisters[addr] = value;
return;
},
getMultipleHoldingRegisters: function(startAddr, length) {
const values = [];
for (let i = 0; i < length; i++) {
values[i] = holdingRegisters[startAddr + i];
}
return values;
},
getCoil: function(addr) {
return coils[addr];
},
setCoil: function(addr, value) {
coils[addr] = value;
return coils[addr];
},
readDeviceIdentification: function() {
return {
0x00: "MyVendorName",
0x01: "MyProductCode",
0x02: "MyMajorMinorRevision",
0x05: "MyModelName",
0x97: "MyExtendedObject1",
0xab: "MyExtendedObject2"
};
}
};

// set the server to answer for modbus requests
const serverSerial = new ModbusRTU.ServerSerial(
vector,
{
port: "/tmp/ttyp0",
debug: true,
unitID: 1
// enron: true,
// enronTables: {
// booleanRange: [1001, 1999],
// shortRange: [3001, 3999],
// longRange: [5001, 5999],
// floatRange: [7001, 7999]
// }
},
{
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: "even"
}
);

serverSerial.on("error", function(err) {
// Handle socket error if needed, can be ignored
console.error(err);
});
Loading