项目作者: node-modbus

项目描述 :
NodeJS Modbus流
高级语言: JavaScript
项目地址: git://github.com/node-modbus/stream.git
创建时间: 2017-03-29T16:36:07Z
项目社区:https://github.com/node-modbus/stream

开源协议:MIT License

下载


Modbus Stream

Build Status
Package Version

This is a NodeJS module to help you process modbus data. It uses pdu to build the core PDU and then uses transports to extend the rest.

Features

  • Support almost every standard function code
  • Support standard exceptions
  • Support transports
    • TCP
    • RTU
    • ASCII
  • Support drivers
    • TCP
    • UDP
    • Serial (RS232, RS485)

Example

This is my current test.js file. It creates a client and a server network socket and the server requests coils as soon as the client connects.

  1. var modbus = require("modbus-stream");
  2. modbus.tcp.server({ debug: "server" }, (connection) => {
  3. connection.readCoils({ address: 5, quantity: 8 }, (err, info) => {
  4. console.log("response", info.response.data);
  5. });
  6. }).listen(12345, () => {
  7. modbus.tcp.connect(12345, { debug: "client" }, (err, connection) => {
  8. connection.on("read-coils", (request, reply) => {
  9. reply(null, [ 1, 0, 1, 0, 1, 1, 0, 1 ]);
  10. });
  11. });
  12. });

Usage

Connection

To connect to a modbus device over TCP, use:

  1. var modbus = require("modbus-stream");
  2. modbus.tcp.connect(502, "134.2.56.231", { debug: "automaton-2454" }, (err, connection) => {
  3. // do something with connection
  4. });

To listen for connections over TCP, use:

  1. var modbus = require("modbus-stream");
  2. modbus.tcp.server({ debug: "server" }, (connection) => {
  3. // do something with connection
  4. }).listen(502, () => {
  5. // ready
  6. });

To connecto to a modbus device over a serial port, use:

  1. var modbus = require("modbus-stream");
  2. modbus.serial.connect("/dev/ttyS123", { debug: "automaton-123" }, (err, connection) => {
  3. // do something with connection
  4. });

Requests

After having a connection, you can send requests and listen for responses.

  1. modbus.serial.connect("/dev/ttyS123", { debug: "automaton-123" }, (err, connection) => {
  2. if (err) throw err;
  3. connection.readCoils({ address: 52, quantity: 8, extra: { unitId: 25 } }, (err, res) => {
  4. if (err) throw err;
  5. console.log(res); // response
  6. })
  7. });

Every method accepts an object options which have defaults parameters (like address = 0) and a callback, in case you want to see the response from the remote device. Here is a list of supported function codes and the corresponding methods:

Base Reads

  • readCoils (address = 0, quantity = 1)
  • readDiscreteInputs (address = 0, quantity = 1)
  • readHoldingRegisters (address = 0, quantity = 1)
  • readInputRegisters (address = 0, quantity = 1)

Base Writes

  • writeSingleCoil (address = 0, value = 0)
  • writeSingleRegister (address = 0, value = <Buffer 0x00 0x00>)
  • writeMultipleCoils (address = 0, values = [])
  • writeMultipleRegisters (address = 0, values = [ <Buffer 0x00 0x00> ])

File Records

  • readFileRecord (requests = [])
  • writeFileRecord (requests = [])

FIFO

  • readFifoQueue (address = 0)

Advanced

  • maskWriteRegister (address = 0, andmask = 0xFFFF, ormask = 0x0000)
  • readWriteMultipleRegisters (read_address = 0, read_quantity = 1, write_address = 0, values = [ <Buffer 0x00 0x00> ])
  • readDeviceIdentification (type = "BasicDeviceIdentification", id = "ProductName")
  • readExceptionStatus ()
  • getCommEventCounter ()
  • getCommEventLog ()

For more information on these methods, look at the pdu repository which is used
to build the packets.

Responses

To respond to remote requests, listen for events.

  1. modbus.serial.connect("/dev/ttyS123", {
  2. // except "debug", everything else is the default for serial
  3. baudRate : 9600,
  4. dataBits : 8,
  5. stopBits : 1,
  6. parity : "none",
  7. debug : "automaton-123"
  8. }, (err, connection) => {
  9. if (err) throw err;
  10. connection.events.on("read-coils", (req, reply) => {
  11. console.log(req); // request
  12. // ...
  13. return reply(null, [ data ]);
  14. })
  15. });

Events

There are events propagated from the transports up to the stream. You should bind some event listener
just in case the connection or serial device errors or just closes. Remember that in NodeJS, an emitted
error event without a listener will cause the process to throw an uncaughtException.

Transport Closed (close)

This event is emitted when the serialport module emits a close event or when a socket emits an
end event.

Transport Error (error)

This event if something happens to the underlying stream, like a ECONNRESET or something similar.