Trouble getting data using Modbus/TCP with RTU

1,753 views
Skip to first unread message

Vivek Dwivedi

unread,
May 9, 2013, 12:02:58 AM5/9/13
to pymo...@googlegroups.com
Greetings:

I have an external device (Mass Flow Controller) with an ethernet port that can be interfaced using Modbus/TCP with RTU:

An example register map for this device is:

I/O description:  Flow
Modbus Function Code (read): 4
Reference Register Number: 0x4000
Number of Registers: 2
Unit: sccm
Data Type: float 4 byte

I am connecting to this device as follows using the ModbutRTUFramer...

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer

import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

client = ModbusClient('192.168.2.176',port=502,framer=ModbusRtuFramer)
client.connect()

address = 0x4000
count = 2
result  = client.read_input_registers(address, count)

My print results always give me "None"

What am I doing wrong???

Any help will be appreciated

Galen Collins

unread,
May 9, 2013, 12:08:04 AM5/9/13
to pymo...@googlegroups.com
Can you let me know what version you are using?  There was a regression with this use case that was fixed in the most recent release on github.  Could you make sure that you are using that code.  Also, here is a complete working example (which looks like what you are already using):


Could you also add any logging information that may be output?


--
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.
 
 

Vivek Dwivedi

unread,
May 9, 2013, 12:34:55 AM5/9/13
to pymo...@googlegroups.com
Greeting thank you for prompt reply.  I have tried to reinstall pymodbus using the example given by:

cd pymodbus
sudo python setup.py install

however, using this method I get the following errors:
Best match: Twisted 13.0.0
Processing Twisted-13.0.0.tar.bz2
Writing /tmp/easy_install-gtv0K3/Twisted-13.0.0/setup.cfg
Running Twisted-13.0.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-gtv0K3/Twisted-13.0.0/egg-dist-tmp-mdZ9io
cc1: error: unrecognized command line option "-arch"
cc1: error: unrecognized command line option "-arch"
cc1: error: unrecognized command line option "-arch"
cc1: error: unrecognized command line option "-arch"
cc1: error: unrecognized command line option "-arch"
cc1: error: unrecognized command line option "-arch"
error: Setup script exited with error: command 'gcc' failed with exit status 1

I am on an OS X machine.

Thank you again.

Vivek Dwivedi

unread,
May 9, 2013, 1:06:21 AM5/9/13
to pymo...@googlegroups.com
I reran the code and I still get "none"

>>> from pymodbus.constants import Endian
>>> from pymodbus.payload import BinaryPayloadDecoder
>>> from pymodbus.payload import BinaryPayloadBuilder
>>> from pymodbus.client.sync import ModbusTcpClient as ModbusClient
>>> from pymodbus.transaction import ModbusRtuFramer
>>> import logging
>>> logging.basicConfig()
>>> log = logging.getLogger()
>>> log.setLevel(logging.INFO)
>>> client = ModbusClient('192.168.2.176',port=502,framer=ModbusRtuFramer)
>>> client.connect()
True
>>> address = 0x4000
>>> count   = 2
>>> result  = client.read_input_registers(address, count)
>>> print result
None
>>> result  = client.read_input_registers(address, 29)
>>> print result
None
>>> address = 0x4002
>>> print result  = client.read_input_registers(address, count)
SyntaxError: invalid syntax
>>> result  = client.read_input_registers(address, count)
>>> print result
None

On Thursday, May 9, 2013 12:08:04 AM UTC-4, Bashwork wrote:

Galen Collins

unread,
May 9, 2013, 1:10:50 AM5/9/13
to pymo...@googlegroups.com
Oh, I actually haven't tested installing twisted on osx too much.  What you can do for the short term is simply remove or comment the twisted dependency out in the setup.py as you are not using it anyways.

Galen

Galen Collins

unread,
May 9, 2013, 1:13:58 AM5/9/13
to pymo...@googlegroups.com
Have you set the logging to DEBUG and seen if there is anything printed out?

Vivek Dwivedi

unread,
May 9, 2013, 1:31:40 AM5/9/13
to pymo...@googlegroups.com
Ok, I downloaded the pymodbus-master from the git and removed any mention of "Twisted" and reinstalled.

Vivek Dwivedi

unread,
May 9, 2013, 1:32:32 AM5/9/13
to pymo...@googlegroups.com
Here is my debug results:

DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:getting transaction 1

Traceback (most recent call last):
  File "/Users/vivekd/Desktop/test.py", line 60, in <module>
    decoder = BinaryPayloadDecoder.fromRegisters(result.registers, endian=Endian.Little)
AttributeError: 'NoneType' object has no attribute 'registers'

Galen Collins

unread,
May 9, 2013, 12:51:01 PM5/9/13
to pymo...@googlegroups.com
So it looks like your device isn't responding to the requests.  Can you give me more information about your device so I can look at a data sheet or can you use another utility (say http://www.modbusdriver.com/modpoll.html) to quickly see if you can get any kind of response at all?

