Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing an application which automattically detects a modem plug to pc through usb_serial port.
Here is my code:
C#
[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);
        }


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!!!!!!!!
Posted
Updated 13-Dec-12 22:19pm
v2

You may use SerialPort.GetPortNames[^] method.
 
Share this answer
 
Comments
congtk88 14-Dec-12 10:22am    
when you use SerialPort.GetPortNames, it will return all comport on you PC. Problem is here that find the comport which i can send at command to modem.thanks!
Here is sample code I solved when i faced a similar problem.

C#
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


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..
 
Share this answer
 
v2
Comments
congtk88 15-Dec-12 2:45am    
Sorry but what is the "GetConnectionOptions" and the "COMPortInfo". i use the visual studio 2008 and my complier cannot find them.Thanks!!!
Jibesh 15-Dec-12 14:33pm    
hummm...about COMPortInfo its a local class i used you can skip these, I already mentioned about this in my solution. Check updated solution for the missing method..


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900