-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_main.py
More file actions
44 lines (35 loc) · 964 Bytes
/
Copy pathserial_main.py
File metadata and controls
44 lines (35 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from fastapi import FastAPI
from pydantic import BaseModel, Field
from loguru import logger
import serial # This is the "pyserial" package
app = FastAPI()
class GlobalOptArgs(BaseModel):
uart_p: str = '/dev/ttyACM0'
baudrate: int = 115200
uart_timeout: int = 1
G = GlobalOptArgs()
### Begin App
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/c")
def get_config():
global G
logger.info(G)
return G
### End App
def loop_forever(S):
cnt=0
logger.info(S)
while (True):
cnt = cnt + 1
ln = S.readline()
print("%d: %s" % (cnt, ln))
if __name__ == '__main__':
logger.info("Main")
Args = GlobalOptArgs()
logger.info(Args)
S = serial.Serial(Args.uart_p, Args.baudrate, timeout=Args.uart_timeout)
loop_forever(S)
#S = serial.Serial(Args.uart_p, Args.baudrate, Args.uart_timeout)
#S = serial.Serial('/dev/ttyACM0', 115200, timeout=1)