Files
VSC/Python3/Logger/Serial/basic_seriallogger_v2.py
claudio 368d6fafea Issue
Code backup
2026-05-10 16:59:01 +02:00

33 lines
689 B
Python

import serial
import struct
import time
# configurazione porta
PORT = 'COM3' # cambia se serve
BAUD = 921600
# struttura Sample = 1 uint32 + 2 IMUData
# IMUData = 6 int16
# Formato struct: < = little-endian
fmt = '<I6h6h' # ts, imu1(6h), imu2(6h)
sample_size = struct.calcsize(fmt)
ser = serial.Serial(PORT, BAUD, timeout=1)
print(f"Listening on {PORT}...")
try:
while True:
data = ser.read(sample_size)
if len(data) != sample_size:
continue
s = struct.unpack(fmt, data)
ts = s[0]
imu1 = s[1:7]
imu2 = s[7:13]
print(f"{ts} | 1:{imu1} | 2:{imu2}")
except KeyboardInterrupt:
ser.close()
print("Closed")