【发布时间】:2020-02-01 06:26:04
【问题描述】:
我想创建一个脚本,在 USB 驱动器插入计算机后进行检测,现在只是在 cmd detect 中打印。
注意我在使用windows搜索后发现我需要使用pyudev包才能与串口通信,并且我需要知道USB设备的供应商ID。
我尝试编写以下代码:
import pyudev
context = pyudev.Context()
monitor = Monitor.from_netlink()
# For USB devices
monitor.filter_by(susbsytem='usb')
# OR specifically for most USB serial devices
monitor.filter_by(susbystem='tty')
for action, device in monitor:
vendor_id = device.get('ID_VENDOR_ID')
if vendor_id in ['USB\\VID_0930&PID_6544&REV_0100'] or vendor_id in ['USB\\VID_0930&PID_6544']:
print ('Detected {0} for device with vendor ID {1}'.format(action, vendor_id))
但系统崩溃并显示此错误:
import fcntl ModuleNotFoundError: No module named 'fcntl'
我认为 fcntl 仅适用于 Ubuntu,因为我尝试安装该软件包但它不存在。
【问题讨论】:
-
if vendor_id in ['USB\\VID_0930&PID_6544&REV_0100'] or vendor_id in ['USB\\VID_0930&PID_6544']:==if vendor_id in ['USB\\VID_0930&PID_6544&REV_0100', 'USB\\VID_0930&PID_6544']: