-
-
Notifications
You must be signed in to change notification settings - Fork 240
Methods
This communication functions use a pre-set unit-id and can return a promise, Using callbacks is optional.
// 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);
});
Sets the unit id
id {number}: The new client id
Returns the unit id
Sets a timeout for the request
duration {number}: Duration of the timeout
Returns the timeout for the request
Returns true if port is open, false o/w.
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.
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.
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.
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.
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.
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.
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.
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.
This communication functions use callbacks.
// 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);
}
});
Opens a modbus connection using the given serial port.
callback {function}: Called when a connection has been opened.
Closes a modbus connection using the given serial port.
callback {function}: Called when a connection has been closed.
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.
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.
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.
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.
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) { ... }
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) { ... }
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) { ... }
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) { ... }
The shorthand connection functions creates a port and open it.
Long way, without shorthand:
// 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:
// 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):
// 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 ...
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.
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.
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.
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.
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.