npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modbus-pdu

v1.14.0

Published

Modbus base PDU

Downloads

6,380

Readme

Modbus PDU

Build Status

This is a generic module to create all the modbus PDU message types. You should then use another abstraction to TCP, RTU and ASCII.

You should not use this directly, use stream instead.

Install

npm i modbus-pdu

Examples

var Modbus = require("modbus-pdu");

// <Buffer 01 00 0d 00 14>
console.log(Modbus.ReadCoils.Request.build(13, 20));
// { address: 13, quantity: 20 }
console.log(Modbus.ReadCoils.Request.parse(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));

// <Buffer 94 06>
console.log(Modbus.ReadFileRecord.Exception.build(Modbus.Exception.ServerDeviceBusy));
// { code: 'ReadFileRecord', exception: 'ServerDeviceBusy' }
console.log(Modbus.Exception.parse(new Buffer([ 0x94, 0x06 ])));

// can also auto detect function code
// { code: "ReadCoils", address: 13, quantity: 20 }
console.log(Modbus.Request(new Buffer([ 0x01, 0x00, 0x0d, 0x00, 0x14 ])));
// { code: 'ReadCoils', data: [ 1, 0, 1, 1, 0, 1, 0, 1 ] }
console.log(Modbus.Response(new Buffer([ 0x01, 0x01, 0xad ])));

Function Codes

  • 01 ReadCoils(address, quantity)
  • 02 ReadDiscreteInputs(address, quantity)
  • 03 ReadHoldingRegisters(address, quantity)
  • 04 ReadInputRegisters(address, quantity)
  • 05 WriteSingleCoil(address, value)
  • 06 WriteSingleRegister(address, value)
  • 07 ReadExceptionStatus()
  • 0B GetCommEventCounter()
  • 0C GetCommEventLog()
  • 0F WriteMultipleCoils(address, values) // values should be Array of 1/0
  • 10 WriteMultipleRegisters(address, values) // values should be Array of 2-size Buffers
  • 14 ReadFileRecord(requests) // requests should be Array of objects with keys file, address and length
  • 15 WriteFileRecord(requests) // requests should be Array of objects with keys file, address and values (Array of 2-size Buffers)
  • 16 MaskWriteRegister(address, andmask, ormask)
  • 17 ReadWriteMultipleRegisters(read_address, read_quantity, write_address, values) // values should be Array of 2-size Buffers
  • 18 ReadFIFOQueue(address)
  • 2B/0E ReadDeviceIdentification(code, id)

Exceptions

  • 01 IllegalFunction
  • 02 IllegalDataAddress
  • 03 IllegalDataValue
  • 04 ServerDeviceFailure
  • 05 Aknowledge
  • 06 ServerDeviceBusy
  • 08 MemoryParityError
  • 0A GatewayPathUnavailable
  • 0B GatewayTargetDeviceFailedToRespond

You can create an Error object with code and message using the following code:

var err = Modbus.Exceptions.error("GatewayPathUnavailable");
console.log(err.code); // 10 (0x0A)
console.log(err.message); // "GatewayPathUnavailable"

You can use this error directly when replying to requests using the stream module.