得到一个USB适配器的COM端口号 [英] get com port number of an USB adapter
问题描述
我想写一个使用一个Arduino万和基于FTDI-USB转RS485适配器的程序。
I am trying to write a program that uses an arduino mega and a FTDI-based USB to RS485 adapter.
我想使程序界面友好,因此,我不惯于用户手动检查COM端口号,但我想自动检测到它。下面的代码
I want to make the program user-friendly, thus I don't wont the user to manually check the com port number, but I want to auto-detect it. Here's a snippet of the code
ManagementScope scope = new ManagementScope();
SelectQuery query = new SelectQuery("SELECT * FROM Win32_SerialPort");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
try
{
foreach (ManagementObject item in searcher.Get())
{
String description = item["Description"].ToString();
String deviceID = item["DeviceID"].ToString();
Console.WriteLine("Porta " + description + " deviceID " + deviceID);
if (description.Contains("USB Serial Port"))
return deviceID;
}
}
catch (ManagementException)
{
}
这是我无法理解的一点是,为什么我能找到的Arduino(匹配 description.Contains(Arduino的)
),但不是USB RS485接口的COM端口。
The point that I am not able to understand is why I can find the USB Serial port of the Arduino (matching description.Contains("Arduino")
) but not the com port of the USB RS485 port.
你有一个想法,为什么会发生这种情况?是查询 SELECT * FROM Win32_SerialPort
错了?
Do you have an idea why this could happen? is the query SELECT * FROM Win32_SerialPort
wrong?
推荐答案
好了,经过学习几个论坛,我发现,如果我运行查询关联到USB / RS485适配器的COM端口被列为 SELECT * FROM Win32_PnPEntity
。
Well, after studying several forums, I discovered that the com port associated to the USB/RS485 adapter is listed if I run the query SELECT * FROM Win32_PnPEntity
.
但我真的不明白,为什么Arduino的串口通过查询,其他端口与其他查询....我的意思是,那些显示都是USB<! - >串行适配器
But I really don't understand why the serial port of the arduino is shown by a query and the other port with the other query.... I mean, those are both uSB<->serial adapters!
这篇关于得到一个USB适配器的COM端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!