Device(u'/sys/devices/pci0000:00/pci0000:00:01.0/0000.000/usb1/1-2')是
USB设备 (即
device.device_type =='usb_device')。在进行枚举时,该
/dev/tty*文件尚不存在,因为稍后会在其自己的枚举期间将其分配给其 子 USB接口
。所以,你需要等待一个单独的 设备添加
事件为
Device(u'/sys/devices/pci0000:00/pci0000:00:01.0/0000.000/usb1/1-2:1.0')这将有
device.device_type== 'usb_interface'。
然后,你可以只是做
print [os.path.join('/dev', f) for f in os.listdir(device.sys_path)if f.startswith('tty')]它device_added():
import osimport glibimport pyudevimport pyudev.glibcontext = pyudev.Context()monitor = pyudev.Monitor.from_netlink(context)monitor.filter_by(subsystem='usb')observer = pyudev.glib.GUDevMonitorObserver(monitor)def device_added(observer, device): if device.device_type == "usb_interface": print device.sys_path, [os.path.join('/dev', f) for f in os.listdir(device.sys_path) if f.startswith('tty')]observer.connect('device-added', device_added)monitor.start()mainloop = glib.MainLoop()mainloop.run()


