Files
mcp_screen/scratch/read_serial.py
T

31 lines
804 B
Python

import serial
import time
import sys
def main():
port = "/dev/cu.usbmodem411CF91D65091"
print(f"Connecting to {port} at 115200...")
try:
ser = serial.Serial(port, 115200, timeout=1.0)
except Exception as e:
print(f"Error opening serial port: {e}")
return
print("Reading serial output for 3 seconds...")
start_time = time.time()
try:
while time.time() - start_time < 3.0:
if ser.in_waiting > 0:
data = ser.read(ser.in_waiting)
sys.stdout.write(data.decode("utf-8", "ignore"))
sys.stdout.flush()
time.sleep(0.1)
except KeyboardInterrupt:
pass
finally:
ser.close()
print("\nSerial connection closed.")
if __name__ == "__main__":
main()