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

modbusclient

v1.0.9

Published

A wrapper for TCP sockets built for the Modbus protocol.

Downloads

4

Readme

ModbusClient

A wrapper for TCP sockets built for the Modbus protocol.

Installation

To install the package simply run the following command

npm install modbusclient

Usage

The package provides a central utility ModbusClient class which implements various methods for handling communications with a Modbus server.

Creating the client

import { ModbusClient } from 'modbusclient'

const client = new ModbusClient(502, '127.0.0.1', () => console.log('Connected'))

As seen above, the constructor takes 3 arguments: the port where the Modbus Server is listening on, the IP address of the machine on which the server runs on and a callback function which is executed once a connection has been established.

Sending a request

To send a request, you need to use the send method defined on the ModbusClient class, which takes as its only argument a ModbusRequest object.

The ModbusRequest object contains the following properties:

  • functionCode - the Modbus function code to be requested;
  • noOfRegisters - the total number of register values to be returned;
  • slaveId - the slave from which to request data;
  • startAddress - the register from which noOfRegisters register values will be returned.
let request = {
  // Read Input Registers Function
  functionCode: 4,
  // Requesting 3 register values
  noOfRegisters: 3,
  // The Modbus Slave from which data is requested
  slaveId: 0,
  // The first register to read values from
  startAddress: 10
}

// Sending the request
client.send(request)

The Response object

The response object contains the following properties:

  • transactionId - the order number of the current response;
  • functionCode - the function code which was previously requested;
  • noOfRegisters - the total number of registers returned;
  • registerValues - array of integers which contain the requested register values.

Receiving the response

The client implements a Promise based API, therefore the send method returns a Promise which resolves once a response is received from the server. As such, the response is passed as an argument to the Promises' then method. If the request fails, the Promise rejects with an appropriate error message.

client
  .send(request)
  // Processing the response
  .then(response => log(response))
  // Handling possible errors
  .catch(err => console.log(err))

function log(response) {
  console.log(`Transaction ID: ${response.transactionId}`)
  console.log(`Function code: ${response.functionCode}`)
  console.log(`Total number of registers: ${response.noOfRegisters}`)
  console.log(`Values: ${response.registerValues.join(', ')}`)
}

If the above snippet encounters no errors, the displayed result will be:

Transaction ID: 1
Function code: 4
Total number of registers: 3
Values: 2, 3, 10

ES6 Async / Await

You can also use the ES6 async / await syntactic sugar to make the code look synchronous. The following example should produce the same result as the previous one:

function log(response) {
  console.log(`Transaction ID: ${response.transactionId}`)
  console.log(`Function code: ${response.functionCode}`)
  console.log(`Total number of registers: ${response.noOfRegisters}`)
  console.log(`Values: ${response.registerValues.join(', ')}`)
}

async function example() {
  try {
    const response = await client.send(request)
    log(response)
  } catch (error) {
    console.log(`Error: ${error}`)
  }
}

example()

TypeScript

Additional type annotations for the ModbusRequest and ModbusResponse objects can be imported with:

import { ModbusRequest, ModbusResponse } from 'modbusclient/types'
import { ModbusClient } from 'modbusclient'

const client: ModbusClient = new ModbusClient(502, '127.0.0.1', () => {})
const request: ModbusRequest = {
  functionCode: 4,
  noOfRegisters: 3,
  slaveId: 0,
  startAddress: 10
}
client.send(request).then((response: ModbusResponse) => console.log(response))