RTU over TCP

866 views
Skip to first unread message

Ivica

unread,
Dec 12, 2012, 7:12:45 AM12/12/12
to pymo...@googlegroups.com
Hello everyone,

first of all, let me thank the author of this excellent module - it's great!

Secondly, I'm having some troubles making RTU over TCP packet. There is one example on groups here, but original poster found another solution. I was following that topic, but am stuck currently, with this code (I am python beginner)

from pymodbus import factory, transaction
from pymodbus.client import async

decoder = factory.ClientDecoder()
framer = transaction.ModbusRtuFramer(decoder)
client_protocol = async.ModbusClientProtocol(framer)

How can I send some read_holding_registers request with this? I have successfully read holding registers from a simulated device with 127.0.0.1 address using ModbusTcpClient. This real device in front of me is giving me troubles :)

Thank you in advance.

Ivica

unread,
Dec 12, 2012, 7:30:42 AM12/12/12
to pymo...@googlegroups.com
Ideally, i would like to use sync client module, with support for RTU frames, as Albert Brandl suggested:

 If you prefer to use the sync client, you
could create a subclass of ModbusTcpClient and override its __init__
method. Try to replace
        BaseModbusClient.__init__(self, ModbusSocketFramer(ClientDecoder()))
with
        BaseModbusClient.__init__(self, ModbusRtuFramer(ClientDecoder()))

Bashwork

unread,
Dec 16, 2012, 1:31:42 PM12/16/12
to pymo...@googlegroups.com
Since this is something that has been requested a few times now, I have added the ability to inject the framer of your choice into the synchronous clients (this ability is already available in the asynchronous versions)::

    client = ModbusTcpClient(host="1.1.1.1", framer=ModbusRtuFramer)

I have extensively tested this, so your mileage may vary.  This is contained in github commit: bdbcc512d6e284339f3861b7e63f51209cd40845

Ivica

unread,
Dec 16, 2012, 4:57:53 PM12/16/12
to pymo...@googlegroups.com
I can't thank you enough for your work in general, but doing something like this is just insane! Thank you very much.

Ivica

unread,
Dec 19, 2012, 5:23:53 AM12/19/12
to pymo...@googlegroups.com
Hello Bashwork,

I tried to implement your changes, and I'm getting some errors. Here is the code: http://pastebin.com/5NskRsFB with some comments about errors.

Am I doing something wrong? Software that came with the device communicates with it using the same settings.

Ivica

unread,
Dec 19, 2012, 5:24:34 AM12/19/12
to pymo...@googlegroups.com

Bashwork

unread,
Dec 21, 2012, 5:40:52 PM12/21/12
to pymo...@googlegroups.com
It seems that there is an error in the decoding step, could you enable the debug logging and copy what is logged to console; here is an example of doing this:

Ivica

unread,
Dec 24, 2012, 1:59:10 PM12/24/12
to pymo...@googlegroups.com
I will gladly report back to you, as soon as I get access to the device again, since it's only available to me once a week. And the holidays are starting...

Thank you for your interest in helping me.

Ivica

unread,
Jan 16, 2013, 5:48:20 AM1/16/13
to pymo...@googlegroups.com
client=ModbusTCP(self.ip,self.port,framer=ModbusRtuFramer)
print client
client.connect()
print client.connect()
result = client read_holding_registers(7220,1)        
print result

True #client.connect()
DEBUG:pymodbus.transaction:Running transaction 1 #logged to console
DEBUG:pymodbus.transaction:Transaction failed. (timed out) #logged to console
DEBUG:pymodbus.transaction:Transaction failed. (timed out) #logged to console
DEBUG:pymodbus.transaction:Transaction failed. (timed out) #logged to console
None #result

I've got this from my code. Did I turn the logging on properly? Were you expecting something like this on the output, or am I wrong?

Michael401

unread,
Mar 28, 2013, 5:10:04 PM3/28/13
to pymo...@googlegroups.com
Hi,

I am also looking for a very basic example of using a rtu frame in tcp.

Here is my code (which does not work)


from pymodbus.client.sync import ModbusTcpClient as ModbusClient

from pymodbus.transaction import ModbusRtuFramer

client = ModbusClient(’localhost’, port=5020, framer=ModbusRtuFramer)

client.read_holding_registers(7,20,unit=1)



Ok, so first issue I have is that:

client = ModbusClient(’x.x.x.x', port=5020, framer=ModbusRtuFramer)

gives me this error

Exception AttributeError: "'ModbusTcpClient' object has no attribute 'socket'" in <bound method ModbusTcpClient.__del__ of <pymodbus.client.sync.ModbusTcpClient object at 0x7f0e54f9a250>> ignored Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __init__() got an unexpected keyword argument 'framer'



If I do this instead

>>>client = ModbusClient()

>>> client.framer

<pymodbus.transaction.ModbusSocketFramer object at 0x2064610>

>>> client.framer = ModbusRtuFramer

>>> client.host = 'x.x.x.x'

>>> client.port = 20002

>>> client.connect()

True


Then I try reading a holding register
results = client.read_holding_registers(7,20,unit=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/pymodbus/client/common.py", line 109, in read_holding_registers return self.execute(request) File "/usr/local/lib/python2.7/dist-packages/pymodbus/client/sync.py", line 81, in execute return self.transaction.execute(request) File "/usr/local/lib/python2.7/dist-packages/pymodbus/transaction.py", line 70, in execute self.client._send(self.client.framer.buildPacket(request)) TypeError: unbound method buildPacket() must be called with ModbusRtuFramer instance as first argument (got ReadHoldingRegistersRequest instance instead)


The second issue is more pressing as I am sure that I am missing the point of using a non-default framer.
I was hoping that someone could publish a quick clear example of how to connect to an modbus server using RTU frame of TCP. For my project, I will be talking to a serial device on the other side of a microhard ipn3G modem va a tcp connection.

Bashwork

unread,
Mar 28, 2013, 10:02:34 PM3/28/13
to pymo...@googlegroups.com

Michael401

unread,
Mar 29, 2013, 11:58:54 AM3/29/13
to pymo...@googlegroups.com
Hey,
copied line for line I still get the error
>>> from pymodbus.client.sync import ModbusTcpClient as ModbusClient
>>> from pymodbus.transaction import ModbusRtuFramer as ModbusFramer
>>> client = ModbusClient('74.198.34.203', port=20002, framer=ModbusFramer)
Exception AttributeError: "'ModbusTcpClient' object has no attribute 'socket'" in <bound method ModbusTcpClient.__del__ of <pymodbus.client.sync.ModbusTcpClient object at 0x7ff0433a6c10>> ignored
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() got an unexpected keyword argument 'framer'
>>> 


I believe I have the latest version of pymodbus (i did a  pip install -U pymodbus just to be sure)


Any ideas?

Thanks!
Mike.

Galen Collins

unread,
Mar 29, 2013, 1:42:56 PM3/29/13
to pymo...@googlegroups.com
Could you install from github instead of pip real quick, I don't think what is on pypi is the latest version.

Galen


--
You received this message because you are subscribed to the Google Groups "pymodbus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pymodbus+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael401

unread,
Mar 29, 2013, 2:35:53 PM3/29/13
to pymo...@googlegroups.com
Yep, I will give that a try and get back to you.

Thanks for the help!

Mike.

Michael401

unread,
Mar 29, 2013, 3:27:15 PM3/29/13
to pymo...@googlegroups.com
Yep that worked.

Thanks!
Reply all
Reply to author
Forward
0 new messages