from fastapi import FastAPI import serial import uvicorn import threading app = FastAPI() ser = serial.Serial('COM6', 9600) line = '0' def read_value(): global line while True: if ser.in_waiting > 0: line = ser.read().decode() threading.Thread(target=read_value, daemon=True).start() @app.get("/stat") async def get_stat(): return {'state': line} @app.get("/") async def root(): return {'message': 'hello world'} uvicorn.run(app, host="0.0.0.0", port=12346)