This is a very simple module that uses NodeJS Streams2 to read Modbus TCP data and convert it to JSON and vice-versa.
npm install modbus-tcp
var modbus = require"modbus-tcp";var client = ;var server = ;// link client and server streams togetherclientwriterpipeserverreader;serverwriterpipeclientreader;// if you have a socket (stream) you can just// call client.pipe(socket) or server.pipe(socket)serveron"read-coils"return replynull 1 0 1 1 ;;// read coils from unit id = 0, from address 10 to 13clientreadCoils0 10 13// coils = [ 1, 0, 1, 1 ];
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.
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
This last event is triggered when an unknown function code is received.