modbus-serial
Advanced tools
Comparing version 4.5.1 to 4.6.1
@@ -27,2 +27,28 @@ 'use strict'; | ||
var open = function(obj, next) { | ||
/* the function check for a callback | ||
* if we have a callback, use it | ||
* o/w build a promise. | ||
*/ | ||
if (next) { | ||
// if we have a callback, use the callback | ||
obj.open(next); | ||
} else { | ||
// o/w use a promise | ||
var promise = new Promise( function (resolve, reject) { | ||
function cb(err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
} | ||
obj.open(cb); | ||
}); | ||
return promise; | ||
} | ||
} | ||
/** | ||
@@ -49,3 +75,3 @@ * Connect to a communication port, using SerialPort. | ||
// open and call next | ||
this.open(next); | ||
return open(this, next); | ||
}; | ||
@@ -72,3 +98,3 @@ | ||
// open and call next | ||
this.open(next); | ||
return open(this, next); | ||
}; | ||
@@ -95,3 +121,3 @@ | ||
// open and call next | ||
this.open(next); | ||
return open(this, next); | ||
}; | ||
@@ -118,3 +144,3 @@ | ||
// open and call next | ||
this.open(next); | ||
return open(this, next); | ||
}; | ||
@@ -141,3 +167,3 @@ | ||
// open and call next | ||
this.open(next); | ||
return open(this, next); | ||
}; | ||
@@ -164,3 +190,3 @@ | ||
// open and call next | ||
this.open(next); | ||
open(this, next); | ||
} | ||
@@ -167,0 +193,0 @@ }; |
@@ -1,43 +0,33 @@ | ||
// Create serial port | ||
var SerialPort = require("serialport"); | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600, autoOpen: false}); | ||
var lastAns = Date.now(); | ||
// Create modbus master | ||
// create an empty modbus client | ||
//var ModbusRTU = require("modbus-serial"); | ||
var ModbusRTU = require("../index"); | ||
var modbusRTU = new ModbusRTU(serialPort); | ||
var client = new ModbusRTU(); | ||
// Open modbus communication. | ||
modbusRTU.open(start); | ||
// open connection to a serial port | ||
//client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}) | ||
client.connectTCP("10.205.1.42") | ||
.then(setClient) | ||
.then(function() { | ||
console.log("Connected"); }) | ||
.catch(function(e) { | ||
console.log(e.message); }); | ||
/* read 10 registers all the time | ||
* 1 - The Slave Address. | ||
* 0 - The Data Address of the first register. | ||
* 10 - Number of registers to read. | ||
*/ | ||
function start() { | ||
modbusRTU.writeFC4(1, 0, 10, function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data.data); | ||
} | ||
// reset lastAns (for watch dog) and | ||
// read again. | ||
lastAns = Date.now(); | ||
start(); | ||
}); | ||
function setClient() { | ||
// set the client's unit id | ||
// set a timout for requests default is null (no timeout) | ||
client.setID(1); | ||
client.setTimeout(1000); | ||
// run program | ||
run(); | ||
} | ||
/* Watch dog | ||
* if we did not receive an answer in last 5 sec | ||
* restart logger | ||
*/ | ||
setInterval(function() { | ||
if (lastAns < (Date.now() - 5000)) { | ||
lastAns = Date.now(); | ||
start(); | ||
} | ||
}, 1000); | ||
function run() { | ||
// read the 4 registers starting at address 5 | ||
client.readHoldingRegisters(5, 4) | ||
.then(function(d) { | ||
console.log("Recive:", d.data); }) | ||
.catch(function(e) { | ||
console.log(e.message); }) | ||
.then(function() { setTimeout(run, 1000); }); | ||
} |
@@ -7,29 +7,32 @@ // create an empty modbus client | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, write); | ||
//client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}) | ||
client.connectTCP("10.205.1.42") | ||
.then(setClient) | ||
.then(function() { | ||
console.log("Connected"); }) | ||
.catch(function(e) { | ||
console.log(e.message); }); | ||
function write() { | ||
var data = [0 , 0xffff]; | ||
function setClient() { | ||
// set the client's unit id | ||
// set a timout for requests default is null (no timeout) | ||
client.setID(1); | ||
console.log("Send: ", data); | ||
// write the values 0, 0xffff to registers starting at address 5 | ||
// on device number 1. | ||
client.writeRegisters(5, data) | ||
.then(read); | ||
client.setTimeout(1000); | ||
// run program | ||
run(); | ||
} | ||
function read() { | ||
// read the 2 registers starting at address 5 | ||
// on device number 1. | ||
client.readHoldingRegisters(5, 2) | ||
.then(function(d) { | ||
console.log("Recive:", d); }) | ||
.then(exit); | ||
function run() { | ||
// read the 4 registers starting at address 5 | ||
client.readHoldingRegisters(5, 4) | ||
.then(function(d) { | ||
console.log("Recive:", d.data); }) | ||
.catch(function(e) { | ||
console.log(e.message); }) | ||
.then(close); | ||
} | ||
function exit() { | ||
client._port.close(); | ||
process.exit(); | ||
function close() { | ||
client.close(); | ||
} | ||
@@ -19,8 +19,8 @@ declare namespace ModbusRTU { | ||
// Connection shorthand API | ||
connectRTU(path: string, options: SerialPortOptions, next: Function): void; | ||
connectTCP(ip: string, options: TcpPortOptions, next: Function): void; | ||
connectTelnet(ip: string, options: TelnetPortOptions, next: Function): void; | ||
connectC701(ip: string, options: C701PortOptions, next: Function): void; | ||
connectRTUBuffered(path: string, options: SerialPortOptions, next: Function): void; | ||
connectAsciiSerial(path: string, options: SerialPortOptions, next: Function): void; | ||
connectRTU(path: string, options: SerialPortOptions, next: Function): Promise<void>; | ||
connectTCP(ip: string, options: TcpPortOptions, next: Function): Promise<void>; | ||
connectTelnet(ip: string, options: TelnetPortOptions, next: Function): Promise<void>; | ||
connectC701(ip: string, options: C701PortOptions, next: Function): Promise<void>; | ||
connectRTUBuffered(path: string, options: SerialPortOptions, next: Function): Promise<void>; | ||
connectAsciiSerial(path: string, options: SerialPortOptions, next: Function): Promise<void>; | ||
@@ -27,0 +27,0 @@ // Promise API |
57
index.js
@@ -147,3 +147,3 @@ 'use strict'; | ||
if (next) { | ||
next('Timed out'); | ||
next(new Error('Timed out')); | ||
} | ||
@@ -224,3 +224,3 @@ }, duration); | ||
if (transaction.next) | ||
transaction.next(error); | ||
transaction.next(new Error(error)); | ||
return; | ||
@@ -236,3 +236,3 @@ } | ||
if (transaction.next) | ||
transaction.next(error); | ||
transaction.next(new Error(error)); | ||
return; | ||
@@ -251,3 +251,3 @@ } | ||
if (transaction.next) | ||
transaction.next(error); | ||
transaction.next(new Error(error)); | ||
return; | ||
@@ -264,3 +264,3 @@ } | ||
if (transaction.next) | ||
transaction.next(error); | ||
transaction.next(new Error(error)); | ||
return; | ||
@@ -277,3 +277,3 @@ } | ||
if (transaction.next) | ||
transaction.next(error); | ||
transaction.next(new Error(error)); | ||
return; | ||
@@ -331,2 +331,20 @@ } | ||
/** | ||
* Check if port is open | ||
*/ | ||
ModbusRTU.prototype.isOpen = function () { | ||
return this._port && this._port.isOpen(); | ||
}; | ||
/** | ||
* Set the port to emit debug messages into callback | ||
* | ||
* @param {Function} callback the function to call on debug message | ||
*/ | ||
ModbusRTU.prototype.setDebug = function (callback) { | ||
// close the serial port | ||
this._port.debug = true; | ||
this._port.on('debug', callback); | ||
}; | ||
/** | ||
* Write a Modbus "Read Coil Status" (FC=01) to serial port. | ||
@@ -358,3 +376,3 @@ * | ||
var error = "Port Not Open"; | ||
if (next) next(error); | ||
if (next) next(new Error(error)); | ||
return; | ||
@@ -412,4 +430,6 @@ } | ||
if (this._port.isOpen() === false) { | ||
var error = "Port Not Open"; | ||
if (next) next(error); | ||
if (next) { | ||
var message = "Port Not Open"; | ||
next(new Error(message)); | ||
} | ||
return; | ||
@@ -454,4 +474,6 @@ } | ||
if (this._port.isOpen() === false) { | ||
var error = "Port Not Open"; | ||
if (next) next(error); | ||
if (next) { | ||
var message = "Port Not Open"; | ||
next(new Error(message)); | ||
} | ||
return; | ||
@@ -501,4 +523,6 @@ } | ||
if (this._port.isOpen() === false) { | ||
var error = "Port Not Open"; | ||
if (next) next(error); | ||
if (next) { | ||
var message = "Port Not Open"; | ||
next(new Error(message)); | ||
} | ||
return; | ||
@@ -600,4 +624,6 @@ } | ||
if (this._port.isOpen() === false) { | ||
var error = "Port Not Open"; | ||
if (next) next(error); | ||
if (next) { | ||
var message = "Port Not Open"; | ||
next(new Error(message)); | ||
} | ||
return; | ||
@@ -643,4 +669,5 @@ } | ||
module.exports.TestPort = require('./ports/testport'); | ||
module.exports.RTUBufferedPort = require('./ports/rtubufferedport'); | ||
module.exports.TcpPort = require('./ports/tcpport'); | ||
module.exports.TelnetPort = require('./ports/telnetport'); | ||
module.exports.C701Port = require('./ports/c701port'); |
{ | ||
"name": "modbus-serial", | ||
"version": "4.5.1", | ||
"version": "4.6.1", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS.", | ||
@@ -28,7 +28,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"chai": "^3.4.0", | ||
"mocha": "^2.3.3", | ||
"mockery": "^1.4.0", | ||
"sinon": "^1.17.4" | ||
"chai": "^3.5.0", | ||
"mocha": "^3.2.0", | ||
"mockery": "^2.0.0", | ||
"sinon": "^1.17.6", | ||
"serialport": "^4.0.6" | ||
} | ||
} |
@@ -26,2 +26,3 @@ 'use strict'; | ||
this._length = 0; | ||
this.debug = false; | ||
@@ -73,3 +74,9 @@ // create the SerialPort | ||
RTUBufferedPort.prototype._emitData = function(start, length) { | ||
this.emit('data', this._buffer.slice(start, start + length)); | ||
var buffer = this._buffer.slice(start, start + length); | ||
// emit debug message | ||
if (this.debug) { this.emit('debug', {action: 'recive', data: buffer}); } | ||
// emit data | ||
this.emit('data', buffer); | ||
this._buffer = this._buffer.slice(start + length); | ||
@@ -139,4 +146,7 @@ }; | ||
this._client.write(data); | ||
// emit debug message | ||
if (this.debug) { this.emit('debug', {action: 'send', data: data}); } | ||
}; | ||
module.exports = RTUBufferedPort; |
@@ -20,2 +20,3 @@ 'use strict'; | ||
this.callback = null; | ||
this.debug = false; | ||
@@ -53,2 +54,5 @@ // options | ||
// emit debug message | ||
if (modbus.debug) { modbus.emit('debug', {action: 'recive', data: buffer}); } | ||
// emit a data signal | ||
@@ -116,4 +120,7 @@ modbus.emit('data', buffer); | ||
this._client.write(buffer); | ||
// emit debug message | ||
if (this.debug) { this.emit('debug', {action: 'send', data: buffer}); } | ||
}; | ||
module.exports = TcpPort; |
@@ -1,5 +0,6 @@ | ||
# modbus-serial | ||
A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS | ||
# modbus-serial master | ||
A pure JavaScript implemetation of MODBUS-RTU (and TCP) master for NodeJS | ||
[![npm](https://img.shields.io/npm/v/npm.svg)](https://www.npmjs.com/package/modbus-serial) | ||
[![Build Status](https://travis-ci.org/yaacov/node-modbus-serial.svg?branch=master)](https://travis-ci.org/yaacov/node-modbus-serial) | ||
@@ -234,2 +235,15 @@ This class makes ModbusRTU (and TCP) calls fun and easy. | ||
---- | ||
##### .isOpen() | ||
Returns true if port is open, false o/w. | ||
---- | ||
##### .setDebug(callback) | ||
Set the port to emit debug messages into callback | ||
*callback {function}:* | ||
Called if port emit a 'debug' message (e.g. on send and recive data) | ||
that looks like: function (data) { ... } | ||
---- | ||
##### .readCoils (address, length) | ||
@@ -236,0 +250,0 @@ Writes "Read Coils" (FC=1) request to serial port. |
@@ -69,3 +69,3 @@ 'use strict'; | ||
modbusRTU.writeFC3(2, 8, 3, function (err, data) { | ||
expect(err).to.have.string('Data length error'); | ||
expect(err.message).to.have.string('Data length error'); | ||
@@ -78,3 +78,3 @@ done(); | ||
modbusRTU.writeFC3(3, 8, 3, function (err, data) { | ||
expect(err).to.have.string('CRC error'); | ||
expect(err.message).to.have.string('CRC error'); | ||
@@ -87,3 +87,3 @@ done(); | ||
modbusRTU.writeFC3(4, 8, 3, function (err, data) { | ||
expect(err).to.have.string('Unexpected data error'); | ||
expect(err.message).to.have.string('Unexpected data error'); | ||
@@ -96,3 +96,3 @@ done(); | ||
modbusRTU.writeFC3(5, 8, 3, function (err, data) { | ||
expect(err).to.have.string('Modbus exception'); | ||
expect(err.message).to.have.string('Modbus exception'); | ||
@@ -117,3 +117,3 @@ done(); | ||
modbusRTU.writeFC4(2, 8, 1, function (err, data) { | ||
expect(err).to.have.string('Data length error'); | ||
expect(err.message).to.have.string('Data length error'); | ||
@@ -126,3 +126,3 @@ done(); | ||
modbusRTU.writeFC4(3, 8, 1, function (err, data) { | ||
expect(err).to.have.string('CRC error'); | ||
expect(err.message).to.have.string('CRC error'); | ||
@@ -135,3 +135,3 @@ done(); | ||
modbusRTU.writeFC4(4, 8, 1, function (err, data) { | ||
expect(err).to.have.string('Unexpected data error'); | ||
expect(err.message).to.have.string('Unexpected data error'); | ||
@@ -144,3 +144,3 @@ done(); | ||
modbusRTU.writeFC4(5, 8, 3, function (err, data) { | ||
expect(err).to.have.string('Modbus exception'); | ||
expect(err.message).to.have.string('Modbus exception'); | ||
@@ -165,3 +165,3 @@ done(); | ||
modbusRTU.writeFC6(2, 1, 42, function (err, data) { | ||
expect(err).to.have.string('Data length error'); | ||
expect(err.message).to.have.string('Data length error'); | ||
@@ -174,3 +174,3 @@ done(); | ||
modbusRTU.writeFC6(3, 1, 42, function (err, data) { | ||
expect(err).to.have.string('CRC error'); | ||
expect(err.message).to.have.string('CRC error'); | ||
@@ -183,3 +183,3 @@ done(); | ||
modbusRTU.writeFC6(4, 1, 42, function (err, data) { | ||
expect(err).to.have.string('Unexpected data error'); | ||
expect(err.message).to.have.string('Unexpected data error'); | ||
@@ -192,3 +192,3 @@ done(); | ||
modbusRTU.writeFC6(5, 1, 42, function (err, data) { | ||
expect(err).to.have.string('Modbus exception'); | ||
expect(err.message).to.have.string('Modbus exception'); | ||
@@ -211,3 +211,3 @@ done(); | ||
modbusRTU.writeFC15(2, 8, [true, false, true], function (err, data) { | ||
expect(err).to.have.string('Data length error'); | ||
expect(err.message).to.have.string('Data length error'); | ||
@@ -220,3 +220,3 @@ done(); | ||
modbusRTU.writeFC15(3, 8, [true, false, true], function (err, data) { | ||
expect(err).to.have.string('CRC error'); | ||
expect(err.message).to.have.string('CRC error'); | ||
@@ -229,3 +229,3 @@ done(); | ||
modbusRTU.writeFC15(4, 8, [true, false, true], function (err, data) { | ||
expect(err).to.have.string('Unexpected data error'); | ||
expect(err.message).to.have.string('Unexpected data error'); | ||
@@ -238,3 +238,3 @@ done(); | ||
modbusRTU.writeFC15(5, 8, [true, false, true], function (err, data) { | ||
expect(err).to.have.string('Modbus exception'); | ||
expect(err.message).to.have.string('Modbus exception'); | ||
@@ -271,3 +271,3 @@ done(); | ||
modbusRTU.writeFC16(2, 8, [42, 128, 5], function (err, data) { | ||
expect(err).to.have.string('Data length error'); | ||
expect(err.message).to.have.string('Data length error'); | ||
@@ -280,3 +280,3 @@ done(); | ||
modbusRTU.writeFC16(3, 8, [42, 128, 5], function (err, data) { | ||
expect(err).to.have.string('CRC error'); | ||
expect(err.message).to.have.string('CRC error'); | ||
@@ -289,3 +289,3 @@ done(); | ||
modbusRTU.writeFC16(4, 8, [42, 128, 5], function (err, data) { | ||
expect(err).to.have.string('Unexpected data error'); | ||
expect(err.message).to.have.string('Unexpected data error'); | ||
@@ -298,3 +298,3 @@ done(); | ||
modbusRTU.writeFC16(5, 8, [42, 128, 5], function (err, data) { | ||
expect(err).to.have.string('Modbus exception'); | ||
expect(err.message).to.have.string('Modbus exception'); | ||
@@ -370,3 +370,3 @@ done(); | ||
modbusRTU.writeFC3(6, 8, 3, function (err, data) { | ||
expect(err).to.have.string('Timed out'); | ||
expect(err.message).to.have.string('Timed out'); | ||
done(); | ||
@@ -387,3 +387,3 @@ }); | ||
.catch(function (err) { | ||
expect(err).to.have.string('Timed out'); | ||
expect(err.message).to.have.string('Timed out'); | ||
done(); | ||
@@ -390,0 +390,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
631
120897
5
34
2906
1