Modbus/RTU via TCP Serial Gateway with Ruby

We recently had a need to connect to a PowerLogic BCM42 through a Barionet-50 from Barix. While the Barionet-50 speaks Modbus/TCP for itself, it does not perform as a TCP/RTU gateway for devices hooked up to it via the RS485 port which is a straight serial gateway. Our project is being implemented in ruby so we started with the excellent RModbus gem and modified it to send Modbus/RTU commands via TCP and it works perfectly. This solution is also significantly cheaper than buying an off-the-shelf Modbus TCP/RTU gateway as those tend to *start* at about $400 and rapidly get more expensive.

 

require 'rubygems'
require 'rmodbus'
ModBus::RTUViaTCPClient.connect('10.10.100.33', '10002', 4) do |cl|
        total_ma = cl.read_holding_registers(0, 42).inject(0) { |acc, i| acc += i; acc}
        puts "Total mA: #{total_ma}"
end
$ ./test.rb
Total mA: 12510

Our code for accessing Modbus/RTU devices via TCP can be found at http://github.com/kreynolds/RModBus

Bookmark and Share

About Kelley Reynolds

A full-stack software engineer, an avid trail runner, and a bassoonist. Kelley occasionally writes about one of his many projects on this blog.

, ,

  • wayer

    Looks very handy.On ruby 1.9.2p136 (2010-12-25 revision 30365) on OS X I’m getting the following error:ruby test.rb/opt/local/lib/ruby1.9/gems/1.9.1/gems/rmodbus-1.0.2/lib/rmodbus/tcp.rb:34:in `[]‘: can’t convert Symbol into Integer (TypeError)Anyone else seeing that?