Files
ElecScalesMeasur/send_vibration_control.py
2025-09-30 14:57:14 +08:00

68 lines
1.5 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
# @Time : 2025/6/18 10:27
# @Author : reenrr
# @File : send_target-test.py
'''
import socket
import json
import time
target_weight = 200
set_vibrate_time = 10
cmd_set_target = {
# 称量
"command": "set_target",
"payload": {
"target_weight": target_weight,
"algorithm": "pid",
"direction_control": False
}
}
cmd_get_weight = {
# 获取重量
"command": "get_weight"
# "payload": {
# "target_weight": 200,
# "algorithm": "pid"
# }
}
cmd_set_zero = {
# 去皮
"command": "set_zero",
"payload": {
# "target_weight": 200,
# "algorithm": "pid"
}
}
cmd_set_vibrate = { # 振动控制
"command": "set_vibrate",
"payload": {
"time": set_vibrate_time # 单位S
}
}
# 使用 with 语句确保 socket 在使用完毕后正确关闭
with socket.socket() as s:
try:
s.connect(('127.0.0.1', 5000))
s.sendall(json.dumps(cmd_set_vibrate).encode())
print("已发送振动控制指令")
# level = gpio.read()
# print(f"GPIO 9 当前电平值为: {level}")
# 接收响应(可选)
data = s.recv(1024)
if data:
print("收到响应:", data.decode())
else:
print("无响应,连接可能已关闭")
except socket.error as e:
print(f"通信错误: {e}")