简体   繁体   English

关于检测usb设备的问题

[英]the problem about detecting usb device

I need detect an usb device when it had been plugged and unplugged, and I write a python programme with dbus.我需要在插入和拔出 usb 设备时检测它,并使用 dbus 编写 python 程序。

But it is very odd the device would be mounted three times at least when it is plugged or unplugged但是很奇怪,设备在插入或拔出时至少会安装三次

the monitor code is following:监控代码如下:

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

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

then we catch the output when I try to insert an usb device(eg. a keyboard)然后当我尝试插入 usb 设备(例如键盘)时,我们会发现 output

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

so, the notify shows three times everytime it was plugged or unplugged how to show only once notification?那么,每次插入或拔出通知时都会显示三次通知如何只显示一次通知?

I am not familiar with dbus, but look at the device names you are getting:我不熟悉 dbus,但看看你得到的设备名称:

usb_device_413c_2003_noserial
usb_device_413c_2003_noserial_if0
usb_device_413c_2003_noserial_if0_logicaldev_input

The first device probably represents the USB device as a whole.第一个设备可能代表了整个 USB 设备。 The second device most likely represents Interface 0 of said device.第二个设备最有可能代表该设备的接口 0。 The third device probably represents an endpoint or some other feature of Interface 0 which may or may not be specified in the device's descriptors.第三个设备可能代表一个端点或接口 0 的某些其他功能,这些功能可能会或可能不会在设备的描述符中指定。

You get three different logical devices even though only plugged in one physical device.即使只插入一个物理设备,您也会得到三个不同的逻辑设备。 This sort of thing is important for people who implement composite USB devices.这种事情对于实现复合 USB 设备的人来说很重要。

To answer the question, though: If you only want to be notified once, then in your notification handler function you should filter out the notifications you don't care about by looking at the device name string and deciding whether you care about the event or not.但是,要回答这个问题:如果您只想收到一次通知,那么在您的通知处理程序 function 中,您应该通过查看设备名称字符串并决定您是否关心事件或关心事件来过滤掉您不关心的通知。不是。 For example, you might decide that you don't care about devices with if0 in the name so your pseudo code would be:例如,您可能决定不关心名称中带有if0的设备,因此您的伪代码将是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM