Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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);
        }
 
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 13-Dec-12 22:15pm
Edited 13-Dec-12 22:19pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may use SerialPort.GetPortNames[^] method.
  Permalink  
Comments
congtk88 at 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!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 
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..
  Permalink  
v2
Comments
congtk88 at 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 at 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)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100