Permalink
Browse files

Version 1.2.0

  • Loading branch information...
2 parents 0a9e5f5 + d3429e4 commit b50db6c2d030fb821cb04f25f159f932808a0e5a @flipback flipback committed Mar 12, 2013
Showing with 50 additions and 21 deletions.
  1. +4 −2 NEWS.md
  2. +1 −1 README.md
  3. +14 −10 lib/rmodbus/tcp_slave.rb
  4. +1 −1 lib/rmodbus/version.rb
  5. +16 −1 spec/logging_spec.rb
  6. +14 −6 spec/tcp_client_spec.rb
View
@@ -1,6 +1,8 @@
-###Next Release
+###2013-03-12 Release 1.2.0
-1. Added ruby-2.0 compatibility
+1. Transaction number mismatch doesn't throw exception in TCPSlave#query method.
+Now this method will wait correct transaction until timeout breaks waiting.
+2. Added ruby-2.0 experemental compatibility
###2012-07-17 Release 1.1.5
View
@@ -5,7 +5,7 @@ RModBus [![Build Status](https://secure.travis-ci.org/flipback/rmodbus.png)](htt
Features
---------------------------
- - Ruby 1.8.7, Ruby 1.9, Ruby 2.0, JRuby (without serial ModBus RTU)
+ - Ruby 1.8.7, Ruby 1.9, Ruby 2.0 (experemntal), JRuby (without serial ModBus RTU)
- TCP, RTU, RTU over TCP protocols
- Client(master) and server(slave)
- 16, 32 -bit and float registers
View
@@ -47,17 +47,21 @@ def send_pdu(pdu)
# overide method for RTU over TCP implamentaion
# @see Slave#query
def read_pdu
- header = @io.read(7)
- if header
- tin = header[0,2].unpack('n')[0]
- raise Errors::ModBusException.new("Transaction number mismatch") unless tin == @transaction
- len = header[4,2].unpack('n')[0]
- msg = @io.read(len-1)
+ loop do
+ header = @io.read(7)
+ if header
+ trn = header[0,2].unpack('n')[0]
+ len = header[4,2].unpack('n')[0]
+ msg = @io.read(len-1)
- log "Rx (#{(header + msg).size} bytes): " + logging_bytes(header + msg)
- msg
- else
- raise Errors::ModBusException.new("Server did not respond")
+ log "Rx (#{(header + msg).size} bytes): " + logging_bytes(header + msg)
+
+ if trn == @transaction
+ return msg
+ else
+ log "Transaction number mismatch. A packet is ignored."
+ end
+ end
end
end
end
View
@@ -13,5 +13,5 @@
# GNU General Public License for more details.
module ModBus
# Package version
- VERSION = '1.1.5'
+ VERSION = '1.2.0'
end
View
@@ -11,23 +11,38 @@
@sock.stub!(:read).with(0).and_return('')
@slave = ModBus::TCPClient.new('127.0.0.1', 1502).with_slave(@uid)
+ @slave.debug = true
end
it 'should log rec\send bytes' do
request, response = "\x3\x0\x6b\x0\x3", "\x3\x6\x2\x2b\x0\x0\x0\x64"
mock_query(request,response)
- @slave.debug = true
$stdout.should_receive(:puts).with("Tx (12 bytes): [00][01][00][00][00][06][01][03][00][6b][00][03]")
$stdout.should_receive(:puts).with("Rx (15 bytes): [00][01][00][00][00][09][01][03][06][02][2b][00][00][00][64]")
@slave.query(request)
end
it "should don't logging if debug disable" do
+ @slave.debug = false
request, response = "\x3\x0\x6b\x0\x3", "\x3\x6\x2\x2b\x0\x0\x0\x64"
mock_query(request,response)
@slave.query(request)
end
+ it "should log warn message if transaction mismatch" do
+ @adu[0,2] = @slave.transaction.next.to_word
+ @sock.should_receive(:write).with(@adu)
+ @sock.should_receive(:read).with(7).and_return("\000\002\000\000\000\001" + @uid.chr)
+ @sock.should_receive(:read).with(7).and_return("\000\001\000\000\000\001" + @uid.chr)
+
+ $stdout.should_receive(:puts).with("Tx (7 bytes): [00][01][00][00][00][01][01]")
+ $stdout.should_receive(:puts).with("Rx (7 bytes): [00][02][00][00][00][01][01]")
+ $stdout.should_receive(:puts).with("Transaction number mismatch. A packet is ignored.")
+ $stdout.should_receive(:puts).with("Rx (7 bytes): [00][01][00][00][00][01][01]")
+
+ @slave.query('')
+ end
+
def mock_query(request, response)
@adu = @slave.transaction.next.to_word + "\x0\x0\x0\x9" + @uid.chr + request
@sock.should_receive(:write).with(@adu[0,4] + "\0\6" + @uid.chr + request)
View
@@ -21,17 +21,25 @@
@slave.query('').should == nil
end
- it 'should throw exception if get other transaction' do
+ it 'should not throw exception and white next packet if get other transaction' do
@adu[0,2] = @slave.transaction.next.to_word
@sock.should_receive(:write).with(@adu)
@sock.should_receive(:read).with(7).and_return("\000\002\000\000\000\001" + @uid.chr)
- begin
- @slave.query('').should == nil
- rescue Exception => ex
- ex.class.should == ModBus::Errors::ModBusException
- end
+ @sock.should_receive(:read).with(7).and_return("\000\001\000\000\000\001" + @uid.chr)
+
+ expect{ @slave.query('') }.to_not raise_error
end
+ it 'should throw timeout exception if do not get own transaction' do
+ @slave.read_retries = 2
+ @adu[0,2] = @slave.transaction.next.to_word
+ @sock.should_receive(:write).any_number_of_times.with(/\.*/)
+ @sock.should_receive(:read).any_number_of_times.with(7).and_return("\000\x3\000\000\000\001" + @uid.chr)
+
+ expect{ @slave.query('') }.to raise_error(ModBus::Errors::ModBusTimeout, "Timed out during read attempt")
+ end
+
+
it 'should return only data from PDU' do
request = "\x3\x0\x6b\x0\x3"
response = "\x3\x6\x2\x2b\x0\x0\x0\x64"

0 comments on commit b50db6c

Please sign in to comment.