modbus-serial
Advanced tools
Comparing version 5.1.3 to 5.2.2
163
index.js
@@ -22,3 +22,5 @@ 'use strict'; | ||
var crc16 = require('./utils/crc16'); | ||
var modbusSerialDebug = require('debug')('modbus-serial'); | ||
var PORT_NOT_OPEN_MESSAGE = 'Port Not Open'; | ||
/** | ||
@@ -128,3 +130,3 @@ * @fileoverview ModbusRTU module, exports the ModbusRTU class. | ||
function _writeBufferToPort(buffer) { | ||
var transaction = this._transactions[this._transactionId] | ||
var transaction = this._transactions[this._transactionId]; | ||
@@ -147,3 +149,3 @@ this._port.write(buffer); | ||
} | ||
return setTimeout(function () { | ||
return setTimeout(function() { | ||
if (next) { | ||
@@ -161,3 +163,3 @@ next(new Error('Timed out')); | ||
function _cancelTimeout(timeoutHandle) { | ||
clearTimeout(timeoutHandle); | ||
clearTimeout(timeoutHandle); | ||
} | ||
@@ -170,3 +172,3 @@ | ||
*/ | ||
var ModbusRTU = function (port) { | ||
var ModbusRTU = function(port) { | ||
// the serial port to use | ||
@@ -190,17 +192,14 @@ this._port = port; | ||
*/ | ||
ModbusRTU.prototype.open = function (callback) { | ||
ModbusRTU.prototype.open = function(callback) { | ||
var modbus = this; | ||
// open the serial port | ||
modbus._port.open(function (error) { | ||
modbus._port.open(function(error) { | ||
if (error) { | ||
/* On serial port open error | ||
* call next function | ||
*/ | ||
modbusSerialDebug({action: 'port open error', error: error}); | ||
/* On serial port open error call next function */ | ||
if (callback) | ||
callback(error); | ||
} else { | ||
/* On serial port open OK | ||
* call next function | ||
*/ | ||
/* On serial port open OK call next function */ | ||
if (callback) | ||
@@ -212,3 +211,3 @@ callback(error); | ||
*/ | ||
modbus._port.on('data', function (data) { | ||
modbus._port.on('data', function(data) { | ||
// set locale helpers variables | ||
@@ -325,7 +324,6 @@ var transaction = modbus._transactions[modbus._transactionId]; | ||
*/ | ||
ModbusRTU.prototype.close = function (callback) { | ||
ModbusRTU.prototype.close = function(callback) { | ||
// close the serial port | ||
this._port.close(callback); | ||
this._port.removeAllListeners('data'); | ||
this._port.removeAllListeners('debug'); | ||
}; | ||
@@ -336,3 +334,3 @@ | ||
*/ | ||
ModbusRTU.prototype.isOpen = function () { | ||
ModbusRTU.prototype.isOpen = function() { | ||
return this._port && this._port.isOpen(); | ||
@@ -342,13 +340,2 @@ }; | ||
/** | ||
* 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. | ||
@@ -361,3 +348,3 @@ * | ||
*/ | ||
ModbusRTU.prototype.writeFC1 = function (address, dataAddress, length, next) { | ||
ModbusRTU.prototype.writeFC1 = function(address, dataAddress, length, next) { | ||
this.writeFC2(address, dataAddress, length, next, 1); | ||
@@ -374,18 +361,18 @@ }; | ||
*/ | ||
ModbusRTU.prototype.writeFC2 = function (address, dataAddress, length, next, code) { | ||
// function code defaults to 2 | ||
code = code || 2; | ||
ModbusRTU.prototype.writeFC2 = function(address, dataAddress, length, next, code) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
// function code defaults to 2 | ||
code = code || 2; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 3 + parseInt((length - 1) / 8 + 1) + 2, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 3 + parseInt((length - 1) / 8 + 1) + 2, | ||
next: next | ||
}; | ||
@@ -405,3 +392,3 @@ | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -417,3 +404,3 @@ | ||
*/ | ||
ModbusRTU.prototype.writeFC3 = function (address, dataAddress, length, next) { | ||
ModbusRTU.prototype.writeFC3 = function(address, dataAddress, length, next) { | ||
this.writeFC4(address, dataAddress, length, next, 3); | ||
@@ -430,18 +417,18 @@ }; | ||
*/ | ||
ModbusRTU.prototype.writeFC4 = function (address, dataAddress, length, next, code) { | ||
// function code defaults to 4 | ||
code = code || 4; | ||
ModbusRTU.prototype.writeFC4 = function(address, dataAddress, length, next, code) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
// function code defaults to 4 | ||
code = code || 4; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 3 + 2 * length + 2, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 3 + 2 * length + 2, | ||
next: next | ||
}; | ||
@@ -461,3 +448,3 @@ | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -473,17 +460,17 @@ | ||
*/ | ||
ModbusRTU.prototype.writeFC5 = function (address, dataAddress, state, next) { | ||
var code = 5; | ||
ModbusRTU.prototype.writeFC5 = function(address, dataAddress, state, next) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
var code = 5; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
}; | ||
@@ -508,3 +495,3 @@ | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -520,17 +507,17 @@ | ||
*/ | ||
ModbusRTU.prototype.writeFC6 = function (address, dataAddress, value, next) { | ||
var code = 6; | ||
ModbusRTU.prototype.writeFC6 = function(address, dataAddress, value, next) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
var code = 6; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
}; | ||
@@ -551,3 +538,3 @@ | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -563,17 +550,18 @@ | ||
*/ | ||
ModbusRTU.prototype.writeFC15 = function (address, dataAddress, array, next) { | ||
var code = 15; | ||
ModbusRTU.prototype.writeFC15 = function(address, dataAddress, array, next) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
var code = 15; | ||
var i = 0; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
}; | ||
@@ -592,7 +580,7 @@ | ||
// clear the data bytes before writing bits data | ||
for (var i = 0; i < dataBytes; i++) { | ||
for (i = 0; i < dataBytes; i++) { | ||
buf.writeUInt8(0, 7 + i); | ||
} | ||
for (var i = 0; i < array.length; i++) { | ||
for (i = 0; i < array.length; i++) { | ||
// buffer bits are already all zero (0) | ||
@@ -609,3 +597,3 @@ // only set the ones set to one (1) | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -621,17 +609,17 @@ | ||
*/ | ||
ModbusRTU.prototype.writeFC16 = function (address, dataAddress, array, next) { | ||
var code = 16; | ||
ModbusRTU.prototype.writeFC16 = function(address, dataAddress, array, next) { | ||
// check port is actually open before attempting write | ||
if (this._port.isOpen() === false) { | ||
if (next) next(new Error("Port Not Open")); | ||
if (next) next(new Error(PORT_NOT_OPEN_MESSAGE)); | ||
return; | ||
} | ||
var code = 16; | ||
// set state variables | ||
this._transactions[this._transactionId] = { | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
nextAddress: address, | ||
nextCode: code, | ||
nextLength: 8, | ||
next: next | ||
}; | ||
@@ -656,3 +644,3 @@ | ||
// write buffer to serial port | ||
_writeBufferToPort.call(this, buf) | ||
_writeBufferToPort.call(this, buf); | ||
}; | ||
@@ -673,2 +661,3 @@ | ||
module.exports.TcpPort = require('./ports/tcpport'); | ||
module.exports.TcpRTUBufferedPort = require('./ports/tcprtubufferedport'); | ||
module.exports.TelnetPort = require('./ports/telnetport'); | ||
@@ -675,0 +664,0 @@ module.exports.C701Port = require('./ports/c701port'); |
{ | ||
"name": "modbus-serial", | ||
"version": "5.1.3", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS.", | ||
"version": "5.2.2", | ||
"description": "A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.", | ||
"main": "index.js", | ||
@@ -29,7 +29,23 @@ "scripts": { | ||
"chai": "^3.5.0", | ||
"eslint": "^3.16.1", | ||
"gulp": "^3.9.1", | ||
"gulp-clean": "^0.3.2", | ||
"gulp-jsdoc3": "^1.0.1", | ||
"mocha": "^3.2.0", | ||
"mocha-eslint": "^3.0.1", | ||
"mockery": "^2.0.0", | ||
"sinon": "^1.17.7", | ||
"serialport": "^4.0.7" | ||
} | ||
"pump": "^1.0.2", | ||
"serialport": "4.0.7", | ||
"sinon": "^1.17.7" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.6.1" | ||
}, | ||
"directories": { | ||
"example": "examples", | ||
"test": "test" | ||
}, | ||
"files": [ | ||
"modbus-serial" | ||
] | ||
} |
562
README.md
@@ -1,8 +0,9 @@ | ||
# modbus-serial master | ||
A pure JavaScript implemetation of MODBUS-RTU (and TCP) master for NodeJS | ||
# modbus-serial | ||
[![npm](https://img.shields.io/npm/v/npm.svg)](https://www.npmjs.com/package/modbus-serial) | ||
A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS. | ||
[![NPM download](https://img.shields.io/npm/dm/modbus-serial.svg)](http://www.npm-stats.com/~packages/modbus-serial) | ||
[![NPM version](https://badge.fury.io/js/modbus-serial.png)](http://badge.fury.io/js/modbus-serial) | ||
[![Build Status](https://travis-ci.org/yaacov/node-modbus-serial.svg?branch=master)](https://travis-ci.org/yaacov/node-modbus-serial) | ||
This class makes ModbusRTU (and TCP) calls fun and easy. | ||
@@ -13,12 +14,10 @@ Modbus is a serial communications protocol, first used in 1979. | ||
**This package makes Modbus calls and serve fun and easy.** | ||
---- | ||
- [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) | ||
- [Methods](https://github.com/yaacov/node-modbus-serial/wiki/Methods) | ||
@@ -29,11 +28,13 @@ ---- | ||
``` | ||
npm install modbus-serial | ||
``` | ||
npm install modbus-serial | ||
try these options on npm install to build, if you have problems to install | ||
--unsafe-perm --build-from-source | ||
For use over serial port (ModbusRTU), also install node-serialport: | ||
``` | ||
npm install serialport | ||
``` | ||
npm install serialport@4.0.7 | ||
#### What can I do with this module ? | ||
@@ -60,3 +61,3 @@ | ||
###### This class implements: | ||
###### This classes are implemented: | ||
@@ -72,34 +73,22 @@ * FC1 "Read Coil Status" | ||
###### Connects types: | ||
###### Client Serial: | ||
* modbus-RTU (modbus-rtu): Over serial line [require node serialport]. | ||
* modbus-ASCII (modbus-ascii): Over serial line [require node serialport]. | ||
* modbus-TCP (modbus-tcp): Over TCP/IP line. | ||
* modbus-RTU (telnet): Over Telnet server, TCP/IP serial bridge. | ||
* modbus-RTU (buffered): Over buffered serial line [require node serialport]. | ||
* modbus-RTU (C701): Over C701 server, commercial UDP to serial bridge. | ||
* modbus-RTU (SerialPort): Over serial line [require node serialport]. | ||
* modbus-RTU (RTUBufferedPort): Over buffered serial line [require node serialport]. | ||
* modbus-ASCII (AsciiPort): Over serial line [require node serialport]. | ||
#### Examples | ||
---- | ||
###### Logger | ||
``` javascript | ||
// create an empty modbus client | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(); | ||
###### Client TCP: | ||
// open connection to a tcp line | ||
client.connectTCP("192.168.1.42", run); | ||
* modbus-TCP (TcpPort): Over TCP/IP line. | ||
* modbus-RTU (UdpPort): Over C701 server, commercial UDP to serial bridge. | ||
* modbus-RTU (TcpRTUBufferedPort): Over TCP/IP line, TCP/IP serial RTU buffered device. | ||
* modbus-RTU (TelnetPort): Over Telnet server, TCP/IP serial bridge. | ||
// read the values of 10 registers starting at address 0 | ||
// on device number 1. and log the values to the console. | ||
function run() { | ||
client.setID(1); | ||
###### Server | ||
client.readInputRegisters(0, 10) | ||
.then(console.log) | ||
.then(run); | ||
} | ||
``` | ||
* modbus-TCP (ServerTCP): Over TCP/IP line. | ||
---- | ||
#### Examples | ||
###### Read and Write | ||
@@ -131,3 +120,3 @@ ``` javascript | ||
---- | ||
###### Logger | ||
###### Logger Serial | ||
``` javascript | ||
@@ -139,3 +128,3 @@ // create an empty modbus client | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}); | ||
client.connectRTUBuffered("/dev/ttyUSB0", {baudrate: 9600}); | ||
client.setID(1); | ||
@@ -152,3 +141,3 @@ | ||
---- | ||
###### Logger-TCP | ||
###### Logger TCP | ||
``` javascript | ||
@@ -160,3 +149,3 @@ // create an empty modbus client | ||
// open connection to a tcp line | ||
client.connectTCP("192.168.1.42"); | ||
client.connectTCP("127.0.0.1", {port: 8502}); | ||
client.setID(1); | ||
@@ -173,22 +162,2 @@ | ||
---- | ||
###### Read raw buffer | ||
``` javascript | ||
// create an empty modbus client | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(); | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, run); | ||
function run() { | ||
client.setID(1); | ||
// read 2 16bit-registers to get one 32bit number | ||
client.readInputRegisters(5, 2, function(err, data) { | ||
var int32 = data.buffer.readUInt32BE(); | ||
console.log(int32); | ||
}); | ||
} | ||
``` | ||
---- | ||
###### ModbusTCP Server | ||
@@ -199,459 +168,14 @@ ``` javascript | ||
var vector = { | ||
getInputRegister: function(addr) { return addr; }, | ||
getHoldingRegister: function(addr) { return addr + 8000; }, | ||
getCoil: function(addr) { return (addr % 2) === 0; }, | ||
setRegister: function(addr, value) { console.log('set register', addr, value); return; }, | ||
setCoil: function(addr, value) { console.log('set coil', addr, value); return; } | ||
getInputRegister: function(addr, unitID) { return addr; }, | ||
getHoldingRegister: function(addr, unitID) { return addr + 8000; }, | ||
getCoil: function(addr, unitID) { return (addr % 2) === 0; }, | ||
setRegister: function(addr, value, unitID) { console.log('set register', addr, value, unitID); return; }, | ||
setCoil: function(addr, value, unitID) { console.log('set coil', addr, value, unitID); return; } | ||
}; | ||
// set the server to answer for modbus requests | ||
console.log('ModbusTCP listening on modbus://0.0.0.0:502'); | ||
var serverTCP = new ModbusRTU.ServerTCP(vector, {host: '0.0.0.0'}); | ||
console.log('ModbusTCP listening on modbus://0.0.0.0:8502'); | ||
var serverTCP = new ModbusRTU.ServerTCP(vector, {host: '0.0.0.0', port: 8502, debug: true, unitID: 1}); | ||
``` | ||
#### Methods | ||
---- | ||
###### API promises | ||
---- | ||
This communication functions use a pre-set unit-id and can return a promise, | ||
Using callbacks is optional. | ||
```javascript | ||
// set the client's unit id | ||
client.setID(1); | ||
// set a timout for requests default is null (no timeout) | ||
client.setTimeout(1000); | ||
// read 8 discrete inputs starting at input 10 | ||
// (function use the unit id 1, we set earlier) | ||
client.readDiscreteInputs(10, 8) | ||
.then(function(data) { | ||
console.log(data); | ||
}); | ||
``` | ||
---- | ||
##### .setID(id) | ||
Sets the unit id | ||
*id {number}:* | ||
The new client id | ||
---- | ||
##### .getID() | ||
Returns the unit id | ||
---- | ||
##### .setTimeout(duration) | ||
Sets a timeout for the request | ||
*duration {number}:* | ||
Duration of the timeout | ||
---- | ||
##### .getTimeout() | ||
Returns the timeout for the request | ||
---- | ||
##### .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) | ||
Writes "Read Coils" (FC=1) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
---- | ||
##### .readDiscreteInputs (address, length) | ||
Writes "Read Discrete Inputs" (FC=2) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
---- | ||
##### .readHoldingRegisters (address, length) | ||
Writes "Read Holding Registers" (FC=3) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
---- | ||
##### .readInputRegisters (address, length) | ||
Writes "Read Input Registers" (FC=4) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
---- | ||
##### .writeCoil(address, state) | ||
Writes "Force Coil Status" (FC=5) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*state {boolean}:* | ||
The state to force into coil. | ||
---- | ||
##### .writeCoils(address, array) | ||
Writes "Force Multiple Coils" (FC=15) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*array {array}:* | ||
The array of states to force into the coils. | ||
---- | ||
##### .writeRegisters (address, array) | ||
Writes "Preset Multiple Registers" (FC=16) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*array {array}:* | ||
The array of values to set into the registers. | ||
---- | ||
---- | ||
##### .writeRegister (address, value) | ||
Writes "Preset Single Register" (FC=6) request to serial port. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*value {number}:* | ||
The value to set into the register. | ||
---- | ||
###### 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) | ||
Opens a modbus connection using the given serial port. | ||
*callback {function}:* (optional) | ||
Called when a connection has been opened. | ||
---- | ||
##### .close(callback) | ||
Closes a modbus connection using the given serial port. | ||
*callback {function}:* (optional) | ||
Called when a connection has been closed. | ||
---- | ||
##### .writeFC1 (unit, address, length, callback) | ||
Writes "Read coil status" (FC=01) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of boolean coils (in multiples of 8 = one byte). | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
---- | ||
##### .writeFC2 (unit, address, length, callback) | ||
Writes "Read input status" (FC=02) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of boolean digital inputs (in multiples of 8 = one byte). | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
---- | ||
##### .writeFC3 (unit, address, length, callback) | ||
Writes "Read Holding Registers" (FC=03) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of unsinged 16 bit registers. | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
---- | ||
##### .writeFC4 (unit, address, length, callback) | ||
Writes "Read Input Registers" (FC=04) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*length {number}:* | ||
The total number of registers requested. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
``` | ||
error - null on success, error string o/w | ||
data - an object with two fildes: | ||
data.data: array of unsinged 16 bit registers. | ||
data.buffer: raw baffer of bytes returned by slave. | ||
``` | ||
---- | ||
##### .writeFC5 (unit, address, state, callback) | ||
Writes "Force Single Coil" (FC=05) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*state {boolean}:* | ||
The coil state. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
---- | ||
##### .writeFC15 (unit, address, array, callback) | ||
Writes "Force Multiple Coils" (FC=15) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*array {array}:* | ||
The array of states to send to unit. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
---- | ||
##### .writeFC6 (unit, address, value, callback) | ||
Writes "Preset Single Register" (FC=6) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*value {number}:* | ||
The value to sent to unit. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
---- | ||
##### .writeFC16 (unit, address, array, callback) | ||
Writes "Preset Multiple Registers" (FC=16) request to serial port. | ||
*unit {number}:* | ||
The slave unit address. | ||
*address {number}:* | ||
The Data Address of the first register. | ||
*array {array}:* | ||
The array of values to sent to unit. | ||
*callback {function}:* (optional) | ||
Called once the unit returns an answer. The callback should be a function | ||
that looks like: function (error, data) { ... } | ||
---- | ||
###### API connection shorthand | ||
---- | ||
The shorthand connection functions creates a port and open it. | ||
Long way, without shorthand: | ||
``` javascript | ||
// open a serial port | ||
var SerialPort = require("serialport"); | ||
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600, autoOpen: false}); | ||
// create a modbus client using the serial port | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(serialPort); | ||
// open connection to a serial port | ||
client.open(); | ||
// tell your coffee machine to do something ... | ||
``` | ||
Using shorthand: | ||
``` javascript | ||
// create an empty modbus client | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(); | ||
// open connection to a serial port | ||
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}); | ||
// tell your robot to do something ... | ||
``` | ||
Using shorthand (TCP): | ||
``` javascript | ||
// create an empty modbus client | ||
var ModbusRTU = require("modbus-serial"); | ||
var client = new ModbusRTU(); | ||
// open connection to a tcp line | ||
client.connectTCP("192.168.1.42"); | ||
// tell your robot to do something ... | ||
``` | ||
---- | ||
##### .connectRTU (path, options, callback) | ||
Connect using serial port. | ||
*path {string}:* | ||
The port path (e.g. "/dev/ttyS0") | ||
*options {object}:* (optional) | ||
The options for this connection. | ||
*callback {function}:* (optional) | ||
Called once the client is connected. | ||
---- | ||
##### .connectRTUBuffered (path, options, callback) | ||
Connect using buffered serial port. | ||
Use when serial port has long delays inside packets. | ||
*path {string}:* | ||
The port path (e.g. "/dev/ttyS0") | ||
*options {object}:* (optional) | ||
The options for this connection. | ||
*callback {function}:* (optional) | ||
Called once the client is connected. | ||
---- | ||
##### .connectTCP (ip, options, callback) | ||
Connect using tcp/ip. | ||
*ip {string}:* | ||
The port ip (e.g. "24.230.1.42") | ||
*options {object}:* (optional) | ||
The options for this connection. | ||
*callback {function}:* (optional) | ||
Called once the client is connected. | ||
---- | ||
##### .connectTelnet (ip, options, callback) | ||
Connect using a telnet server | ||
*ip {string}:* | ||
The port ip (e.g. "24.230.1.42") | ||
*options {object}:* (optional) | ||
The options for this connection. | ||
*callback {function}:* (optional) | ||
Called once the client is connected. | ||
---- | ||
##### .connectAsciiSerial (path, options, callback) | ||
Connect using serial port with ASCII encoding. | ||
*path {string}:* | ||
The port path (e.g. "/dev/ttyS0") | ||
*options {object}:* (optional) | ||
The options for this connection. | ||
*callback {function}:* (optional) | ||
Called once the client is connected. | ||
to get more see [Examples](https://github.com/yaacov/node-modbus-serial/wiki) |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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 2 instances in 1 package
1
26750
1
11
4
549
172
+ Addeddebug@^2.6.1
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)