【问题标题】:the problem about detecting usb device关于检测usb设备的问题
【发布时间】:2011-09-27 18:07:42
【问题描述】:

我需要在插入和拔出USB设备时检测它,我用dbus编写了一个python程序。

但是很奇怪,设备在插入或拔出时至少会安装 3 次

监控代码如下:

            device = dbus.Interface(self.bus.get_object("org.freedesktop.Hal", udi),
                                    "org.freedesktop.Hal.Device")

            self.notify_message(device.GetProperty("info.udi"))

然后当我尝试插入 USB 设备(例如键盘)时,我们会捕获输出

Mon Jul  4 03:47:31 2011    /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial

Mon Jul  4 03:47:31 2011    /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial_if0

Mon Jul  4 03:47:31 2011    /org/freedesktop/Hal/devices/usb_device_413c_2003_noserial_if0_logicaldev_input

所以,每次插入或拔出时,通知都会显示 3 次 如何只显示一次通知?

【问题讨论】:

    标签: python usb dbus


    【解决方案1】:

    我不熟悉 dbus,但是看看你得到的设备名称:

    usb_device_413c_2003_noserial
    usb_device_413c_2003_noserial_if0
    usb_device_413c_2003_noserial_if0_logicaldev_input
    

    第一个设备可能代表整个 USB 设备。第二个设备最有可能代表该设备的接口 0。第三个设备可能代表一个端点或接口 0 的某些其他功能,这些功能可能会或可能不会在设备的描述符中指定。

    即使只插入一个物理设备,您也会得到三个不同的逻辑设备。这类事情对于实现复合 USB 设备的人来说很重要。

    不过,要回答这个问题:如果您只想收到一次通知,那么在您的通知处理程序函数中,您应该通过查看设备名称字符串并确定您是否关心来过滤掉您不关心的通知事件与否。例如,您可能决定不关心名称中带有 if0 的设备,因此您的伪代码将是:

    def notificationHandler(notification)
      if notification.name does not contain `if0`
        pass notification to higher level code
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多