Skip to content

Commit 48e9d09

Browse files
Kelley ReynoldsKelley Reynolds
authored andcommitted
Added the ability to send RTU commands via TCP Serial Gateway
1 parent a610668 commit 48e9d09

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Timin Aleksey
22
James Sanders
3+
Kelley Reynolds

ChangeLog

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@
5757
2010-02-6 Timin Aleksey <[email protected]>
5858
* lib/rmodbus/client.rb, lib/rmodbus/tcp_client.rb: Client#connection_retries don't use more
5959
2010-07-16 Timin Aleksey <[email protected]>
60-
* lib/rmodbus/rtu_client.rb: Added option read_timeout and accessor RTUClient#read_timeout
60+
* lib/rmodbus/rtu_client.rb: Added option read_timeout and accessor RTUClient#read_timeout
61+
2010-07-28 Kelley Reynolds <[email protected]>
62+
* lib/rmodbus/rtu_via_tcp_client.rb: Added the ability to send RTU commands via TCP Serial Gateway

lib/rmodbus.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
require 'rmodbus/tcp_server'
1414
require 'rmodbus/rtu_client'
1515
require 'rmodbus/rtu_server'
16+
require 'rmodbus/rtu_via_tcp_client'
1617

lib/rmodbus/rtu_via_tcp_client.rb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# RModBus - free implementation of ModBus protocol in Ruby.
2+
#
3+
# Copyright (C) 2009 Timin Aleksey
4+
# Copyright (C) 2010 Kelley Reynolds
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
require 'rmodbus/crc16'
16+
require 'timeout'
17+
require 'rmodbus/client'
18+
require 'rmodbus/exceptions'
19+
20+
module ModBus
21+
22+
class RTUViaTCPClient < Client
23+
24+
include CRC16
25+
attr_reader :ipaddr, :port, :slave
26+
attr_accessor :debug
27+
28+
# Connect with Serial TCP Gateway (eg barionet-50)
29+
#
30+
# ipaddr - ip of the server
31+
#
32+
# port - port TCP connections
33+
#
34+
# slaveaddr - slave ID of the server
35+
#
36+
# RTUViaTCPClient.connect('127.0.0.1') do |cl|
37+
#
38+
# put cl.read_holding_registers(0, 10)
39+
#
40+
# end
41+
def self.connect(ipaddr, port = 10002, slaveaddr = 1)
42+
cl = RTUViaTCPClient.new(ipaddr, port, slaveaddr)
43+
yield cl
44+
cl.close
45+
end
46+
47+
# Connect with a ModBus server
48+
#
49+
# ipaddr - ip of the server
50+
#
51+
# port - port TCP connections
52+
#
53+
# slaveaddr - slave ID of the server
54+
def initialize(ipaddr, port = 10002, slaveaddr = 1)
55+
@ipaddr, @port = ipaddr, port
56+
tried = 0
57+
begin
58+
timeout(1, ModBusTimeout) do
59+
@sock = TCPSocket.new(@ipaddr, @port)
60+
end
61+
rescue ModBusTimeout => err
62+
raise ModBusTimeout.new, 'Timed out attempting to create connection'
63+
end
64+
@slave = slaveaddr
65+
@debug = false
66+
super()
67+
end
68+
69+
# Close TCP connections
70+
def close
71+
@sock.close unless @sock.closed?
72+
end
73+
74+
# Check TCP connections
75+
def closed?
76+
@sock.closed?
77+
end
78+
79+
protected
80+
def send_pdu(pdu)
81+
msg = @slave.chr + pdu
82+
msg << crc16(msg).to_word
83+
@sock.write msg
84+
85+
log "Tx (#{msg.size} bytes): " + logging_bytes(msg) + "\n"
86+
end
87+
88+
def read_pdu
89+
msg = @sock.read(7)
90+
91+
log "Rx (#{msg.size} bytes): " + logging_bytes(msg) + "\n"
92+
93+
if msg.getbyte(0) == @slave
94+
return msg[1..-3] if msg[-2,2].unpack('n')[0] == crc16(msg[0..-3])
95+
log "Ignore package: don't match CRC\n"
96+
else
97+
log "Ignore package: don't match slave ID\n"
98+
end
99+
loop do
100+
#waite timeout
101+
end
102+
end
103+
end
104+
end

rmodbus.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require 'rubygems'
22
spec = Gem::Specification.new do |s|
33
s.name = "rmodbus"
44
s.version = "0.4.0"
5-
s.author = 'A.Timin, J. Sanders'
5+
s.author = 'A.Timin, J. Sanders, K. Reynolds'
66
s.email = "[email protected]"
77
s.homepage = "http://rmodbus.heroku.com"
88
s.rubyforge_project = "RModBus"

0 commit comments

Comments
 (0)