本文介绍了Pydbus更多通知同时处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过pydbus通过通知从BLE设备接收传感器数据。
我在主循环中使用GLib
链接了我的部分代码:

  def sensor1_handler(iface,prop_changed,prop_removed ):
,如果prop_chang中的值已更改:

处理值。

def sensor2_handler(iface,prop_changed,prop_removed):
(如果prop_changed中为 Value):
处理值

sensor1 = bus.get( org.bluez, / org / bluez / hciX / dev_XX_XX_XX_XX_XX_XX / serviceYYYY / charYYYY)
sensor2 = bus.get( org .bluez, / org / bluez / hciX / dev_XX_XX_XX_XX_XX_XX / serviceYYYY / charYYYY))
sensor1.onPropertiesChanged = sensor1_handler
sensor2.onPropertiesChanged = sensor2_handler

sensor2.StartNotify )
sensor1.StartNotify()

当我想接收通知时,两个信号几乎在同一时间到达,只有一个(第一个到达的通知)通知回调函数运行。


如何解决此问题?我在考虑消息队列。


编辑:


sensor1函数:

  def sensor1_handler(iface,prop_changed,prop_removed):
如果prop_changed中的'Value':
temperatureLSB = prop_changed ['Value'] [1]
temperatureMSB = prop_changed ['Value'] [0]
humidityLSB = prop_changed ['Value'] [3]
humidityMSB = prop_changed ['Value'] [2]
temperature = temperatureLSB | (温度MSB≤8)
湿度=湿度LSB | (湿度MSB< 打印(-45 + 175 *(温度/(pow(2,16)-1)))
打印(100 *(湿度/(pow(2, 16)-1)))

sensor2函数:

  def sensor2_handler(iface,prop_changed,prop_removed):
如果prop_changed中的'Value':
iaqLSB = prop_changed ['Value'] [1]
iaqMSB = prop_changed ['值'] [0]
iaq = iaqLSB | (iaqMSB< print(iaq)

在sensor1之前发送sensor2数据,因此sensor2数据到达第一。这两个数据来自同一设备,我还没有添加其他设备。

解决方案

感谢您的帮助! / p>

经过多次尝试,我注意到问题出在服务器端。
这两个传感器数据是从一台设备发送的,相差约10毫秒,并且消息发送不是在通知模式下,而是指示。


我之前没有注意到,因为我已经仅发送了1个传感器数据,相差2秒,但是当我尝试几乎同时发送2条消息时出现了问题。


将其设置为Notification之后,服务器可以快速发送消息(不需要


我知道这个错误最终不是由pydbus引起的,而是我的错。我希望如果有人发现类似的问题,他们也应该检查发送方(服务器)。


I would like to receive sensor data from BLE devices by notifications using pydbus.I am using GLib with main loopLinked a part of my code:

def sensor1_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:

        """Handle values"""

def sensor2_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
       """Handle values"""

sensor1=bus.get("org.bluez", "/org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX/serviceYYYY/charYYYY")
sensor2=bus.get("org.bluez", "/org/bluez/hciX/dev_XX_XX_XX_XX_XX_XX/serviceYYYY/charYYYY")
sensor1.onPropertiesChanged = sensor1_handler
sensor2.onPropertiesChanged = sensor2_handler

sensor2.StartNotify()
sensor1.StartNotify()

When I would like to receive the notifications, the two signals arrive almost the same time and only one (the first arrived notification) Notification Callback function runs.

How could I solve this problem? I was thinking on a message queue.

EDIT:

sensor1 function:

def sensor1_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
        temperatureLSB = prop_changed['Value'][1]
        temperatureMSB = prop_changed['Value'][0]
        humidityLSB = prop_changed['Value'][3]
        humidityMSB = prop_changed['Value'][2]
        temperature = temperatureLSB | (temperatureMSB << 8)
        humidity = humidityLSB | (humidityMSB << 8)
        print(-45+175*(temperature/(pow(2,16)-1)))
        print(100*(humidity/(pow(2,16)-1)))

sensor2 function:

def sensor2_handler(iface, prop_changed, prop_removed):
    if 'Value' in prop_changed:
        iaqLSB = prop_changed['Value'][1]
        iaqMSB = prop_changed['Value'][0]
        iaq = iaqLSB | (iaqMSB << 8)
        print(iaq)

sensor2 data sent before sensor1 so sensor2 data arrives first. The two data arrives from the same device, I did not add the other devices yet.

解决方案

Thank you for every help!

After many attemptions, I've noticed the problem was on my server side.The two sensor data was sent from one device with ~10ms difference and the message sending was not in Notification mode but Indication.

I have not noticed it before, because I have sent only 1 sensor data with 2 seconds difference but problem came when I was trying to send 2 messages almost simultaneously.

After setting it to Notification, the server could send messages rapidly (did not need acknoledgement).

I know this error was not caused by pydbus in the end, but by my fault. I hope that if someone finds a similar problem, they should check the sender (server) side as well.

这篇关于Pydbus更多通知同时处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 09:55