### Install node-modbus-tcp via npm Source: https://github.com/dresende/node-modbus-tcp/blob/master/Readme.md Instructions to install the `modbus-tcp` library using npm. It's advised to consider `modbus-stream` for more complete functionality and broader protocol support (TCP, RTU, ASCII). ```sh npm install modbus-tcp ``` -------------------------------- ### Node.js Modbus TCP Client and Server Example Source: https://github.com/dresende/node-modbus-tcp/blob/master/Readme.md Demonstrates how to set up a Modbus TCP client and server using `node-modbus-tcp`, linking their streams for communication. It shows how to define a server event handler for 'read-coils' requests and how the client can initiate a 'readCoils' request, receiving the response via a callback. ```js var modbus = require("modbus-tcp"); var client = new modbus.Client(); var server = new modbus.Server(); // link client and server streams together client.writer().pipe(server.reader()); server.writer().pipe(client.reader()); // if you have a socket (stream) you can just // call client.pipe(socket) or server.pipe(socket) server.on("read-coils", function (from, to, reply) { return reply(null, [ 1, 0, 1, 1 ]); }); // read coils from unit id = 0, from address 10 to 13 client.readCoils(0, 10, 13, function (err, coils) { // coils = [ 1, 0, 1, 1 ] }); ``` -------------------------------- ### Modbus TCP Client Methods Reference Source: https://github.com/dresende/node-modbus-tcp/blob/master/Readme.md Lists the available methods on the `modbus.Client` object for reading and writing Modbus data. All read methods follow the signature `method(unitId, from, to, next)` and write methods follow `method(unitId, addr, val, next)`, where `next` is an optional callback. Addresses are 0-indexed, meaning 40001 in protocol refers to address 0. ```APIDOC Client Methods: - readCoils(unitId: number, from: number, to: number, next?: Function) - readDiscreteInputs(unitId: number, from: number, to: number, next?: Function) - readHoldingRegisters(unitId: number, from: number, to: number, next?: Function) - readInputRegisters(unitId: number, from: number, to: number, next?: Function) - writeSingleCoil(unitId: number, addr: number, val: number, next?: Function) - writeSingleRegister(unitId: number, addr: number, val: number, next?: Function) - writeMultipleCoils(unitId: number, addr: number, val: number, next?: Function) - writeMultipleRegisters(unitId: number, addr: number, val: number, next?: Function) Parameters: unitId: The Modbus unit identifier. from: The starting address for read operations. to: The ending address for read operations. addr: The address for write operations. val: The value to write. next: An optional callback function (err, result) called upon server reply. ``` -------------------------------- ### Modbus TCP Server Events Reference Source: https://github.com/dresende/node-modbus-tcp/blob/master/Readme.md Lists the events emitted by the `modbus.Server` object when Modbus function codes are received. These events correspond to standard Modbus operations. The `data` event is a special case, triggered when an unknown function code is received by the server. ```APIDOC 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: Triggered when an unknown function code is received. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.