Galen

Vivek Dwivedi

unread,
May 9, 2013, 1:47:07 PM5/9/13
to pymo...@googlegroups.com
Thank you Galen:

Are you aware of a utility that I can use under OS X?  BTW if I want to remove the pymodbus library and reinstall it, what would be the best way to go about it?

Thanks.

--Vivek

Galen Collins

unread,
May 9, 2013, 5:45:31 PM5/9/13
to pymo...@googlegroups.com

I actually do all my development under Linux, so I don't really know any quick utilities. I know there is the jamod java library, but I don't think it has any utilities built for it.

As for uninstalling, I would recommend running in virtualenv with pip as the easiest way to manage dependencies. Then it is just pip uninstall pymodbus.

With easy install, you actually need to manually delete the .egg file or directory (more information can be found by googling easy_install).

Galen

Vivek Dwivedi

unread,
May 9, 2013, 8:26:56 PM5/9/13
to pymo...@googlegroups.com
Ok, so I am using ModBus Probe, http://www.modbusapps.com/mbp.php to see if I can connect to my device and read input registers and I can.

in python 2.7 however I still get the following error:

>>> import pymodbus
>>> from pymodbus.client.sync import ModbusTcpClient as ModbusClient
>>> from pymodbus.transaction import ModbusRtuFramer
>>> import logging
>>> logging.basicConfig()
>>> log = logging.getLogger()
>>> log.setLevel(logging.DEBUG)
>>> client = ModbusClient('192.168.2.176',port=502,framer=ModbusRtuFramer)
>>> client.connect()
True
>>> address = 0x4000
>>> count   = 2
>>> result  = client.read_input_registers(address, count)
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:getting transaction 1

Vivek Dwivedi

unread,
May 10, 2013, 11:04:07 PM5/10/13
to pymo...@googlegroups.com
Galen:

I want to make sure that I am installing pymodbus without the twisted dependencies properly, this is what I do:

cd pymodbus 

I edit the setup.py file and comment out:

install_requires = [
       # 'twisted >= 12.2.0',
        'pyserial >= 2.6'
    ],

I then cd..
and run

vhdapple:Desktop vivekd$ easy_install pymodbus 

and here is my output:

vhdapple:Desktop vivekd$ easy_install pymodbus
Processing pymodbus
Writing /Users/vivekd/Desktop/pymodbus/setup.cfg
Running setup.py -q bdist_egg --dist-dir /Users/vivekd/Desktop/pymodbus/egg-dist-tmp-nwdZ2g
Adding pymodbus 1.2.0 to easy-install.pth file

Installed /Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/pymodbus-1.2.0-py2.7.egg
Processing dependencies for pymodbus==1.2.0
Finished processing dependencies for pymodbus==1.2.0

Does this look correct??

Thanks.

On Thursday, May 9, 2013 5:45:31 PM UTC-4, Bashwork wrote:

Galen Collins

unread,
May 10, 2013, 11:05:38 PM5/10/13
to pymo...@googlegroups.com

That is fine.

Vivek Dwivedi

unread,
May 11, 2013, 12:20:12 AM5/11/13
to pymo...@googlegroups.com
Ok, so I know for a fact that I can read my device using Modbus probe...Here is code:

from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
from pymodbus.transaction import ModbusRtuFramer

#---------------------------------------------------------------------------# 
# configure the client logging
#---------------------------------------------------------------------------# 
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------# 
# We are going to use a simple client to send our requests
#---------------------------------------------------------------------------# 
client = ModbusClient('192.168.2.176',port=502,framer=ModbusRtuFramer)
client.connect()

address = 0x4000
count   = 2
result  = client.read_input_registers(address,count)
print result
#---------------------------------------------------------------------------# 
# close the client
#---------------------------------------------------------------------------# 
client.close()

and here is the result:

>>> 
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:Transaction failed. (timed out) 
DEBUG:pymodbus.transaction:getting transaction 1
None

I can't figure out what is wrong.  I'm attaching a short excerpt of the modbus codes....I'm at an impasse and I can't figure out what I am doing wrong.
P4 Modbus Specification Rev 1.1-Draft.pdf

Vivek Dwivedi

unread,
May 13, 2013, 1:15:55 PM5/13/13
to pymo...@googlegroups.com
Ok, So I have been able to communicate (get and receive info) by not using the RTU framework though it states in my manual that my device is Modbus/TCP with RTU protocol.



On Friday, May 10, 2013 11:05:38 PM UTC-4, Bashwork wrote:

Galen Collins

unread,
May 13, 2013, 2:03:16 PM5/13/13
to pymo...@googlegroups.com
Oh, is your device a modbus TCP that also supports RTU?  That makes more sense and you can test this by directly connecting to your device's serial port and using the serial client with RTU.  For now, I would recommend just using the TCP going forward if it works.

Galen

Sorry again for the slow responses, I have been pretty busy lately.
Reply all
Reply to author
Forward
0 new messages