为什么我的错误没有被except捕捉到?

2024-10-02 00:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我一整天都在努力解决这个问题,做了大量的研究,尝试了很多不同的方法,但都不管用。我现在有一个使用bluepy连接到bluetooth BLE设备的代码,当它连接时工作正常,但有时它不连接,也不会给出错误,只是在[00:00:00:00]连接线路上被吸住,并且不会在“除外”中显示。当我发送一个新命令时,有时是有效的,但是因为我没有得到错误,也没有任何东西,我永远不确定什么时候我需要发送一个新命令或没有。你知道吗

代码如下:

#!/usr/bin/env python
from bluepy import *
import paho.mqtt.client as mqtt
import struct

### Variables

mqtt_client = ""
mqtt_port = 
mqtt_user = ""
mqtt_password = ""
mqtt_path = ""

####  Don't edit below here

open = "\x00\xff\x00\x00\x9a\x0d\x01\x00\x96"
close = "\x00\xff\x00\x00\x9a\x0d\x01\x64\xf2"

def shade_command(fble, fcmd):
      try:
        print "["+ fble + "] Connecting"
        dev = btle.Peripheral(fble)
        print "["+ fble + "] Connected!"
        chs = dev.getCharacteristics()
        for ch in chs:
          if ch.uuid == "0000fe51-0000-1000-8000-00805f9b34fb":
            ch.write(fcmd)
        dev.disconnect
        print  "["+ fble + "] Disconnected"
      except:
        print "["+ fble + "] Error Connecting"

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected to MQTT with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe(mqtt_path + "/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    if msg.payload == "open":
        address = msg.topic.replace(mqtt_path + "/", "")
        result = shade_command(address, open)
        if result == True:
          print msg.topic + "/status"
          client.publish(msg.topic + "/status", "on", qos=0, retain=False)
    if msg.payload == "close":
        address = msg.topic.replace(mqtt_path + "/", "")
        result = shade_command(address, close)
        if result == True:
          client.publish(msg.topic + "/status", "off", qos=0, retain=False)
          print msg.topic + "/status"

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(mqtt_user, password=mqtt_password)
client.connect(mqtt_client, mqtt_port, 60)

问题发生在这里:

def shade_command(fble, fcmd):
      try:
        print "["+ fble + "] Connecting"
        dev = btle.Peripheral(fble)
        print "["+ fble + "] Connected!"
        chs = dev.getCharacteristics()
        for ch in chs:
          if ch.uuid == "0000fe51-0000-1000-8000-00805f9b34fb":
            ch.write(fcmd)
        dev.disconnect
        print  "["+ fble + "] Disconnected"
      except:
        print "["+ fble + "] Error Connecting"

当我尝试连接到一个MAC地址,但无法工作时,我没有收到错误,只需在完成以下代码部分后停止:

        print "["+ fble + "] Connecting"
        dev = btle.Peripheral(fble)

我看到的最后一件事是[00:00:00:00]连接而不是其他!我本来想看看密码的。你知道吗

下面是一个输出示例,当我发送一个MQTT命令时,它首先尝试连接,但是什么都没有发生,它只是停止,而我发送了另一个命令,它工作了,我知道它在显示“Connecting”、“Connected”时工作了和“断开连接”

[02:B3:CD:3D:C5:34] Connecting
[02:B3:CD:3D:C5:34] Connecting
[02:B3:CD:3D:C5:34] Connected!
[02:B3:CD:3D:C5:34] Disconnected

编辑1:

尝试使用以下代码,但无效:

except btle.BTLEException as e:

编辑2:

多亏了@hardillb,现在我知道paho mqtt是一个捕捉错误并忽略它们的人,我添加了这段代码,现在我可以看到错误何时发生:

def on_log(client, userdata, level, buff):  # mqtt logs function
    print buff

client.on_log = on_log

当我出错时,它显示如下:

Caught exception in on_message: global name 'traceback' is not defined

Tags: 代码devclienttopicifondef错误
1条回答
网友
1楼 · 发布于 2024-10-02 00:35:29

感谢hardillb看起来像是有尝试/除了功能外做的把戏!你知道吗

新代码如下:

功能:

def shade_command(fble, fcmd):
    print "["+ fble + "] Connecting"
    dev = btle.Peripheral(fble)
    print "["+ fble + "] Connected!"
    chs = dev.getCharacteristics()
    for ch in chs:
      if ch.uuid == "0000fe51-0000-1000-8000-00805f9b34fb":
        ch.write(fcmd)
    dev.disconnect
    print  "["+ fble + "] Disconnected"

调用函数:

def on_message(client, userdata, msg):
    if msg.payload == "open":
        try:
          address = msg.topic.replace(mqtt_path + "/", "")
          result = shade_command(address, open)
          print msg.topic + "/status"
          client.publish(msg.topic + "/status", "on", qos=0, retain=False)
        except:
          print "Error"

相关问题 更多 >

    热门问题