368d6fafea
Code backup
40 lines
1018 B
Python
40 lines
1018 B
Python
import serial
|
|
import struct
|
|
|
|
# ---------------- CONFIG ----------------
|
|
PORT = 'COM3' # aggiorna con la tua COM
|
|
BAUD = 921600
|
|
|
|
fmt = '<I6h6h' # ts, imu1(6h), imu2(6h)
|
|
sample_size = struct.calcsize(fmt)
|
|
|
|
# ---------------- INIT ------------------
|
|
ser = serial.Serial(PORT, BAUD, timeout=1)
|
|
print(f"Listening on {PORT}...")
|
|
|
|
start_ts = None # primo timestamp ricevuto
|
|
|
|
try:
|
|
while True:
|
|
data = ser.read(sample_size)
|
|
if len(data) != sample_size:
|
|
continue # pacchetto incompleto
|
|
|
|
# ---- unpack del pacchetto ----
|
|
s = struct.unpack(fmt, data)
|
|
ts = s[0] # ts dell'ESP32 in microsecondi
|
|
imu1 = s[1:7]
|
|
imu2 = s[7:13]
|
|
|
|
# ---- calcolo timestamp relativo ----
|
|
if start_ts is None:
|
|
start_ts = ts
|
|
rel_time = (ts - start_ts) / 1_000_000 # in secondi
|
|
|
|
# ---- stampa dati ----
|
|
print(f"{rel_time:.6f}s | 1:{imu1} | 2:{imu2}")
|
|
|
|
except KeyboardInterrupt:
|
|
ser.close()
|
|
print("Closed")
|