Project/main.py

28 lines
662 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import FastAPI
import serial
app = FastAPI()
# Открываем последовательный порт
ser = serial.Serial('COM4', 9600)
count = 0
@app.get("/count")
async def get_count():
global count
count += 1
return {"count": count}
@app.get("/")
async def root():
if ser.is_open:
# Читаем данные с порта, если порт открыт
sensor_val = ser.readline().strip().decode()
return {"sensor_value": sensor_val}
else:
return {"error": "Serial port is not open"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=12345)