Project

modbus

0.0
No commit activity in last 3 years
No release in over 3 years
Constructs Modbus standard datagrams that make it easy to communicate with devices on Modbus networks
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 12
~> 3.5

Runtime

~> 2.3
~> 0.4
 Project Readme

Ruby Modbus

Constructs modbus standard datagrams that make it easy to communicate with devices on modbus networks. It does not implement the transport layer so you can use it with native ruby, eventmachine, celluloid or the like.

Build Status

You'll need a gateway that supports TCP/IP.

Install the gem

Install it with RubyGems

gem install modbus

or add this to your Gemfile if you use Bundler:

gem 'modbus'

Usage

require 'modbus'

modbus = Modbus.new

# Reading input obtained from the network
# The class instance performs buffering and yields complete ADUs
modbus.read(byte_string) do |adu|
    adu.header.transaction_identifier # => 32

    # Response PDU returned
    if adu.exception?
        # Get error message
        puts adu.value
        # Error code
        puts adu.pdu.exception_code
    else
        case adu.function_name
        when :read_coils
            # raw response data
            puts adu.pdu.get.data.bytes
            # or values
            puts adu.value # => [true, false, true, false, false, false, false, false]
        when :read_input_registers
            # Grab the 16 bit values
            puts adu.value # => [1234, 8822]
        end
    end
end


# You can generate requests like so (writing multipe coils starting from 123)
request = modbus.write_coils(123, true, true, false)
byte_string = request.to_binary_s

# Read 4 coils starting from address 123
request = modbus.read_coils(123, 4)
byte_string = request.to_binary_s

# Send byte_string to the modbus gateway to execute the request

License and copyright

MIT