控制机器臂
import socket
import sys
received_count: int = 0
def getCmdStr(cmd):
if cmd == '011':
return "00 00 00 00 00 06 01 05 00 00 FF 00"
elif cmd == '012':
return "00 00 00 00 00 06 01 05 00 00 00 00"
elif cmd == '021':
return "00 00 00 00 00 06 01 05 00 01 FF 00"
elif cmd == '022':
return "00 00 00 00 00 06 01 05 00 01 00 00"
elif cmd == "031":
return "00 00 00 00 00 06 01 05 00 02 FF 00"
elif cmd == '032':
return "00 00 00 00 00 06 01 05 00 02 00 00"
elif cmd == '121':
return "00 00 00 00 00 06 01 05 00 11 FF 00"
elif cmd == '122':
return "00 00 00 00 00 06 01 05 00 11 00 00"
else:
return "fail"
def start_tcpClient(ip, port,cmd):
###create socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
failed_count = 0
while True:
try:
print("start connect to server")
client.connect((ip, port))
break
except socket.error:
failed_count += 1
print("fail to connect to server %d times" % failed_count)
if failed_count == 10: return
###send comand
# print("connect success")
# print("011:打开1#口") # :00 00 00 00 00 06 01 05 00 01 FF 00
# print("012:关闭1#口") # :00 00 00 00 00 06 01 05 00 01 00 00
# print("021:打开2#口") # :00 00 00 00 00 06 01 05 00 02 FF 00
# print("022:关闭2#口") # :00 00 00 00 00 06 01 05 00 02 00 00
# print("121:打开12#口") # :00 00 00 00 00 06 01 05 00 0C FF 00
# print("122:关闭12#口") # :00 00 00 00 00 06 01 05 00 0C 00 00
cmdstr = getCmdStr(cmd)#获得代码
if cmdstr == "fail":
print("wrong comand", cmd)
else:
d = bytes.fromhex(cmdstr)
client.send(d)
print("send message:", cmdstr)
recvMsg = client.recv(1024)
print("receive message:", recvMsg)
print("recv len is :[%d]" % len(recvMsg))
###received_count+=1
client.close()
start_tcpClient('192.168.0.12', 502,"012")
# received_data = client.recv(1024)
# received_size = len(received_data)
# print("cmd res receive message:",received_data)
# print("cmd res receive done......",received_size)