mfc 下:
CString s; for (int i=1; i<256; i++) { s.Format("COM%d", i); if (CSerialPort::CheckCommPort(s)) { m_ListComm.AddString(s); } }
bool CSerialPort::CheckCommPort(LPCSTR commPortName) { COMMCONFIG cc; DWORD dwCCSize = sizeof(cc); return ::GetDefaultCommConfig(commPortName, &cc, &dwCCSize) == TRUE; }
C# 下可以直接使用
string[] ports = System.IO.Ports.SerialPort.GetPortNames();
但如果想要更高级的做法,如辨别由 usb, bluetooth 等虚拟出的串口,则
using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
namespace Utility
{
public class CSystemDeviceInfo
{
protected const int DIGCF_PRESENT = (0x00000002);
protected const int MAX_DEV_LEN = 1000;
protected const int SPDRP_FRIENDLYNAME = (0x0000000C);
protected const int SPDRP_DEVICEDESC = (0x00000000);
[StructLayout(LayoutKind.Sequential)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public int DevInst;
public ulong Reserved;
};
[DllImport("setupapi.dll")]//
public static extern Boolean
SetupDiClassGuidsFromNameA(string ClassN, ref Guid guids,
UInt32 ClassNameSize, ref UInt32 ReqSize);
[DllImport("setupapi.dll")]
public static extern IntPtr //result HDEVINFO
SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator,
IntPtr hwndParent, UInt32 Flags);
[DllImport("setupapi.dll")]
public static extern Boolean
SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex,
SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll")]
public static extern Boolean
SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);
[DllImport("setupapi.dll")]
public static extern Boolean
SetupDiGetDeviceRegistryPropertyA(IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData, UInt32 Property,
UInt32 PropertyRegDataType, StringBuilder PropertyBuffer,
UInt32 PropertyBufferSize, IntPtr RequiredSize);
public static string[] EnumerateDevices(string ClassName)
{
ArrayList devices = new ArrayList(8);
StringBuilder DeviceName = new StringBuilder("");
UInt32 RequiredSize = 0;
Guid guid = Guid.Empty;
Guid[] guids = new Guid[1];
IntPtr NewDeviceInfoSet;
SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA();
bool res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);
if (RequiredSize == 0) {
return (string[]) devices.ToArray(typeof(string));
}
if (!res) {
guids = new Guid[RequiredSize];
res = SetupDiClassGuidsFromNameA(ClassName, ref guids[0], RequiredSize, ref RequiredSize);
if (!res || RequiredSize == 0) {
return (string[]) devices.ToArray(typeof(string));
}
}
UInt32 DeviceIndex = 0;
for (int i = 0; i < RequiredSize; i++) {
NewDeviceInfoSet = SetupDiGetClassDevsA(ref guids[i], 0, IntPtr.Zero, DIGCF_PRESENT);
if (NewDeviceInfoSet.ToInt32() == -1) {
if (!res) {
return (string[]) devices.ToArray(typeof(string));
}
}
while (true) {
DeviceInfoData.cbSize = 28;
DeviceInfoData.DevInst = 0;
DeviceInfoData.ClassGuid = System.Guid.Empty;
DeviceInfoData.Reserved = 0;
res = SetupDiEnumDeviceInfo(NewDeviceInfoSet, DeviceIndex, DeviceInfoData);
if (!res) {
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
break;
}
DeviceName.Capacity = MAX_DEV_LEN;
if (!SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet, DeviceInfoData,
SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero)) {
res = SetupDiGetDeviceRegistryPropertyA(NewDeviceInfoSet,
DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN,
IntPtr.Zero);
if (!res) {
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
return (string[]) devices.ToArray(typeof(string));
}
}
devices.Add(DeviceName.ToString());
DeviceIndex++;
}
}
return (string[]) devices.ToArray(typeof(string));
}
}
}