Skip to content

Commit ffc8fcd

Browse files
author
Aleksey Timin
committed
Add a test of the IO class in # clean_input_buffer. Issue rmodbus#35.
The test is needed to avoid calling #flush_input for Socket class in RTUviaTCPClient.
1 parent 58d02ec commit ffc8fcd

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ html/
77
doc/
88
Gemfile.lock
99
.rvmrc
10+
tags

lib/rmodbus/rtu.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def read_rtu_response(io)
4646

4747
def clean_input_buff
4848
# empty the input buffer
49-
@io.flush_input
49+
if @io.kind_of? SerialPort
50+
@io.flush_input
51+
else
52+
@io.flush
53+
end
5054
end
5155

5256
def send_rtu_pdu(pdu)

spec/logging_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ def mock_query(request, response)
6060

6161
@sp.should_receive(:flow_control=).with(SerialPort::NONE)
6262
@sp.stub(:read_timeout=)
63-
63+
@sp.stub(:kind_of?).with(SerialPort).and_return(true)
64+
@sp.stub(:flush_input)
65+
6466
@slave = ModBus::RTUClient.new("/dev/port1", 9600, :data_bits => 7, :stop_bits => 2, :parity => SerialPort::ODD).with_slave(1)
6567
@slave.read_retries = 0
68+
6669
end
6770

6871
it 'should log rec\send bytes' do

spec/rtu_client_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
SerialPort.should_receive(:new).with("/dev/port1", 9600, 8, 1, 0).and_return(@sp)
88
@sp.stub(:read_timeout=)
99
@sp.should_receive(:flow_control=).with(SerialPort::NONE)
10+
@sp.stub(:kind_of?).with(SerialPort).and_return(true)
1011
@sp.stub(:flush_input)
1112

1213
@cl = ModBus::RTUClient.new("/dev/port1", 9600, :data_bits => 8, :stop_bits => 1, :parity => SerialPort::NONE)

spec/rtu_via_tcp_client_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
describe ModBus::RTUViaTCPClient do
55
describe "method 'query'" do
66
before do
7-
@sock = double('Socked')
7+
@sock = double('Socket')
88
TCPSocket.should_receive(:new).with("127.0.0.1", 10002).and_return(@sock)
99
@sock.stub(:read_timeout=)
10-
@sock.stub(:flush_input)
10+
@sock.stub(:flush)
1111

1212
@cl = ModBus::RTUViaTCPClient.new("127.0.0.1")
1313
@slave = @cl.with_slave(1)

0 commit comments

Comments
 (0)