枚举 window 系统串口的几种方法

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));

		}

	}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值