Skip to content

Commit

Permalink
Introduce an optional startOfSlaveFrameChar for Modus ASCII (#431)
Browse files Browse the repository at this point in the history
* Option for slave frame delimiter

* Typo in README

* Fixed eslint errors

* Eslint fix 2

* Eslint fix 3

* Esliont fix 4

* Eslint fix 5

* Eslint fix 6

* optional slave frame delimiter fixed

* fixed typo
  • Loading branch information
ThKls authored Oct 26, 2021
1 parent 3acdb3b commit 691fcd1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,40 @@ serverTCP.on("socketError", function(err){
console.error(err);
});
```
----
###### Read and Write Modbus ASCII
``` javascript
// create an empty modbus client
var Modbus = require("modbus-serial");
var client = new Modbus();

// open connection to a serial port
client.connectAsciiSerial(
"/dev/ttyUSB0",
{
baudRate: 9600,
startOfSlaveFrameChar: 0x3A // optional: slave frame delimiter
},
write);

function write() {
client.setID(1);

// write the values 0, 0xffff to registers starting at address 5
// on device number 1.
client.writeRegisters(5, [0 , 0xffff])
.then(read);
}

function read() {
// read the 2 registers starting at address 5
// on device number 1.
client.readHoldingRegisters(5, 2)
.then(console.log);
}
```
----


to get more see [Examples](https://github.com/yaacov/node-modbus-serial/wiki)

Expand Down
10 changes: 9 additions & 1 deletion ports/asciiport.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"use strict";
/* eslint-disable no-ternary */

var util = require("util");
var events = require("events");
var EventEmitter = events.EventEmitter || events;
Expand Down Expand Up @@ -108,6 +110,12 @@ var AsciiPort = function(path, options) {
// options
options = options || {};

// select char for start of slave frame (usually :)
this._startOfSlaveFrameChar =
(options.startOfSlaveFrameChar === undefined)
? 0x3A
: options.startOfSlaveFrameChar;

// disable auto open, as we handle the open
options.autoOpen = false;

Expand All @@ -130,7 +138,7 @@ var AsciiPort = function(path, options) {
modbusSerialDebug(JSON.stringify({ action: "receive serial ascii port strings", data: data, buffer: modbus._buffer }));

// check buffer for start delimiter
var sdIndex = modbus._buffer.indexOf(0x3E); // ascii for '>', as this indicates slave replies
var sdIndex = modbus._buffer.indexOf(modbus._startOfSlaveFrameChar);
if(sdIndex === -1) {
// if not there, reset the buffer and return
modbus._buffer = Buffer.from("");
Expand Down
17 changes: 10 additions & 7 deletions test/ports/asciiport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe("Modbus Ascii port", function() {
mockery.enable({ warnOnReplace: false, useCleanCache: true, warnOnUnregistered: false });
mockery.registerMock("serialport", mock);
var AsciiPort = require("./../../ports/asciiport");
port = new AsciiPort("/dev/null", {});
port = new AsciiPort(
"/dev/null",
{ startOfSlaveFrameChar: 0x3E } // optional slave frame char ('>')
);
});

after(function() {
Expand Down Expand Up @@ -60,7 +63,7 @@ describe("Modbus Ascii port", function() {
port.open(function() {
port.write(Buffer.from("1103006B00037687", "hex"));
setTimeout(function() {
port._client.receive(Buffer.from(">", "ascii"));
port._client.receive(Buffer.from(">", "ascii")); // changed slave frame char
port._client.receive(Buffer.from("11", "ascii"));
port._client.receive(Buffer.from("03", "ascii"));
port._client.receive(Buffer.from("06", "ascii"));
Expand All @@ -76,7 +79,7 @@ describe("Modbus Ascii port", function() {
});
});

it("should return a valid Modbus RTU exception", function(done) {
it("should return a valid Modbus ASCII exception", function(done) {
port.once("data", function(data) {
expect(data.toString("hex")).to.equal("1183044136");
done();
Expand Down Expand Up @@ -112,7 +115,7 @@ describe("Modbus Ascii port", function() {
});
});

it("Illegal start chars, should synchronize to valid Modbus RTU message", function(done) {
it("Illegal start chars, should synchronize to valid Modbus ASCII message", function(done) {
port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
done();
Expand Down Expand Up @@ -140,7 +143,7 @@ describe("Modbus Ascii port", function() {
});
});

it("Illegal end chars, should return a valid Modbus RTU message", function(done) {
it("Illegal end chars, should return a valid Modbus ASCII message", function(done) {
port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
done();
Expand Down Expand Up @@ -168,7 +171,7 @@ describe("Modbus Ascii port", function() {
});
});

it("should return a valid Modbus RTU message on illegal chars", function(done) {
it("should return a valid Modbus ASCII message on illegal chars", function(done) {
port.once("data", function(data) {
expect(data.toString("hex")).to.equal("110306ae415652434049ad");
done();
Expand All @@ -194,7 +197,7 @@ describe("Modbus Ascii port", function() {
});

describe("#write", function() {
it("should write a valid RTU message to the port", function() {
it("should write a valid Modbus ASCII message to the port", function() {
port.write(Buffer.from("1103006B00037687", "hex"));
expect(port._client._data.toString("ascii")).to.equal(":1103006B00037E\r\n");
});
Expand Down

0 comments on commit 691fcd1

Please sign in to comment.