Skip to content

Commit

Permalink
Modified the clearing buffer to @pwood variant.
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Jun 27, 2012
1 parent d660fd3 commit 40ef48c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
55 changes: 55 additions & 0 deletions examples/perfomance_rtu_via_tcp.rb
@@ -0,0 +1,55 @@
$:.unshift File.join(File.dirname(__FILE__),'../lib')

require 'rmodbus'
require 'benchmark'

include ModBus

TIMES = 1000

srv = ModBus::RTUViaTCPServer.new 1502
srv.coils = [0,1] * 50
srv.discrete_inputs = [1,0] * 50
srv.holding_registers = [0,1,2,3,4,5,6,7,8,9] * 10
srv.input_registers = [0,1,2,3,4,5,6,7,8,9] * 10
srv.start


cl = RTUViaTCPClient.new('127.0.0.1', 1502)
cl.with_slave(1) do |slave|
Benchmark.bmbm do |x|
x.report('Read coils') do
TIMES.times { slave.read_coils 0, 100 }
end

x.report('Read discrete inputs') do
TIMES.times { slave.read_discrete_inputs 0, 100 }
end

x.report('Read holding registers') do
TIMES.times { slave.read_holding_registers 0, 100 }
end

x.report('Read input registers') do
TIMES.times { slave.read_input_registers 0, 100 }
end

x.report('Write single coil') do
TIMES.times { slave.write_single_coil 0, 1 }
end

x.report('Write single register') do
TIMES.times { slave.write_single_register 100, 0xAAAA }
end

x.report('Write multiple coils') do
TIMES.times { slave.write_multiple_coils 0, [1,0] * 50 }
end

x.report('Write multiple registers') do
TIMES.times { slave.write_multiple_registers 0, [0,1,2,3,4,5,6,7,8,9] * 10 }
end
end
end
cl.close
srv.stop
15 changes: 8 additions & 7 deletions lib/rmodbus/rtu.rb
Expand Up @@ -23,7 +23,6 @@ def read_rtu_response(io)
msg = nil
while msg.nil?
msg = io.read(2)
sleep(0.01)
end

function_code = msg.getbyte(1)
Expand All @@ -46,11 +45,14 @@ def read_rtu_response(io)
end

def clean_input_buff
rt = @io.read_timeout
@io.read_timeout = -1
@io.read # Clean input buffer

@io.read_timeout = rt
begin
# Read up to 1500 bytes of trash.
@io.read_nonblock(1500)
rescue Errno::EAGAIN
# Ignore the fact we couldn't read.
rescue Exception => e
raise e
end
end

def send_rtu_pdu(pdu)
Expand Down Expand Up @@ -119,7 +121,6 @@ def serv_rtu_requests(io, &blk)
log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
io.write resp
end
sleep(0.01)
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/rmodbus/tcp_server.rb
Expand Up @@ -57,7 +57,6 @@ def serve(io)
log "Server TX (#{resp.size} bytes): #{logging_bytes(resp)}"
io.write resp
end
sleep(0.01)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions rmodbus.gemspec
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'pry'
gem.add_development_dependency 'serialport' unless RUBY_PLATFORM == 'java'
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'ruby-prof'
end
4 changes: 2 additions & 2 deletions spec/logging_spec.rb
Expand Up @@ -42,8 +42,8 @@ def mock_query(request, response)
@sp = mock('Serial port')
SerialPort.should_receive(:new).with("/dev/port1", 9600, 7, 2, SerialPort::ODD).and_return(@sp)

@sp.stub!(:read_timeout=)
@sp.stub!(:read_timeout)
@sp.stub!(:read_timeout=)

@slave = ModBus::RTUClient.new("/dev/port1", 9600, :data_bits => 7, :stop_bits => 2, :parity => SerialPort::ODD).with_slave(1)
@slave.read_retries = 0
Expand All @@ -52,7 +52,7 @@ def mock_query(request, response)
it 'should log rec\send bytes' do
request = "\x3\x0\x1\x0\x1"
@sp.should_receive(:write).with("\1#{request}\xd5\xca")
@sp.should_receive(:read).and_return("\xff\xff") # Clean a garbage
@sp.should_receive(:read_nonblock).and_return("\xff\xff") # Clean a garbage
@sp.should_receive(:read).with(2).and_return("\x1\x3")
@sp.should_receive(:read).with(1).and_return("\x2")
@sp.should_receive(:read).with(4).and_return("\xff\xff\xb9\xf4")
Expand Down
2 changes: 1 addition & 1 deletion spec/rtu_client_spec.rb
Expand Up @@ -6,7 +6,7 @@
SerialPort.should_receive(:new).with("/dev/port1", 9600, 8, 1, 0).and_return(@sp)
@sp.stub!(:read_timeout=)
@sp.stub!(:read_timeout)
@sp.stub!(:read)
@sp.stub!(:read_nonblock)

@cl = ModBus::RTUClient.new("/dev/port1", 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::NONE)
@slave = @cl.with_slave(1)
Expand Down
3 changes: 1 addition & 2 deletions spec/rtu_via_tcp_client_spec.rb
Expand Up @@ -6,8 +6,7 @@
@sock = mock('Socked')
TCPSocket.should_receive(:new).with("127.0.0.1", 10002).and_return(@sock)
@sock.stub!(:read_timeout=)
@sock.stub!(:read_timeout)
@sock.stub!(:read)
@sock.stub!(:read_nonblock)

@cl = ModBus::RTUViaTCPClient.new("127.0.0.1")
@slave = @cl.with_slave(1)
Expand Down

0 comments on commit 40ef48c

Please sign in to comment.