Options
All
  • Public
  • Public/Protected
  • All
Menu

simple-modbus

A simple library for working with Modbus

styled with prettier Greenkeeper badge Travis Coveralls Dev Dependencies Donate All Contributors

The aim of this project is to make it extremely easy to work with Modbus with Node.js. It's written in Typescript, so you get easy to use strong typings. The error handling is consistent and easy, and it has a full test suite to ensure repeatable behavior.

Typedoc documentation

Find it here.

Currently Implemented

  • Modbus TCP Server
  • Modbus RTU Server
  • Modbus ASCII Server
  • Modbus RTU/IP Server
  • Modbus TCP Client
  • Modbus RTU Client
  • Modbus ASCII Client
  • Modbus RTU/IP Client

Function Codes

Examples

Start a ModbusTCP server, and listen for all implemented Modbus commands.

Typescript:

import { ModbusCommandException, ModbusTcp } from 'simple-modbus'

const server = new ModbusTcp.Server()
server.listen(502)

server.onReadCoilStatus.on(command => {
  console.log(`Read Coil Status - Address: ${command.coilStartAddress} Number: ${command.numberOfCoils}`)
  // Respond with Success (response array must be same length as numberOfCoils)
  command.success([true, false, true, false])
})

server.onReadInputStatus.on(command => {
  console.log(`Read Input Status - Address: ${command.inputStartAddress} Number: ${command.numberOfInputs}`)
  // Respond with Success (response array must be same length as numberOfInputs)
  command.success([true, false, true, false])
})

server.onReadHoldingRegisters.on(command => {
  console.log(`Read Holding Registers - Address: ${command.registerStartAddress} Number: ${command.registerLength}`)
  // Respond with Success (response array must be same length as registerLength)
  command.success(new Uint16Array([0xFFFF, 0x0000, 0xDEAD, 0xBEEF]))
})

server.onReadInputRegisters.on(command => {
  console.log(`Read Input Registers - Address: ${command.registerStartAddress} Number: ${command.registerLength}`)
  // Respond with Success (response array must be same length as registerLength)
  command.success(new Uint16Array([0xFFFF, 0x0000, 0xDEAD, 0xBEEF]))
})

server.onPresetSingleRegister.on(command => {
  console.log(`Preset Single Register - Address: ${command.registerAddress} - Value: ${command.registerValue}`)
  // Respond with Success
  command.success()
})

server.onPresetMultipleRegisters.on(command => {
  console.log(`Preset Multiple Registers - Start Address: ${command.registerStartAddress} Length: ${command.registerLength} Values: ${command.registerValues}`)
  // Respond with Success
  command.success()
})

server.onForceSingleCoil.on(command => {
  console.log(`Force Single Coil - Address: ${command.coilAddress} Value: ${command.coilStatusAsCoilStatus}}`)
  // Respond with Success
  command.success()
})

server.onForceMultipleCoils.on(command => {
  console.log(`Force Multiple Coils - Start Address: ${command.coilStartAddress} Length: ${command.coilLength} Values: ${command.coilStatuses}}`)
  // Respond with Failure
  command.fail(ModbusCommandException.ILLEGAL_DATA_ADDRESS)
})

server.onCommandError.on(err => {
  console.error(`Error when trying to decode a packet: ${err.message}`)
})

server.onServerError.on(err => {
  console.error(`Error from underlying TCP server: ${err.message}`)
})

Javascript:

const ModbusTcp = require('simple-modbus').ModbusTcp
const ModbusCommandException = require('simple-modbus').ModbusCommandException

const server = new ModbusTcp.Server();
server.listen(502);

server.onPresetSingleRegister.on(command => {
  console.log(`${command.registerAddress} - ${command.registerValue}`)
  // Respond with Success
  command.success()
  // Or respond with failure
  command.fail(ModbusCommandException.ILLEGAL_DATA_ADDRESS)
})

Options

A server can be initialized with options which will affect its behavior.

const server = new ModbusTcp.Server({ simpleAddressing: false })
  • Modbus Addressing: By default, register and coil addresses will be 0 indexed, not using Modbus addresses. To change this, set simpleAddressing to false

Contributors

I need help! I've tested this with an Automation Direct Click PLC, but I need people to test with additional types of PLCs and submit issues. If you can include a Wireshark PCAP captue, that would be very helpful as well. All contributions are appreciated!

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Index

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc