首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程蓝牙通信Raspberry pi (Python 3)

线程蓝牙通信Raspberry pi (Python 3)
EN

Stack Overflow用户
提问于 2021-01-10 09:00:57
回答 1查看 223关注 0票数 0

我的问题是,我如何实现我的程序的线程,在那里我与Rpi3通信BLE。

我的程序运行得很好,但是响应太慢了。请帮帮忙。谢谢。

BMS_reading:

代码语言:javascript
复制
import gatt
import sys
import time
import threading

class AnyDevice(gatt.Device):

    def write(self, characteristic):
        self.response=bytearray()
        self.bms_write_characteristic.write_value(bytes([0xDD,0xA5,0x03,0x00,0xFF,0xFD,0x77]));

    def services_resolved(self):
        super().services_resolved() 
        device_information_service = next(
            s for s in self.services
            if s.uuid == '0000ff00-0000-1000-8000-00805f9b34fb')

        self.bms_read_characteristic = next(
            c for c in device_information_service.characteristics
            if c.uuid == '0000ff01-0000-1000-8000-00805f9b34fb')
            
        self.bms_write_characteristic = next(
            c for c in device_information_service.characteristics
            if c.uuid == '0000ff02-0000-1000-8000-00805f9b34fb')
            
        self.bms_read_characteristic.enable_notifications()
        self.write(self.bms_read_characteristic)

    def characteristic_value_updated(self, characteristic, value):
        self.value=value
        def write(): 
            self.response+=self.value
            if (self.response.endswith(b'w')):
                self.response=self.response[4:]
                self.SoC=int.from_bytes(self.response[19:20], byteorder = 'big')
                self.manager.stop()
        write()

 
#reading loop (I want add threading and read info "SoC")
while True:
    address="A4:C1:38:A0:59:EB"
    manager = gatt.DeviceManager(adapter_name='hci0')
    device = AnyDevice(mac_address=address, manager=manager)
    device.connect()
    manager.run()
    print("Capacity is: "+str(device.SoC)+"%")

TERMINAL <<< Capacity is: 76% 
#long delay which i dont want
<<< Capacity is: 76% 

我不知道我怎么才能做到。当我让线程全部执行while循环时,通信没有时间做出反应,并打印出错误的数字或错误。

请帮帮忙。

代码语言:javascript
复制
import gatt
import json
import sys
#from gi.repository import GLib

manager = gatt.DeviceManager(adapter_name='hci0')
class AnyDevice(gatt.Device):
    def connect_succeeded(self):
        super().connect_succeeded()
        print("[%s] Připojeno" % (self.mac_address))

    def connect_failed(self, error):
        super().connect_failed(error)
        print("[%s] Connection failed: %s" % (self.mac_address, str(error)))

    def disconnect_succeeded(self):
        super().disconnect_succeeded()
        print("[%s] Disconnected" % (self.mac_address))
        self.manager.stop()

    def services_resolved(self):
        super().services_resolved()

        device_information_service = next(
            s for s in self.services
            if s.uuid == '0000ff00-0000-1000-8000-00805f9b34fb')

        self.bms_read_characteristic = next(
            c for c in device_information_service.characteristics
            if c.uuid == '0000ff01-0000-1000-8000-00805f9b34fb')

        self.bms_write_characteristic = next(
            c for c in device_information_service.characteristics
            if c.uuid == '0000ff02-0000-1000-8000-00805f9b34fb')

        print("BMS found")
        self.bms_read_characteristic.enable_notifications()
        

    def characteristic_enable_notifications_succeeded(self, characteristic):
        super().characteristic_enable_notifications_succeeded(characteristic)
        print("BMS request generic data")
        self.response=bytearray()
        self.bms_write_characteristic.write_value(bytes([0xDD,0xA5,0x03,0x00,0xFF,0xFD,0x77]));

    def characteristic_enable_notifications_failed(self, characteristic, error):
        super.characteristic_enable_notifications_failed(characteristic, error)
        print("BMS notification failed:",error)

    def characteristic_value_updated(self, characteristic, value):
        self.response+=value
        if (self.response.endswith(b'w')):
            self.response=self.response[4:]
            temperature= (int.from_bytes(self.response[23+1*2:1*2+25],'big')-2731)/10
            print("Temperature is: "+str(temperature) + " C")
         

    def characteristic_write_value_failed(self, characteristic, error):
        print("BMS write failed:",error)

device = AnyDevice(mac_address="A4:C1:38:A0:59:EB", manager=manager)
device.connect()
manager.run()

终端打印,即使值发生变化并且管理器正在运行:

代码语言:javascript
复制
>>>BMS found
>>>BMS request generic data
>>>Temperature is: 19 C
#there program get stuck even if value is changing

谢谢,我编辑了带有通知的程序,正如你所看到的,它支持它。

但是我这里有一个问题,即使manager.run ()中的​​(温度)值发生变化并进行管理,即使我对设备进行加热,终端也只会向我发送一个值,而不会执行任何其他操作。当我重新启动程序时,该值再次更改,并且只剩下一个。我的代码写对了吗?

谢谢您的宝贵时间,先生。

EN

回答 1

Stack Overflow用户

发布于 2021-01-10 16:19:54

我的假设是您使用的是gatt-python库。

manager.run()行启动了事件循环,因此您的代码中不需要有while循环。

如果温度特性支持通知,那么当这些值发生变化时,turning them on将是最有效的读取值的方式。

如果设备没有通知,则建议创建一个定时事件,以便以所需的频率读取温度。timeout_add_seconds的文档并不总是最容易理解的,但是导入是:

代码语言:javascript
复制
from gi.repository import GLib

然后,就在运行事件循环调用之前:

代码语言:javascript
复制
GLib.timeout_add_seconds(2, my_callback_to_read_temperature)

我希望RPi上已经安装了gi.repository,但如果您需要安装说明,请访问:https://pygobject.readthedocs.io/en/latest/getting_started.html#ubuntu-getting-started

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65649203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档