使用C#自动检测串口 [英] Auto detect serial port using C#
问题描述
我正在编写一个应用程序,通过usb_serial端口自动检测调制解调器插件到PC。
这是我的代码:
I am writing an application which automattically detects a modem plug to pc through usb_serial port.
Here is my code:
[StructLayout(LayoutKind.Sequential)]
public struct DEV_BROADCAST_PORT
{
public UInt32 dbcv_size;
public UInt32 dbcv_devicetype;
public UInt32 dbcv_reserved;
public char dbcp_name;
}
protected override void WndProc(ref Message m)
{
const int WM_DEVICECHANGE = 0x0219;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
const int DBT_DEVTYP_PORT = 0x00000003;
switch (m.Msg)
{
case WM_DEVICECHANGE:
{
switch (m.WParam.ToInt32())
{
case DBT_DEVICEARRIVAL:
{
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_PORT)
{
DEV_BROADCAST_PORT _FTDI;
_FTDI = (DEV_BROADCAST_PORT)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_PORT));
string port = Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12));
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
{
int devType = Marshal.ReadInt32(m.LParam, 4);
if (devType == DBT_DEVTYP_PORT)
{
DEV_BROADCAST_PORT _FTDI;
_FTDI = (DEV_BROADCAST_PORT)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_PORT));
string port = Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12));
}
}
break;
}
}
break;
default:
break;
}
base.WndProc(ref m);
}
当我将调制解调器插入电脑时,它会创建两个串口。我想找到这个配件可以发送命令到modem.Anybody帮助我???
谢谢!!!!!!!!
When i plug a modem to pc,it will create two serial port.And i wanna to find the comport which can send at command to modem.Anybody help me???
Thanks!!!!!!!!
推荐答案
你可以使用 SerialPort.GetPortNames [ ^ ]方法。
You may use SerialPort.GetPortNames[^] method.
以下是我遇到类似问题时解决的示例代码。
Here is sample code I solved when i faced a similar problem.
using System.Management; // include this namespace
ConnectionOptions options = GetConnectionOptions();
ManagementScope connectionScope = GetConnectionScope(Environment.MachineName, options, @"\root\CIMV2");
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);
using (comPortSearcher)
{
string caption = null;
foreach (ManagementObject obj in comPortSearcher.Get())
{
if (obj != null)
{
object captionObj = obj["Caption"];
if (captionObj != null)
{
caption = captionObj.ToString();
if (caption.Contains("(COM"))
{
COMPortInfo comPortInfo = new COMPortInfo();
comPortInfo.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")",
string.Empty);
comPortInfo.Description = caption;
comPortInfoList.Add(comPortInfo);
}
}
}
}
/EDIT
private static ManagementScope GetConnectionScope(string machineName, ConnectionOptions options, string path)
{
ManagementScope connectScope = new ManagementScope();
connectScope.Path = new ManagementPath(@"\\" + machineName + path);
connectScope.Options = options;
connectScope.Connect();
return connectScope;
}
private static ConnectionOptions GetConnectionOptions(
{
ConnectionOptions options = new ConnectionOptions();
options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = AuthenticationLevel.Default;
options.EnablePrivileges = true;
return options;
}
/EDIT
在我的应用程序中,我使用Description属性来识别我的设备。你可以忽略COMPortInfo我在我的应用程序中使用的本地类的类。留下代码只是为了更好的实现..
In my application i make use of the Description property to identify my device. You may ignore the COMPortInfo class which is local class i used in my application. left the code just for better implementations..
这篇关于使用C#自动检测串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!