-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatChain.py
More file actions
44 lines (35 loc) · 1.23 KB
/
floatChain.py
File metadata and controls
44 lines (35 loc) · 1.23 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import serial
import time
PORT = 'COM7' # or '/dev/ttyUSB0'
BAUDRATE = 115200
try:
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
print(f"Connected to {PORT}. Waiting for ESP32 reset...")
time.sleep(2)
ser.reset_input_buffer()
print("Ready.")
while True:
cmd = input("Press A to get 12 float values (Q to quit): ").strip().upper()
if cmd == 'A':
ser.write(b'A')
time.sleep(0.1) # Allow ESP32 to respond
line = ser.readline().decode('utf-8').strip()
print("Raw response:", line)
try:
values = [float(v) for v in line.split(',')]
if len(values) == 12:
print("Received 12 floats:", values)
else:
print(f"Expected 12 values, got {len(values)}")
except ValueError:
print("Invalid float values received.")
elif cmd == 'Q':
break
else:
print("Only 'A' is valid to get data. Use 'Q' to quit.")
except Exception as e:
print("Error:", e)
finally:
if 'ser' in locals() and ser.is_open:
ser.close()
print("Serial port closed.")