有几种方法可以列出Windows下的串行端口,但我不确定哪种方法是正确的:检测所有可用的串行端口的方法。
一个很好的代码示例是http://www.naughter.com/enumser.html -其中有9(9!)枚举串行设备的方法。
问题是:做这件事的最佳方式是什么。
要求:
要检查端口是否可用,请不要打开端口。要检测与COMx
.
发布于 2013-09-09 13:22:06
void SelectComPort() //added function to find the present serial
{
TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
DWORD test;
bool gotPort=0; // in case the port is not found
for(int i=0; i<255; i++) // checking ports from COM0 to COM255
{
CString str;
str.Format(_T("%d"),i);
CString ComName=CString("COM") + CString(str); // converting to COM0, COM1, COM2
test = QueryDosDevice(ComName, (LPSTR)lpTargetPath, 5000);
// Test the return value and error if any
if(test!=0) //QueryDosDevice returns zero if it didn't find an object
{
m_MyPort.AddString((CString)ComName); // add to the ComboBox
gotPort=1; // found port
}
if(::GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
continue;
}
}
if(!gotPort) // if not port
m_MyPort.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found
}
发布于 2015-02-10 20:16:37
如果您可以访问注册表,则Windows键包含HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
当前支持的COM端口列表(在某些情况下,此信息可能已过时/不正确;我怀疑提供串行端口的即插即用设备未完成检测/安装或最近已被移除时)。
这是.NET框架的SerialPort.GetPortNames()
方法报告可用COM端口的方式,上述信息派生自链接的页面。
发布于 2010-04-20 20:01:48
串行端口是非常简单的设备,可以追溯到计算硬件的石器时代。它们不支持即插即用,因此无法判断是否有人插入了设备。您唯一能做的就是发现哪些端口可用,SerialPort.GetPortNames()返回列表。一些USB模拟器可以生成一个描述性的名称来配合端口名称,你可以发现那些带有WMI,Win32_SerialPort类的。
这些都不能帮助您发现连接到特定设备的COM端口。只有人类知道,她把电缆插在了连接器上。您需要提供一个配置UI,让用户选择端口号。一个组合框可以完成这项工作。将选择保存在配置数据中,则下次程序启动时,设备很可能仍连接到相同的端口。
https://stackoverflow.com/questions/2674048
复制相似问题