-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuart_test_script.py
More file actions
30 lines (27 loc) · 836 Bytes
/
Copy pathuart_test_script.py
File metadata and controls
30 lines (27 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import serial
import time
def uart_handshake():
print("Opening serial port...")
try:
ser = serial.Serial('/dev/serial0', baudrate=115200, timeout=1)
except Exception as e:
print(f"Failed to open port: {e}")
return None
print("Pinging Arduino...")
while True:
ser.write(b"PING\n")
time.sleep(0.5) # wait for Arduino to respond
if ser.in_waiting:
line = ser.readline().decode('utf-8', errors='ignore').strip()
print(f"Received: '{line}'")
if line == "PONG":
print("Handshake successful!")
return ser
else:
print("No response, retrying...")
time.sleep(0.5)
if __name__ == "__main__":
ser = uart_handshake()
if ser:
print("UART ready.")
ser.close()