From 69dcef1710cb629fe3688884ea5ac1f9e6748671 Mon Sep 17 00:00:00 2001 From: cdeyw <827523911@qq.com> Date: Mon, 17 Feb 2025 14:51:56 +0800 Subject: [PATCH] =?UTF-8?q?+=E6=A0=87=E5=AE=9A=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CU/EMV.py | 56 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/CU/EMV.py b/CU/EMV.py index 89a1d2d..f5aefac 100644 --- a/CU/EMV.py +++ b/CU/EMV.py @@ -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: - sock.connect((HOST, PORT)) - sock.send(byte_data) - response = sock.recv(1024) - return response - + try: + sock.connect((HOST, PORT)) + sock.send(byte_data) + # 接收响应 + response = sock.recv(1024) + 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']) - #缺少反馈函数 不知道命令是否下发正确 是否需要等待延时 + print("打开电磁阀 1") # 抓取 + if send_command(valve_commands[1]['open']): + time.sleep(1) # 等待 1 秒 if throw: - print("打开电磁阀 2")#扔袋 - send_command(valve_commands[2]['open']) + print("打开电磁阀 2") # 扔袋 + if send_command(valve_commands[2]['open']): + time.sleep(1) # 等待 1 秒 if shake: - print("打开电磁阀 3")#震动 - send_command(valve_commands[3]['open']) - + print("打开电磁阀 3") # 震动 + if send_command(valve_commands[3]['open']): + time.sleep(1) # 等待 1 秒 # 控制电磁阀关闭 -def close(grasp, throw, shake ): +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 \ No newline at end of file +# 关闭电磁阀 +close(True, True, True) # 参数传True和False