rmodbus / rmodbus

RModBus - free implementation of ModBus protocol in pure Ruby
BSD 3-Clause "New" or "Revised" License
112 stars 46 forks source link

RTUClient Timeout error #1

Closed atimin closed 13 years ago

atimin commented 14 years ago

I've started using today your rmodbus library, I've found it very interesting.

I am trying to use it to read and write to the serial port of one of our plc, but I'm in trouble as the library returns always timeout, while instead the plc replied correctly.

This is the error returned by your software:

$ ruby serial_modbux.rb /home/morpheusz/.gem/ruby/1.8/gems/rmodbus-0.4.0/lib/rmodbus/client.rb:142:in query': Timed out during read attempt (ModBus::Errors::ModBusTimeout) from /home/morpheusz/.gem/ruby/1.8/gems/rmodbus-0.4.0/lib/rmodbus/client.rb:55:in read_holding_registers' from serial_modbux.rb:5

This is the traffic in the serial line, as you can see it is correct (the software is requesting the holding registers from 1 to 5):

TX: 01 03 00 01 00 05 D4 09 RX: 01 03 0A 00 00 00 00 00 00 00 00 00 00 24 B6

This is the source code of my simple application:

require 'rmodbus' cl = ModBus::RTUClient.new('/dev/ttyUSB0', 19200, 1, :data_bits => 8, :stop_bits => 2, :parity => SerialPort::NONE ) puts cl.read_holding_registers(1,5) cl.close

I'm using Gentoo Linux with a USB/RS485 converter with the following gems:

$ gem list --local

* LOCAL GEMS *

rmodbus (0.4.0) serialport (1.0.1)

Alberto Zennaro

elif commented 14 years ago

I'm having the exact same error over rs232... I've confirmed that the modbus server (Trendpoint Enersure) is communicating fine via serial. I've tried with both a windows and linux client, and it times out on every request.

markf commented 13 years ago

I can also confirm there is an issue with the RTUClient piece of this library. I've forked and I'm in the process of troubleshooting. Will update as I can.

markf commented 13 years ago

I am finding that the issue with the RTUClient "timing out" is related to the method read_pdu in rtu_client.rb. Specifically, it calls for @sp.read and expects to receive everything at once. I have played with the read_timeout settings for the SerialPort instance but cannot "force" this to work as-is. Instead, I have more luck specifying the number of bytes to read in this method. Something like:

def read_pdu(numbytes)
  msg = @sp.read(numbytes)
  ...
end

seems to work well. Changes are needed in client.rb to determine from nreg how many bytes to expect.

I'm not sure if this is a consistent issue or an issue with the serialport library under certain versions of Ruby. I have not tried anything >= 1.9.

If I have time I will go back and look but for now I don't have a need for this method and am focusing on the functionality I need for my current project.

atimin commented 13 years ago

I think is not good idea.. because slave RTU may return exception code. Please, try it:

https://github.com/flipback/RModBus/blob/release-0.4.0/lib/rmodbus/rtu_client.rb#L96 @sp.read_timeout = 100

For more information - https://github.com/hparra/ruby-serialport/issues#issue/20 I will have fixed this bug in next release later.

markf commented 13 years ago

Yes, as I mentioned above, I've tried a number of values for the read_timeout. Here is strace read,write output with @sp.read_timeout set to 100.

write(3, "\2\3\4\261\0\2\225/", 8)      = 8
write(1, "Tx (8 bytes): [02][03][04][b1][0"..., 47Tx (8 bytes): [02][03][04][b1][00][02][95][2f]
) = 47
read(3, "\2\3", 4096)                   = 2
read(3, "\4\0\0004\25\36<", 4096)       = 7
^C

As you can see, there's a successful read of 9 bytes, as expected. However, the application hangs indefinitely. This occurs with any setting of read_timeout, even as high as 5000.

atimin commented 13 years ago

I did as You say. Now method #read_pdu for rtu clients use reading per bytes. See Client#read_rtu_response. It is working with simulator modbus slave and 0 modem cable. If you have possibility for testing with real device, please do it. Thks.

markf commented 13 years ago

Aleksey,

Looks good. I tested with a number of registers on my device and had no timeout issues. Thanks for the attention. Any specific tests you'd like me to perform, let me know.. but this looks good.

Mark

atimin commented 13 years ago

Thanks, Mark. I will release new version 0.5.0 with this fix soon.