I need to run this python code in MATLAB. Can anyone guide me, please?

5 views (last 30 days)
This is the python code:
from pyModbusTCP.client import ModbusClient
import time
#Initialize variables
##################
executionTime=0
MODBUS_SERVER_IP="192.168.125.6"
#Your PC must have a fix IP like 192.168.1.xxx for example 192.168.1.10
#Process initialization
##################
# Setup modbus TCP connection parameters
c = ModbusClient(host=MODBUS_SERVER_IP, port=502, auto_open=True)
c.host(MODBUS_SERVER_IP)
c.port(502)
c.unit_id(9)
# Open the connection
c.open()
#Debug mode
#c.debug(True)
#Wait for the connection to establish
time.sleep(5)
#Display register contents
##################
#Display input register where gripper return its status
readData=c.read_input_registers(0,6)
print("Input Register (Gripper status)")
i=0
for value in readData:
print(i," : ",bin(value))
i+=1
#Display input register where gripper control parameter can be written
readData=c.read_holding_registers(0,6)
print("Output Register (Gripper control)")
i=0
for value in readData:
print(i," : ",bin(value))
i+=1
#Initinalise out register
##################
#Reset output register values
response=c.write_multiple_registers(0,[0,0,0])
print(response)
#Request automatic grip
##################
#Write output register to request automatic grip
#This required the re-assertion of rGTO bit
response=c.write_multiple_registers(0,[0b0000000100000000,0,0])
print(response)
response=c.write_multiple_registers(0,[0b0000100100000000,0,0])
print(response)
time.sleep(2)
#Display input register where gripper return its status
readData=c.read_input_registers(0,6)
print("Input Register (Gripper status)")
i=0
for value in readData:
print(i," : ",bin(value))
i+=1
readData=c.read_holding_registers(0,6)
print("Output Register (Gripper control)")
i=0
for value in readData:
print(i," : ",bin(value))
i+=1
time.sleep(3)
#Request automatic release
##################
#Open valve for release
#This required the re-assertion of rATR bit
response=c.write_multiple_registers(0,[0b0000000100000000,0,0])
response=c.write_multiple_registers(0,[0b0001000100000000,0,0])
time.sleep(5)
#Close connection
##################
c.close()
exit()

Answers (2)

Monika Jaskolka
Monika Jaskolka on 7 Jun 2021

Sean de Wolski
Sean de Wolski on 7 Jun 2021
Why not use MATLAB's native MODBUS support? MODBUS Communication - MATLAB & Simulink (mathworks.com)
  1 Comment
Kausthub Narayan
Kausthub Narayan on 7 Jun 2021
It would be helpful if I could run this code on MATLAB without writing a whole new code since I am not an expert at it.

Sign in to comment.

Categories

Find more on Python Package Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!