30 lines
730 B
Python
30 lines
730 B
Python
import socket
|
|
import time
|
|
from protocol import build_frame
|
|
|
|
SERVER_IP = '127.0.0.1'
|
|
SERVER_PORT = 9000
|
|
|
|
DEVICE_ID = 10000052 # 每个设备改这个
|
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s.connect((SERVER_IP, SERVER_PORT))
|
|
print(f"[DEVICE {DEVICE_ID}] connected")
|
|
|
|
while True:
|
|
# 发送心跳
|
|
hb = build_frame(0x00, DEVICE_ID)
|
|
s.sendall(hb)
|
|
|
|
# 接收控制指令
|
|
s.settimeout(1)
|
|
try:
|
|
data = s.recv(1024)
|
|
if data and data[8] == 0xA0:
|
|
print(f"[DEVICE {DEVICE_ID}] recv A0:", data.hex(' '))
|
|
# 这里执行你的 IO / GPIO
|
|
except socket.timeout:
|
|
pass
|
|
|
|
time.sleep(3)
|