Initial commit
This commit is contained in:
40
tests/test_device.py
Normal file
40
tests/test_device.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
import threading
|
||||
import time
|
||||
|
||||
import serial
|
||||
|
||||
PORT = "/dev/ttyACM0"
|
||||
BAUDRATE = 115200
|
||||
|
||||
|
||||
def read_from_port(ser):
|
||||
while True:
|
||||
if ser.in_waiting > 0:
|
||||
data = ser.read(ser.in_waiting)
|
||||
print(f"Received: {data.decode('utf-8', errors='ignore')}")
|
||||
|
||||
|
||||
try:
|
||||
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
|
||||
print(f"Connected to {PORT}")
|
||||
|
||||
# Запускаем чтение в фоне
|
||||
thread = threading.Thread(target=read_from_port, args=(ser,), daemon=True)
|
||||
thread.start()
|
||||
|
||||
# Пишем данные
|
||||
counter = 0
|
||||
while True:
|
||||
msg = f"Ping {counter}\n"
|
||||
ser.write(msg.encode("utf-8"))
|
||||
print(f"Sent: {msg.strip()}")
|
||||
counter += 1
|
||||
time.sleep(1)
|
||||
|
||||
except serial.SerialException as e:
|
||||
print(f"Error: {e}")
|
||||
except KeyboardInterrupt:
|
||||
print("\nExiting...")
|
||||
if "ser" in locals() and ser.is_open:
|
||||
ser.close()
|
||||
Reference in New Issue
Block a user