枚举 window 系统串口的几种方法
2008-07-01 23:47
323 查看
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 等虚拟出的串口,则
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)); } } }
相关文章推荐
- 发个枚举系统串口的方法(XP已经过实验),环境为VS2008
- Windows枚举出系统中所有的串口的9种方法
- ok6410开发板的andriod系统下装apk的几种方法整理
- Mac 10.9.1+Window 8双系统安装方法(13.3英寸Macbook pro Retina屏幕)
- window系统下 python3.5安装lxml-3.5.0 方法
- Mac OS系统下配置hosts的几种方法(以前都不是自己配的嘛,我也是被逼的)0.0
- 获取系统时间的几种方法,最高精确1ms
- window10系统电脑磁盘拒绝访问 window0系统电脑磁盘打不开的解决方法
- Python执行系统命令并获得输出的几种方法
- 四种方法实现VC枚举系统当前进程
- Linux系统设置编译器路径的几种方法介绍
- Linux系统下串口接收数据,部分特殊字符丢失的解决方法 .
- 强制重启Linux系统的几种方法
- 进入有密码的XP系统的几种方法
- window 7系统中提示“您的计算机配置似乎正确的”的DNS故障问题解决方法
- java 几种常用的枚举(Enum)方法
- Java获取系统时间的几种方法
- 介绍Ubuntu系统上轻松截图的几种方法
- 08-01-29>pe_xscan 增加了一种枚举系统服务的方法
- android中通知系统扫描系统媒体文件的几种方法