diff --git a/README.md b/README.md index cb2d42b..475e39e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ports/asciiport.js b/ports/asciiport.js index 99e94fc..2da4815 100644 --- a/ports/asciiport.js +++ b/ports/asciiport.js @@ -1,4 +1,6 @@ "use strict"; +/* eslint-disable no-ternary */ + var util = require("util"); var events = require("events"); var EventEmitter = events.EventEmitter || events; @@ -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; @@ -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(""); diff --git a/test/ports/asciiport.test.js b/test/ports/asciiport.test.js index eb11dd3..b51ee7e 100644 --- a/test/ports/asciiport.test.js +++ b/test/ports/asciiport.test.js @@ -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() { @@ -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")); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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"); });