+标定程序

This commit is contained in:
cdeyw
2025-02-17 14:51:56 +08:00
parent 6f0bbae288
commit 69dcef1710

View File

@ -1,11 +1,12 @@
import socket
import binascii
import time
# 网络继电器的 IP 和端口
HOST = '192.168.20.18'
PORT = 50000
# 电磁阀控制报文
valve_commands = {
1: {
'open': '00000000000601050000FF00',
@ -21,43 +22,58 @@ valve_commands = {
}
}
# 将十六进制字符串转换为字节数据并发送
def send_command(command):
byte_data = binascii.unhexlify(command)
# 创建套接字并连接到继电器
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
try:
sock.connect((HOST, PORT))
sock.send(byte_data)
# 接收响应
response = sock.recv(1024)
return response
print(f"收到响应: {binascii.hexlify(response)}")
# 校验响应
if response == byte_data:
print("命令成功下发,继电器已执行操作。")
return True
else:
print("命令下发失败,响应与请求不符。")
return False
except Exception as e:
print(f"通信错误: {e}")
return False
# 控制电磁阀打开
def open(grasp, throw, shake):
if grasp:
print("打开电磁阀 1") # 抓取
send_command(valve_commands[1]['open'])
#缺少反馈函数 不知道命令是否下发正确 是否需要等待延时
if send_command(valve_commands[1]['open']):
time.sleep(1) # 等待 1 秒
if throw:
print("打开电磁阀 2") # 扔袋
send_command(valve_commands[2]['open'])
if send_command(valve_commands[2]['open']):
time.sleep(1) # 等待 1 秒
if shake:
print("打开电磁阀 3") # 震动
send_command(valve_commands[3]['open'])
if send_command(valve_commands[3]['open']):
time.sleep(1) # 等待 1 秒
# 控制电磁阀关闭
def close(grasp, throw, shake):
if grasp:
print("关闭电磁阀 1")
send_command(valve_commands[1]['close'])
if send_command(valve_commands[1]['close']):
time.sleep(1) # 等待 1 秒
if throw:
print("关闭电磁阀 2")
send_command(valve_commands[2]['close'])
if send_command(valve_commands[2]['close']):
time.sleep(1) # 等待 1 秒
if shake:
print("关闭电磁阀 3")
send_command(valve_commands[3]['close'])
if send_command(valve_commands[3]['close']):
time.sleep(1) # 等待 1 秒
# close(True,True,True)#参数传True和False
# 关闭电磁阀
close(True, True, True) # 参数传True和False