Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理并行运行2个任务-使用Bluepy在Raspberry Pi上进行项目_Python_Raspberry Pi - Fatal编程技术网

Python 多处理并行运行2个任务-使用Bluepy在Raspberry Pi上进行项目

Python 多处理并行运行2个任务-使用Bluepy在Raspberry Pi上进行项目,python,raspberry-pi,Python,Raspberry Pi,我正在进行一个项目,其目标是使用蓝牙低能量将几个nano 33 iot Arduinos(作为外围设备)连接到一个Raspberry pi(作为中心设备) 我的Arduinos不断向Raspberry Pi发送数据。我正在使用“侦听通知”功能实时截获所有数据 我的代码如下所示: from bluepy import btle class MyDelegate(btle.DefaultDelegate): def __init__(self,params): btle.D

我正在进行一个项目,其目标是使用蓝牙低能量将几个nano 33 iot Arduinos(作为外围设备)连接到一个Raspberry pi(作为中心设备)

我的Arduinos不断向Raspberry Pi发送数据。我正在使用“侦听通知”功能实时截获所有数据

我的代码如下所示:

from bluepy import btle

class MyDelegate(btle.DefaultDelegate):
    def __init__(self,params):
        btle.DefaultDelegate.__init__(self)

    def handleNotification(self,cHandle,data):
        print(data)


def connectionArduino1() :
    global Arduino1
    print("trying to connect to Arduino1")
    connected = False
    while(connected == False) :
        try :
            Arduino1 = btle.Peripheral("xx:xx:xx:xx:xx:xx")
            Arduino1.setDelegate(MyDelegate(0))

            servicesArduino1=Arduino1.getServices()

            sArduino1 = Arduino1.getServiceByUUID(UUID)
            chArduino1 = sArduino1.getCharacteristics()[0]

            Arduino1.writeCharacteristic(14,bytes('Start','utf-8'))
            Arduino1.writeCharacteristic(chArduino1.valHandle+1, (1).to_bytes(2, byteorder='little'))
            connected = True
        except :
            connected = False


def connectionArduino2() :
    global Arduino2
    print("trying to connect to Arduino2")
    connected = False
    while(connected == False) :
        try :
            Arduino2 = btle.Peripheral("xx:xx:xx:xx:xx:xx")
            Arduino2.setDelegate(MyDelegate(0))

            servicesArduino2=Arduino2.getServices()

            sArduino2 = Arduino2.getServiceByUUID(UUID)
            chArduino2 = sArduino2.getCharacteristics()[0]

            Arduino2.writeCharacteristic(14,bytes('Start','utf-8'))
            Arduino2.writeCharacteristic(chArduino2.valHandle+1, (1).to_bytes(2, byteorder='little'))
            connected = True
        except :
            connected = False


def mainLoop() :
    while True:

        try :
            if Arduino1.waitForNotifications(5) :
                print("Notification received")
            else :
                try :
                    Arduino1.disconnect()
                    connectionArduino1()
                except :
                    connectionArduino1()
        except :
            connectionArduino1()

        try :
            if Arduino2.waitForNotifications(5) :
                print("Notification received")
            else :
                try :
                    Arduino2.disconnect()
                    connectionArduino2()
                except :
                    connectionArduino2()
        except :
            connectionArduino2()

connectionArduino1()
connectionArduino2()
mainLoop()
main循环中的
try
命令:

  • 确保如果其中一个Arduinos断开,程序不会崩溃
  • 如果Arduino断开连接,则自动将Raspberry Pi重新连接到Arduino(通过启动
    连接Arduinox
    功能)
  • 问题是当两个Arduinos中的一个断开时:当程序尝试重新连接到它时,它不再能够读取从其他Arduinos接收到的通知。因此,
    mainLoop
    功能暂停

    我想创建一个健壮的程序,该程序能够读取连接的Arduinos接收到的所有通知,并且能够并行地重新连接到Arduinos(如果其中一个断开连接),以便
    mainLoop
    功能永远不会暂停

    我尝试按如下方式创建线程:

    from bluepy import btle
    from threading import Thread
    
    class MyDelegate(btle.DefaultDelegate):
        def __init__(self,params):
            btle.DefaultDelegate.__init__(self)
    
        def handleNotification(self,cHandle,data):
            print(data)
    
    Arduino1Connected = False
    Arduino2Connected = False
    
    def connectionArduino1() :
        global Arduino1
        global Arduino1Connected
        print("trying to connect to Arduino1")
        connected = False
        while(Arduino1Connected == False) :
            try :
                Arduino1 = btle.Peripheral("xx:xx:xx:xx:xx:xx")
                Arduino1.setDelegate(MyDelegate(0))
                
                servicesArduino1=Arduino1.getServices()
                
                sArduino1 = Arduino1.getServiceByUUID(UUID)
                chArduino1 = sArduino1.getCharacteristics()[0]
    
                Arduino1.writeCharacteristic(14,bytes('Start','utf-8'))
                Arduino1.writeCharacteristic(chArduino1.valHandle+1, (1).to_bytes(2, byteorder='little'))
                Arduino1Connected = True
            except :
                Arduino1Connected = False
                
                
    def connectionArduino2() : 
        global Arduino2
        global Arduino2Connected
        print("trying to connect to Arduino2")
        connected = False
        while(Arduino2Connected == False) :
            try :
                Arduino2 = btle.Peripheral("xx:xx:xx:xx:xx:xx")
                Arduino2.setDelegate(MyDelegate(0))
                
                servicesArduino2=Arduino2.getServices()
                
                sArduino2 = Arduino2.getServiceByUUID(UUID)
                chArduino2 = sArduino2.getCharacteristics()[0]
    
                Arduino2.writeCharacteristic(14,bytes('Start','utf-8'))
                Arduino2.writeCharacteristic(chArduino2.valHandle+1, (1).to_bytes(2, byteorder='little'))
                Arduino2Connected = True
            except :
                Arduino2Connected = False
                
                
    def mainLoop() : 
        while True:
        
            try : 
                if Arduino1.waitForNotifications(5) :
                    print("Notification received")
                else :
                    try :
                        Arduino1.disconnect()
                        Arduino1Connected = False
                    except :
                        Arduino1Connected = False
            except :
                Arduino1Connected = False
    
            try : 
                if Arduino2.waitForNotifications(5) :
                    print("Notification received")
                else :
                    try :
                        Arduino2.disconnect()
                        Arduino2Connected = False
                    except :
                        Arduino2Connected = False
            except :
                Arduino2Connected = False
    
    thread1 = Thread(target = mainLoop)
    thread2 = Thread(target = connectionArduino1)
    thread3 = Thread(target = connectionArduino2)
    
    thread1.start()
    thread2.start()
    thread3.start()
    
    thread1.join()
    thread2.join()
    thread3.join()
    
    但这不起作用,我认为我的代码要慢得多

    所以我考虑使用线程或多处理原理。具有读取从连接的Arduinos接收到的所有通知的
    mainLoop
    进程,以及能够检测其中一个Arduinos可能断开连接并能够自动重新连接的进程

    你知道我怎样才能做到这一点吗


    或者您是否有其他解决方案允许连续读取Arduinos接收到的所有通知,并并行检测断开连接并直接重新连接?

    我想我应该创建一个
    队列
    ,然后为每个通过队列的Arduino客户端启动一个线程。然后,线程处理其客户机,重新连接并将接收到的任何消息放入队列。主程序可以使用
    select
    在任一队列上等待消息,或者更简单的是,检查每个队列是否有消息,而不是阻止等待消息。经过几次测试后,它终于工作了,谢谢您的回答