2

So I've got a bluetooth connection from an arduino reading a joystick and sending the axis readout over bluetooth to my raspberry pi (4b running ubuntu 20.10). I've confirmed it's receiving this data too.

Now I try to run this bluetooth communcication in a separate process using the python multiprocessing module. to access the data from the arduino, I give the function a queue from the parent main process to put the data in there. Then in the main function I continuously try to read from this queue and process the data there.

The queue in the parent process always remains empty, however, and as such I can't process the data any further.

How can I get the data from the bluetooth process back to the main process?

main.py
#!/usr/bin/env python3
import time
import logging
import multiprocessing as mp
import bluetoothlib

logging.basicConfig(level=logging.DEBUG)

logging.info("creating queue")
global q
q = mp.Queue()      

def main():
    try:
        logging.info("starting bluetooth process")
        p = mp.Process(target=bluetoothlib.serlistener, args=(q,))
        p.start()
    except:
        logging.error("unable to start bluetooth listener")
        
    logging.info("start reading from queue")
    while True:
        #logging.info(q.qsize())
        if not q.empty():
            mss = q.get()
            logging.info(mss)
            #do something with data
        elif q.empty():
            logging.info("queue empty")
            
        time.sleep(1)
            

main()
bluetoothlib.py
#!/usr/bin/env python3
import os
import serial
import io

def serlistener(q):
    print ("creating connection")
    btConn = serial.Serial("/dev/rfcomm0", 57600, timeout=1)
    btConn.flushInput()
    sio = io.TextIOWrapper(io.BufferedRWPair(btConn, btConn, 1),encoding="utf-8")
    
    print ("connection created, starting listening")
    while btConn.is_open:
        try:
            mss = sio.readline()
            q.put(mss)
        except:
            print("error")                                                                              
            break

  • 1
    Why are you using 'global queue';then passing the object q to the serlistener function; and then calling 'queue' for get and put? It seems like you are calling global queue in the main thread; but expected messages in the q object. – thelizardking34 Apr 30 '21 at 15:54
  • @thelizardking34 oh whoops forgot to fix that here, I did fix it in the actual program without any success, I'll fix it in the question now – Alexander de Haan Apr 30 '21 at 15:57
  • 1
    Is the program logging "queue empty" correctly? – thelizardking34 Apr 30 '21 at 16:16
  • @thelizardking34 yep it did do that at least. now with the global things fixed, it works! Thanks for your help! – Alexander de Haan Apr 30 '21 at 16:21

1 Answers1

1

At thelizardking34's suggestion, I relooked at the global stuff I'd been messing with and after correcting it, the code as now given in the question works.

Thanks to thelizardking34!

  • It might be worth adding your global declaration back into post with a comment on what was causing the issue, for future posterity :D – thelizardking34 Apr 30 '21 at 16:47