This commit is contained in:
Антон Кирпиков 2024-05-20 19:48:54 +03:00
parent b43920bacc
commit 4f3726d89b

28
ard.py
View File

@ -2,22 +2,30 @@ import uvicorn
from serial import Serial from serial import Serial
import threading import threading
from fastapi import FastAPI from fastapi import FastAPI
import json
s=""
ser = Serial('COM6', 9600) ser = Serial('COM6', 9600)
def pot(): data = {"value": 0}
global s
def read_data():
global data
while True: while True:
s = ser.read(1) # read up to one hundred bytes incoming_data = ser.read(1) # read up to one byte
th=threading.Thread(target=pot) try:
th.start() data["value"] = int(incoming_data)
except ValueError:
pass
app = FastAPI() app = FastAPI()
def get_data():
return json.dumps(data)
th = threading.Thread(target=read_data)
th.start()
@app.get("/") @app.get("/")
async def root(): async def root():
return ("message", s) return data
if __name__ == "__main__": if __name__ == "__main__":
uvicorn.run(app, port=10000) uvicorn.run(app, port=10000)