新增步进电机方向控制,振动引脚

This commit is contained in:
HJW
2025-02-19 16:18:41 +08:00
parent 2b96624a1a
commit b95980afe1
9 changed files with 189 additions and 14 deletions

View File

@ -7,13 +7,15 @@
'''
import socket
import json
import time
cmd_set_target = {
# 称量
"command": "set_target",
"payload": {
"target_weight": 200,
"algorithm": "pid"
"algorithm": "pid",
"direction_control": False
}
}
@ -43,8 +45,30 @@ cmd_set_vibrate = { # 振动控制
}
}
# 使用 with 语句确保 socket 在使用完毕后正确关闭
with socket.socket() as s:
s.connect(('127.0.0.1', 5000))
s.send(json.dumps(cmd_set_target).encode())
print(s.recv(1024).decode())
# 发送命令
try:
s.sendall(json.dumps(cmd_set_target).encode())
except socket.error as e:
print(f"发送数据时发生错误: {e}")
# break # 如果发送失败,退出循环
start_time = time.time()
# 接收数据
while True:
try:
data = s.recv(1024)
if not data:
print("没有收到数据,连接可能已关闭")
# break # 如果没有数据,退出循环
time.sleep(1)
print(data.decode())
end_time = time.time()
elapsed_time = end_time - start_time
print(f"代码执行时间:{elapsed_time:.6f}")
break
except socket.error as e:
print(f"接收数据时发生错误: {e}")
#break # 如果接收失败,退出循环