Skip to content

Commit

Permalink
Serial docs (#508)
Browse files Browse the repository at this point in the history
* Improved Serial documentation.

* Adding Enron function documentation.

* Formatting of MD file

* not ignoring node_modules when linting?

* removing older LTS

---------

Co-authored-by: 1038642892 <[email protected]>
  • Loading branch information
1038642892 and 1038642892 authored Aug 21, 2023
1 parent f62b36d commit 5caff30
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 4 deletions.
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
92 changes: 89 additions & 3 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)` |
| 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 Down Expand Up @@ -228,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 @@ -284,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
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);
});
9 changes: 9 additions & 0 deletions test/servers/serverserial.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ describe("Modbus Serial Server (no serverID)", function() {
port: "/dev/server",
portResponse: clientSerial,
debug: true
},
{
baudRate: 9600
});
});

Expand Down Expand Up @@ -258,6 +261,9 @@ describe("Modbus Serial Server (serverID = requestID)", function() {
portResponse: clientSerial,
debug: true,
unitID: 4
},
{
baudRate: 9600
});
});

Expand Down Expand Up @@ -318,6 +324,9 @@ describe("Modbus Serial Server (serverID != requestID)", function() {
portResponse: clientSerial,
debug: true,
unitID: 4
},
{
baudRate: 9600
});
});

Expand Down

0 comments on commit 5caff30

Please sign in to comment.