关于检测usb设备的问题
我需要在插入和拔出USB设备时检测它,并且我用dbus编写了一个python程序。
但很奇怪的是,设备在插入或拔出时至少会被安装三次,
监视器代码如下:
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
因此,通知显示每次插入或拔出插头三次 如何只显示一次通知?
I need detect an usb device when it had been plugged and unplugged, and I write a python programme with dbus.
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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 dbus,但是看看您获得的设备名称:
第一个设备可能代表整个 USB 设备。第二个设备很可能代表所述设备的接口 0。第三个设备可能代表接口 0 的端点或某些其他功能,这些功能可能会也可能不会在设备描述符中指定。
即使只插入一个物理设备,您也会获得三个不同的逻辑设备。这类事情对于实现复合 USB 设备的人来说很重要。
不过,要回答这个问题:如果您只想收到一次通知,那么在您的通知处理程序函数中,您应该通过查看设备名称字符串并决定您是否关心该事件或来过滤掉您不关心的通知不是。例如,您可能决定不关心名称中带有
if0
的设备,因此您的伪代码将是:I am not familiar with dbus, but look at the device names you are getting:
The first device probably represents the USB device as a whole. The second device most likely represents Interface 0 of said device. 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.
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.
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. For example, you might decide that you don't care about devices with
if0
in the name so your pseudo code would be: