modbus-serial
Advanced tools
Comparing version 2.2.4 to 2.3.4
@@ -55,3 +55,3 @@ 'use strict'; | ||
* | ||
* @param {string} ip the ip of theTCP Port - required. | ||
* @param {string} ip the ip of the TCP Port - required. | ||
* @param {object} options - the serial port options - optional. | ||
@@ -77,4 +77,36 @@ * @param {function} next the function to call next. | ||
} | ||
/** | ||
* Connect to a communication port, using TelnetPort. | ||
* | ||
* @param {string} ip the ip of the TelnetPort - required. | ||
* @param {object} options - the serial port options - optional. | ||
* @param {function} next the function to call next. | ||
*/ | ||
cl.connectTelnet = function (ip, options, next) { | ||
var port; | ||
// check if we have options | ||
if (typeof(next) == 'undefined' && typeof(options) == 'function') { | ||
next = options; | ||
options = {}; | ||
} | ||
// check for port | ||
if (options && options.port) { | ||
port = options.port; | ||
} | ||
// create the TcpPort | ||
var TelnetPort = require('./telnetport'); | ||
var telnetPort = new TelnetPort(ip, port); | ||
// re-set the port to use | ||
this._port = telnetPort; | ||
// open and call next | ||
this.open(next); | ||
} | ||
} | ||
module.exports = addConnctionAPI; |
@@ -7,3 +7,3 @@ // create an empty modbus client | ||
// open connection to a tcp line | ||
client.connectTCP("192.168.1.24"); | ||
client.connectTCP("192.168.1.42"); | ||
@@ -10,0 +10,0 @@ /* read 10 registers every one second |
@@ -1,30 +0,12 @@ | ||
// Create serial port | ||
var SerialPort = require("serialport").SerialPort; | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600}); | ||
// 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(); | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, write); | ||
/* read 2 16bit registers to get one 32bit value: | ||
* 1 - The Slave Address. | ||
* 2 - The Data Address of the first register. | ||
* 2 - Number of registers to read. | ||
*/ | ||
setTimeout(function() { | ||
modbusRTU.writeFC4(1, 5, 2, function(err, data) { | ||
console.log(data.buffer.readUInt32BE()); | ||
}); | ||
}, 500); | ||
/* Write one 32bit value as two 16bit registers: | ||
* 1 - The Slave Address. | ||
* 0 - The Data Address of the first register. | ||
* 32bit timestamp into two 16bit uint registers. | ||
*/ | ||
setTimeout(function() { | ||
function write() { | ||
client.setID(1); | ||
var timestamp = Math.floor(Date.now() / 1000); | ||
@@ -34,8 +16,20 @@ var msb = timestamp >> 16; | ||
modbusRTU.writeFC16(1, 0, [msb, lsb]); | ||
}, 1000); | ||
console.log('Write:', timestamp); | ||
client | ||
.writeRegisters(0, [msb, lsb]) | ||
.then(read); | ||
} | ||
// Close communication. | ||
setTimeout(function() { | ||
serialPort.close(); | ||
}, 2000); | ||
function read() { | ||
// read the 2 registers starting at address 0 | ||
// on device number 1. | ||
client | ||
.readHoldingRegisters(0, 2) | ||
.then( function(data) { | ||
var timestamp = data.buffer.readUInt32BE(); | ||
console.log('Read: ', timestamp); | ||
} ); | ||
} | ||
@@ -1,15 +0,8 @@ | ||
'use strict'; | ||
// Create serial port | ||
var SerialPort = require("serialport").SerialPort; | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600}); | ||
// Create modbus master | ||
// create an empty modbus client | ||
//var ModbusRTU = require("modbus-serial"); | ||
var ModbusRTU = require("../index"); | ||
var client = new ModbusRTU(serialPort); | ||
var client = new ModbusRTU(); | ||
// Open modbus communication. | ||
// and set the client unit id to 1 | ||
client.setID(1); | ||
client.open(stage1); | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, stage1); | ||
@@ -16,0 +9,0 @@ // use callback, if function get a callback it |
@@ -1,36 +0,24 @@ | ||
// Create serial port | ||
var SerialPort = require("serialport").SerialPort; | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600}); | ||
// 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(); | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, write); | ||
/* Write 3 16bit registers: | ||
* 1 - The Slave Address. | ||
* 5 - The Data Address of the first register. | ||
* [0x0800, 0x0000, 0x1800] - The values to write. | ||
*/ | ||
setTimeout(function() { | ||
modbusRTU.writeFC16(1, 5, [0x0800, 0x0000, 0x1800]); | ||
}, 500); | ||
function write() { | ||
client.setID(1); | ||
/* read 2 16bit registers: | ||
* 1 - The Slave Address. | ||
* 5 - The Data Address of the first register. | ||
* 2 - Number of registers to read. | ||
*/ | ||
setTimeout(function() { | ||
modbusRTU.writeFC4(1, 5, 2, function(err, data) { | ||
console.log(data); | ||
}); | ||
}, 1500); | ||
// write the values 0, 0xffff to registers starting at address 5 | ||
// on device number 1. | ||
client.writeRegisters(5, [0 , 0xffff]) | ||
.then(read); | ||
} | ||
// Close communication. | ||
setTimeout(function() { | ||
serialPort.close(); | ||
}, 2000); | ||
function read() { | ||
// read the 2 registers starting at address 5 | ||
// on device number 1. | ||
client.readHoldingRegisters(5, 2) | ||
.then(console.log); | ||
} | ||
11
index.js
@@ -186,3 +186,3 @@ 'use strict'; | ||
*/ | ||
/* check message length | ||
@@ -193,3 +193,4 @@ * if we do not expect this data | ||
if (data.length != length) { | ||
error = "Data length error"; | ||
error = "Data length error, expected " + | ||
length + " got " + data.length; | ||
if (next) | ||
@@ -208,3 +209,4 @@ next(error); | ||
if (address != modbus._nextAddress || code != modbus._nextCode) { | ||
error = "Unexpected data error"; | ||
error = "Unexpected data error, expected " + | ||
modbus._nextAddress + " got " + address; | ||
if (next) | ||
@@ -224,3 +226,3 @@ next(error); | ||
var crcIn = data.readUInt16LE(length - 2); | ||
var crc = _CRC16(data, length - 2) | ||
var crc = _CRC16(data, length - 2); | ||
@@ -440,1 +442,2 @@ if (crcIn != crc) { | ||
module.exports.TcpPort = require('./tcpport'); | ||
module.exports.TelnetPort = require('./telnetport'); |
{ | ||
"name": "modbus-serial", | ||
"version": "2.2.4", | ||
"version": "2.3.4", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,12 +12,15 @@ # modbus-serial | ||
- [modbus-serial](#modbus-serial) | ||
- [Install](#install) | ||
- [What can I do with this module ?](#what-can-i-do-with-this-module-) | ||
- [Compatibility](#compatibility) | ||
- [Examples](#examples) | ||
- [Methods](#methods) | ||
- [API Promises](#api-promises) | ||
- [API](#api) | ||
- [API connection shorthand](#api-connection-shorthand) | ||
---- | ||
- [Install](#install) | ||
- [What can I do with this module ?](#what-can-i-do-with-this-module-) | ||
- [Compatibility](#compatibility) | ||
- [Examples](#examples) | ||
- [Methods](#methods) | ||
- [API Promises](#api-promises) | ||
- [API Callbacks](#api-callbacks) | ||
- [API connection shorthand](#api-connection-shorthand) | ||
---- | ||
#### Install | ||
@@ -29,6 +32,2 @@ | ||
###### Requires | ||
*node-serialport - for using the serial port* | ||
For use over serial port (ModbusRTU), also install node-serialport: | ||
@@ -38,3 +37,2 @@ ``` | ||
``` | ||
[ TCP/IP connection does not require the node-serialport package. ] | ||
@@ -71,2 +69,8 @@ #### What can I do with this module ? | ||
###### Connects types: | ||
* Modbus-RTU: Over serial line [require node serialport]. | ||
* modbus-TCP: Over TCP/IP line. | ||
* modbus-RTU: Over Telnet server, TCP/IP serial bridge. | ||
#### Examples | ||
@@ -185,4 +189,4 @@ ---- | ||
The communication functions have a wrapper function that use | ||
a pre-set unit-id and return a promise. | ||
This communication functions use a pre-set unit-id and can return a promise, | ||
Using callbacks is optional. | ||
@@ -269,4 +273,20 @@ ```javascript | ||
---- | ||
###### API | ||
###### API Callbacks | ||
---- | ||
This communication functions use callbacks. | ||
```javascript | ||
// read 8 holding registers starting at register 10 | ||
// (function use the unit id 1) | ||
client.writeFC3(1, 10, 8, function(err, data) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(data); | ||
}); | ||
``` | ||
---- | ||
##### .open(callback) | ||
@@ -475,1 +495,13 @@ Opens a modbus connection using the given serial port. | ||
---- | ||
##### .connectTelnet (ip, options, callback) | ||
Connect using a telnet server | ||
###### ip | ||
The port ip (e.g. "24.230.1.42") | ||
###### options (optional) | ||
The options for this connection. | ||
###### callback (optional) | ||
Called once the client is connected. |
@@ -53,3 +53,3 @@ 'use strict'; | ||
modbusRTU.writeFC4(2, 8, 1, function(err, data) { | ||
expect(err).to.equal('Data length error'); | ||
expect(err).to.have.string('Data length error'); | ||
@@ -62,3 +62,3 @@ done() | ||
modbusRTU.writeFC4(3, 8, 1, function(err, data) { | ||
expect(err).to.equal('CRC error'); | ||
expect(err).to.have.string('CRC error'); | ||
@@ -71,3 +71,3 @@ done() | ||
modbusRTU.writeFC4(4, 8, 1, function(err, data) { | ||
expect(err).to.equal('Unexpected data error'); | ||
expect(err).to.have.string('Unexpected data error'); | ||
@@ -90,3 +90,3 @@ done() | ||
modbusRTU.writeFC16(2, 8, [42, 128, 5], function(err, data) { | ||
expect(err).to.equal('Data length error'); | ||
expect(err).to.have.string('Data length error'); | ||
@@ -99,3 +99,3 @@ done() | ||
modbusRTU.writeFC16(3, 8, [42, 128, 5], function(err, data) { | ||
expect(err).to.equal('CRC error'); | ||
expect(err).to.have.string('CRC error'); | ||
@@ -108,3 +108,3 @@ done() | ||
modbusRTU.writeFC16(4, 8, [42, 128, 5], function(err, data) { | ||
expect(err).to.equal('Unexpected data error'); | ||
expect(err).to.have.string('Unexpected data error'); | ||
@@ -111,0 +111,0 @@ done() |
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
Mixed license
License(Experimental) Package contains multiple licenses.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
62226
20
1260
501
1