Modbus TCP/IP Stream
This is a very simple module that uses NodeJS Streams2 to read Modbus TCP data and convert it to JSON and vice-versa.
Install
If you're just starting or just trying to use this library, I advise you to try modbus-stream. It's more complete (it supports all standard function codes) and it was designed to work with TCP, RTU and ASCII modes.
npm install modbus-tcp
Example
var modbus = ;var client = ;var server = ; // link client and server streams togetherclient;server;// if you have a socket (stream) you can just// call client.pipe(socket) or server.pipe(socket) server; // read coils from unit id = 0, from address 10 to 13client;
Client Methods
All of the following read methods have the form method(unitId, from, to, next)
and write methods have the form method(unitId, addr, val, next)
, where next
is an optional function called if the server replies (with the same transactionId) to the sent message.
- readCoils
- readDiscreteInputs
- readHoldingRegisters
- readInputRegisters
- writeSingleCoil
- writeSingleRegister
- writeMultipleCoils
- writeMultipleRegisters
Addresses are exactly as is in protocol, so if you see a paper talking about address 40001
this usually means first record address of that function so it means address 0
.
Server Events
- read-coils
- read-discrete-inputs
- read-holding-registers
- read-input-registers
- write-single-coil
- write-single-register
- write-multiple-coils
- write-multiple-registers
- data
This last event is triggered when an unknown function code is received.