-
I would like to run ble-serial concurrently with the code I use to communicate with a device and update a website. When I run ble-serial in command prompt and then run my code in the editor, it works fine. However, if I attempt to run ble-serial from within my code, it only runs ble-serial and never reaches the rest of my code where the updating would take place. def get_hr_only(portname, time_wait = 0.01):
send_command(portname, READ)
try:
while True:
if (portname.in_waiting > 0):
yield(portname.read(portname.in_waiting).decode("ascii").split(','))
time.sleep(time_wait)
except KeyboardInterrupt as e:
print("Done executing loop")
stop_device(portname)
ser = serial.Serial("COM9",115200,8,'N',1)
ser.timeout = None
ser.is_open
sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)
pauseUpdates = True
timeOffset = 0
async def index(request):
with open('index2.html') as f:
return web.Response(text=f.read(), content_type='text/html')
app.router.add_get('/', index)
# Listens for messages from the website.
@sio.event
def s_PauseUpdates(sid, isPaused):
global pauseUpdates
pauseUpdates = isPaused
# loop that sends messages to the website
async def send_data_to_webclient():
hr = get_hr_only(ser)
for i in hr:
try:
if pauseUpdates:
send_command(ser, "pause 0\n")
await sio.emit('c_UpdateData', i[2])
else:
send_command(ser, "pause 1\n")
await sio.sleep(1)
except IndexError:
continue
async def init_app():
sio.start_background_task(send_data_to_webclient)
return app
if __name__ == '__main__':
clear_buffers(ser)
set_stream(ser)
web.run_app(init_app()) I would like to start ble-serial in here if __name__ == '__main__':
clear_buffers(ser)
set_stream(ser)
web.run_app(init_app()) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I don't see the complete code, so not entirely sure what is imported and where some variables/functions are coming from. Looks like you are using aiohttp though? Per default it controls the event loop and you should pass the main async function from ble-serial to the app: https://docs.aiohttp.org/en/stable/web_advanced.html#complex-applications Also you have some sync IO with the Serial part, which can cause issues. If you don't actually need a serial port (for other programs etc.) then you might consider removing this and hook directly into the BLE interface, see the example: #63 (comment) |
Beta Was this translation helpful? Give feedback.
I don't see the complete code, so not entirely sure what is imported and where some variables/functions are coming from. Looks like you are using aiohttp though? Per default it controls the event loop and you should pass the main async function from ble-serial to the app: https://docs.aiohttp.org/en/stable/web_advanced.html#complex-applications
Also you have some sync IO with the Serial part, which can cause issues. If you don't actually need a serial port (for other programs etc.) then you might consider removing this and hook directly into the BLE interface, see the example: #63 (comment)
Instead of
asyncio.run
it would be the append to your server context.