필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Kausthub Narayan
Kausthub Narayan 2021년 6월 7일
댓글: Kausthub Narayan 2021년 6월 7일
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에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by