[英]Threading Bluetooth communication Raspberry pi (Python 3)
我的问题是,我如何在我的程序中实现线程,在那里我与 Rpi3 进行通信 BLE。
我的程序运行良好,但响应太慢。 请帮助解决这个问题。 谢谢。
BMS_读数:
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%
我不知道我该怎么做。 当我在循环中创建线程时,通信没有时间做出反应并打印错误的数字或错误。
请帮忙。
--------------------已编辑--程序--用于--通知------更新----------
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()
终端打印,即使值更改并且管理器正在运行:
>>>BMS found
>>>BMS request generic data
>>>Temperature is: 19 C
#there program get stuck even if value is changing
谢谢,我编辑了带有通知的程序,如您所见,它支持它。
但是我在这里有一个问题,即使值(温度)发生变化并且 manager.run()中的管理器,即使我加热设备,终端也只会向我发送一个值并且什么都不做。 当我重新启动程序时,值再次改变,只剩下一个。 请问我的代码写对了吗?
先生,非常感谢您的时间。
我的假设是您正在使用gatt-python库。
行manager.run()
正在启动事件循环,因此您不需要在代码中使用 while 循环。
如果温度特性支持通知,那么打开它们将是在值发生变化时读取值的最有效方式。
如果设备没有通知,则建议创建一个定时事件以您需要的频率读取温度。 timeout_add_seconds的文档并不总是最容易理解的,但重要的是:
from gi.repository import GLib
然后在运行事件循环调用之前:
GLib.timeout_add_seconds(2, my_callback_to_read_temperature)
我希望 gi.repository 已经安装在 RPi 上,但是如果您需要安装说明,那么它们位于: https://pygobject.readthedocs.io/en/latest/getting_started.html#ubuntu-getting-started
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.