Modbus PDU
This is a generic module to create all the modbus PDU message types. You should then use another abstraction to TCP, RTU and ASCII.
You should not use this directly, use stream instead.
Install
npm i modbus-pdu
Examples
var Modbus = require("modbus-pdu");
console.log(Modbus.ReadCoils.Request.build(13, 20));
console.log(Modbus.ReadCoils.Request.parse(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));
console.log(Modbus.ReadFileRecord.Exception.build(Modbus.Exception.ServerDeviceBusy));
console.log(Modbus.Exception.parse(new Buffer([ 0x94, 0x06 ])));
console.log(Modbus.Request(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));
console.log(Modbus.Response(new Buffer([ 0x01, 0x01, 0xad ])));
Function Codes
01
ReadCoils(address
, quantity
)02
ReadDiscreteInputs(address
, quantity
)03
ReadHoldingRegisters(address
, quantity
)04
ReadInputRegisters(address
, quantity
)05
WriteSingleCoil(address
, value
)06
WriteSingleRegister(address
, value
)07
ReadExceptionStatus()0B
GetCommEventCounter()0C
GetCommEventLog()0F
WriteMultipleCoils(address
, values
) // values
should be Array of 1
/0
10
WriteMultipleRegisters(address
, values
) // values
should be Array of 2-size Buffers14
ReadFileRecord(requests
) // requests
should be Array of objects with keys file
, address
and length
15
WriteFileRecord(requests
) // requests
should be Array of objects with keys file
, address
and values
(Array of 2-size Buffers)16
MaskWriteRegister(address
, andmask
, ormask
)17
ReadWriteMultipleRegisters(read_address
, read_quantity
, write_address
, values
) // values
should be Array of 2-size Buffers18
ReadFIFOQueue(address
)2B/0E
ReadDeviceIdentification(code
, id
)
Exceptions
01
IllegalFunction02
IllegalDataAddress03
IllegalDataValue04
ServerDeviceFailure05
Aknowledge06
ServerDeviceBusy08
MemoryParityError0A
GatewayPathUnavailable0B
GatewayTargetDeviceFailedToRespond
You can create an Error
object with code and message using the following code:
var err = Modbus.Exceptions.error("GatewayPathUnavailable");
console.log(err.code);
console.log(err.message);
You can use this error directly when replying to requests using the stream module.