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

5 次查看(过去 30 天)
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()

回答(2 个)

Monika Jaskolka
Monika Jaskolka 2021-6-7

Sean de Wolski
Sean de Wolski 2021-6-7
Why not use MATLAB's native MODBUS support? MODBUS Communication - MATLAB & Simulink (mathworks.com)
  1 个评论
Kausthub Narayan
Kausthub Narayan 2021-6-7
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Python Package Integration 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by