368d6fafea
Code backup
25 lines
715 B
Python
25 lines
715 B
Python
import serial
|
|
from datetime import datetime
|
|
|
|
PORT = "COM3" # oppure "/dev/ttyUSB0"
|
|
BAUDRATE = 115200
|
|
LOG_FILE = "serial_log.txt"
|
|
|
|
ser = serial.Serial(PORT, BAUDRATE, timeout=1)
|
|
|
|
print("In ascolto sulla seriale... Ctrl+C per uscire")
|
|
|
|
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
|
try:
|
|
while True:
|
|
line = ser.readline().decode(errors="ignore").strip()
|
|
if line:
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
|
out = f"[{timestamp}] {line}"
|
|
print(out)
|
|
f.write(out + "\n")
|
|
f.flush()
|
|
except KeyboardInterrupt:
|
|
print("\nChiusura...")
|
|
finally:
|
|
ser.close() |