2

I am trying to communicate and read the words on a Mitsubishi PLC (Q06udeh) using the following code:

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('10.1.1.4',port=5007)
client.connect()

result = client.read_holding_registers(1,1)
print result
t = result.registers[0]
print t

I am getting following output:

None

Traceback (most recent call last):
  File "C:\Users\serkan\Desktop\sasda.py", line 8, in <module>
    t = result.registers[0]
 AttributeError: 'NoneType' object has no attribute 'registers'

No matter how I changed the accessing words with different parameters no hope, I still have no success.

Please help.

SiHa
  • 7,830
  • 13
  • 34
  • 43
  • First of all, I would recommend you to use the [modbus_tk](https://pypi.python.org/pypi/modbus_tk) Did you check the doc of `ModbusTcpClient`, what should `read_holding_registers` return? In your case, it returns nothing => None value. Why are you using **5007** port for Modbus? **502** is a recommended one. – Jagoda Gorus Apr 11 '17 at 12:10
  • Thank you very much for fast response , I will try in a couple of minutes? Btw do you know how to access D100 adress value ? – Serkan Demirhan Apr 11 '17 at 12:15
  • Yes, give me some time to prepare an answer. – Jagoda Gorus Apr 11 '17 at 12:15
  • If I do not enter port number as 5007 (I got it from a mitsubishi plc technician), I am getting disconnetion error. even with 502 . [Errno 10061] No connection could be made because the target machine actively refused it – Serkan Demirhan Apr 11 '17 at 12:35
  • This error message ensures me that the 5007Port is indeed ok. Which `device_id` did you specify? It always matters because Modbus also uses that ID to connect to proper device. So suppose it's the problem now. – Jagoda Gorus Apr 11 '17 at 12:37
  • How can see the device_id of the plc ? Currently it is assigned 1 – Serkan Demirhan Apr 11 '17 at 12:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141434/discussion-between-jagoda-sokol-and-serkan-demirhan). – Jagoda Gorus Apr 11 '17 at 12:44

1 Answers1

1

Using modbus_tk

First of all, you will need to install modbus_tk package:

pip install modbus_tk

Then, try to run this sample code:

#  General MODBUS includes
import modbus_tk
import modbus_tk.defines as cst
# TCP MODBUS includes
from modbus_tk import modbus_tcp

def main():
    device_id = 1
    master = modbus_tcp.TcpMaster("10.1.1.4")
    master.set_timeout(3)     #it's really optional
    master.set_verbose(True)  # this is optional too
    data = master.execute(device_id, cst.READ_HOLDING_REGISTERS, 100, 1) #Usage: 100 is registry number and 1 means that size of data is 16bit (modbus protocol specification)
    print "data    ", data
    print "data[0] ", data[0]

if __name__ == "__main__":
    main()

You can find more examples here: https://github.com/ljean/modbus-tk/tree/master/examples

Jagoda Gorus
  • 329
  • 4
  • 18
  • I adde port as master = modbus_tcp.TcpMaster("10.1.1.4",port=5007) I just tried but I am getting this error Traceback (most recent call last): File "C:\Users\serkan\Desktop\sasda.py", line 17, in main() File "C:\Users\serkan\Desktop\sasda.py", line 12, in main data = master.execute(device_id, cst.READ_HOLDING_REGISTERS, 100, 2) File "C:\Python27\lib\site-packages\modbus_tk\utils.py", line 39, in new raise excpt timeout: timed out – Serkan Demirhan Apr 11 '17 at 12:26
  • Have you tried checking the availability of your PLC by `ping` command? Which `device_id` are you using? – Jagoda Gorus Apr 11 '17 at 12:28
  • Why are you using port 5007? Are you sure the device is using this one? – Jagoda Gorus Apr 11 '17 at 12:33
  • If I do not use 5007 , I am having disconneciton error (I got this port number from a mitsubishi plc technician ) . – Serkan Demirhan Apr 11 '17 at 12:37