Make a little robust with lessons learned later

This commit is contained in:
Danny Staple
2022-11-13 18:46:59 +00:00
parent e444040b86
commit 1d31bee775
2 changed files with 31 additions and 27 deletions
+7 -4
View File
@@ -9,10 +9,12 @@ async def command_handler():
while True:
if robot.uart.in_waiting:
print("Receiving data...")
data = robot.uart.readline().decode()
print(f"Received data: {data}")
request = json.loads(data)
print(f"Received command: {request}")
try:
data = robot.uart.readline().decode()
request = json.loads(data)
except (UnicodeError, ValueError):
print("Invalid data")
continue
# {"command": "arena"}
if request["command"] == "arena":
response = {
@@ -20,5 +22,6 @@ async def command_handler():
"target_zone": arena.target_zone,
}
robot.uart.write((json.dumps(response)+"\n").encode())
await asyncio.sleep(0.1)
asyncio.run(command_handler())