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

raspi-modbus-halfduplex

v0.0.8

Published

Raspberry Pi native module to do half duplex modbus communication.

Downloads

4

Readme

raspi-modbus-halfduplex

Communicate using half duplex modbus on the Raspberry Pi. The library has been used with a MAX485 TTL to RS485 converter. Please see the "Background and Limitations" section for how this is implemented. Only some modbus functions are implemented, other should be easy to add. Pull requests welcome.

Installation

To build the native code, you need to install the libgpiod-dev package, which is used to switch the GPIO to indicate the half-duplex direction:

sudo apt-get install libgpiod-dev
npm install raspi-modbus-halfduplex

Usage

Connect your RS-485 to TTL converter board:

  • R0 to RXD0 / pin 10 / GPIO 15 of the Raspberry Pi
  • RE to DE pin (both on the RS-485 board)
  • DE to a GPIO pin on the Raspberry Pi. I use pin 7 / GPIO 4, but any pin should work.
  • DI to TXD0 / pin 8 / GPIO 14 of the Rasperry Pi
  • VCC to 3.3 or 5V (pins 1 or 2 of your Raspberry Pi). According to the module specs, you should need 5V, but it works for me on 3.3V. In theory you might risk damage to your Raspberry Pi if you power it with 5V, but I did without damanging my Pi.
  • A and B to the A and B of the modbus device(s) you want to talk to
  • GND to ground of the Raspberry Pi (pins 6, 9, 14, 20, 25, 30, 34 or 39)

Then write your javascript code like:

const modbus = require( 'raspi-modbus-halfduplex' );

modbus.init( 4 )  // 4 is the GPIO number of the pin that switches direction

console.log( modbus.read_analog_register( 1, 0 ) );

API

All functions that take registers use the data address and not the logical address, so for instance 0 rather than 40001.

All functions may throw modbus errors. Error numbers are in the src/modbus.h file.

modbus.init( directionPin )

Initialize the modbus code. Opens the serial port, and requests access to the GPIO pin. Must be called before the other functions

modbus.read_analog_register( device, register )

Read an analog register over modbus. Returns the 16 bit value as an unsigned integer. Device is the modbus address of the device you want to talk to (an 8 bit value).

modbus.write_holding_register( device, register, value ) {

Writes to a holding register. Device is the modbus address of the device you want to talk to (an 8 bit value). Register is the 16 bit register (not logical address, see above). Value is the 16 bit unsigned value to be written.

modbus.write_discrete_coil( device, register, value )

Writes a bit (on/off) to a "discrete coil". Device is the modbus address of the device you want to talk to (an 8 bit value). Register is the 16 bit register (not logical address, see above). Value is the boolean value to be written.

Background and Limitations

Currently this library is hard coded to 9600 bps N81, but that can be changed in the modbus.c file, lines

#define BAUD_RATE 9600
#define BITS_PER_BYTE 10 // start bit, 8 data bits, stop bit

the function set_interface_attribs and

set_interface_attribs (fd, B9600, 0);

Or even better, make it configurable, and submit a pull request.

Communicating in half duplex is tricky, because as soon as the last outgoing bit has been sent, the signal to the RS485 chip to receive instead of transmit must be switched. But there is no way to get the feedback when that bit has been sent. This native code uses a bit of a hack; it sleeps for as long as it thinks it will take to send the bits, then toggles the direction signal. It works for me, but there are no guarantees. I occasionally get a CRC error, but then I retry and it normally works.