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.
npm i modbus-pdu
var Modbus = require("modbus-pdu");
// <Buffer 01 00 0d 00 14>
console.log(Modbus.ReadCoils.Request.build(13, 20));
// { address: 13, quantity: 20 }
console.log(Modbus.ReadCoils.Request.parse(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));
// <Buffer 94 06>
console.log(Modbus.ReadFileRecord.Exception.build(Modbus.Exception.ServerDeviceBusy));
// { code: 'ReadFileRecord', exception: 'ServerDeviceBusy' }
console.log(Modbus.Exception.parse(new Buffer([ 0x94, 0x06 ])));
// can also auto detect function code
// { code: "ReadCoils", address: 13, quantity: 20 }
console.log(Modbus.Request(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));
// { code: 'ReadCoils', data: [ 1, 0, 1, 1, 0, 1, 0, 1 ] }
console.log(Modbus.Response(new Buffer([ 0x01, 0x01, 0xad ])));
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 of1
/0
10
WriteMultipleRegisters(address
,values
) //values
should be Array of 2-size Buffers14
ReadFileRecord(requests
) //requests
should be Array of objects with keysfile
,address
andlength
15
WriteFileRecord(requests
) //requests
should be Array of objects with keysfile
,address
andvalues
(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
)
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); // 10 (0x0A)
console.log(err.message); // "GatewayPathUnavailable"
You can use this error directly when replying to requests using the stream module.