我试图找出哪个串行端口属于Arduino
.
由于该SerialPort
课程没有透露有关
底层硬件的任何信息,因此我尝试改用LibUsbDotNet。
使用以下代码获取所有设备的列表:
UsbDevice.ForceLibUsbWinBack = true;
var devices = UsbDevice.AllDevices;
然后我可以遍历所有这些或搜索Arduino
具体:
var deviceFinder = new UsbDeviceFinder(0x2341, 0x8036);
var arduino = devices.Find(deviceFinder);
这实际上有效,我得到了一个实例UsbRegistry
,
但我无法打开设备或确定它是通过哪个串行端口暴露的。
USBDevice arduinoUsbDevice;
usbRegistry.Open(out arduinoUsbDevice);
由于这不起作用arduinoUsbDevice
仍然存在null
。
然后,我尝试使用在
系统中添加或删除设备DeviceNotifier
时引发事件的类:
var notifier = DeviceNotifier.OpenDeviceNotifier();
notifier.OnDeviceNotify += (s, e) =>
{
WriteLine(e.Device?.Name ?? "no device");
WriteLine(e.Device?.IdProduct ?? 0);
WriteLine(e.Device?.IdVendor ?? 0);
WriteLine(e.EventType);
WriteLine(e.Object);
WriteLine(e.Port?.Name ?? "");
};
现在,每当我连接Arduino
到计算机时,都会引发两次事件。
好像正在连接两个单独的设备,
但其中只有一个被返回UsbDevice.AllDevices
:
` \\?\USB#VID_2341&PID_8036#7&533912d&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
32822
9025
DeviceArrival
FullName:USB#VID_2341&PID_8036#7&533912d&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
Vid:0x2341
Pid:0x8036
SerialNumber:7&533912d&0&2
ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed
no device
0
0
DeviceArrival
[Port Name:COM5]
COM5
第一次为我们之前可以找到的设备引发事件。
第二次用e.Device
set tonull
但
用e.Port
set to 提出COM5
,这是我所追求的信息。
所以问题是我只能在软件启动后连接时才能获得此信息Arduino
,
即使连接这两个事件也是
一种猜谜游戏。
有什么方法可以在不依赖班级
引发的事件的情况下获取信息DeviceNotifier
?
我知道我可以使用System.Management
和WMI
查询,但这些在 Linux 和 MacOS 上不可用,这就是我
LibUsbDotNet
改用的原因。
我使用的本机库是libusb-1.0