Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Detecting USB Modem Device using C#

0.00/5 (No votes)
3 Jul 2015 1  
Detecting USB Modem Device using C#.NET

Introduction

Communicating with USB modem using C# is shown in this example. It detects and lists the ports at which USB modem is connected, communicates with USB Modem using AT Commands. retrieves the basic information from USB device. You can use the WMI Code Creator tool to generate WMI Query script in VBScript, C#, and VB.NET.

To download WMI, click here.

Screenshot of Example

Commands Used in Example

Here are the commands used in this example:

  1. AT+CGMM
    To Get the USB Model Identification
    Product Name: EC122
    Input: AT+CGMM
    Output: EC122
  2. AT+CGMR: To get Firmware version of the modem
  3. AT+CGSN: To get modem IMEI
  4. AT+CIMI: To get IMSI Number
  5. AT+CGMI: To get Manufacturer of USB Model
  6. AT: To get Modem's Attention

Code Sections

  1. Detecting and listing the port names to which USB Modem is connected. Here, we are using the WMI Query to get the names of port to which USB modem is connected.
        ManagementObjectSearcher searcher = new ManagementObjectSearcher
        ("root\\CIMV2", "SELECT * FROM Win32_PnpEntity ");
        ManagementObjectSearcher searcher1 = new ManagementObjectSearcher
        ("root\\CIMV2", "SELECT * FROM Win32_POTSModem "); 
  2. The section of code shown below accomplishes the task of opening the appropriate port, and communicating with it using the AT commands, arranging data elements in array, displaying it. Here, we are using the function WriteLine() to write at output Buffer and ReadExisting() to read the data from input Buffer. After successful reading of the data, we use Array to arrange the data elements.
        sp.PortName = (string)comboBox1.SelectedItem;
        if (!sp.IsOpen)
        {
            sp.Open();
            
            if (sp.IsOpen)
            {
                MessageBox.Show("Connected to Port" + portname);
                
                sp.WriteLine("AT");           //Get the modem's attention
                sp.WriteLine("ATI");          // Get All Manufacturer Info
                sp.WriteLine("AT+CGMM");      // Get USB Model
                sp.WriteLine("AT+CGMI");      // Manufacturer
                sp.WriteLine("AT+CIMI");      // Get SIM IMSI number
                sp.WriteLine("AT+CGSN");      //Get modem IMEI
                sp.WriteLine("AT+CGMR");      // Print firmware version of the modem                   
            }
        }
        
        t = sp.ReadExisting()).Contains("OK")
        
        string[] f = new string[100];
        string[] c = t.Split(z);
        int m = 0;
        
        for(int i = 0; i < c.Length; i++)
            {
                if (!(c[i].Equals("")))
                    {
                        f[m] = c[i];
                        m++;
                    }
            }
            
            // Display the Data
            
            if ((f[i].Equals("AT+CGMI")))
                {
                    label6.Text = f[i + 1]; // Manufacturer
                }
                if ((f[i].Equals("AT+CIMI")))
                {
                    label7.Text = f[i + 1]; // Get SIM IMSI number
                }
                if ((f[i].Equals("AT+CGSN")))
                {
                    label8.Text = f[i + 1]; //Get modem IMEI
                }
                if ((f[i].Equals("AT+CGMM")))
                {
                    label11.Text = f[i + 1]; // Get Model of USB 3G
                }
                if ((f[i].Equals("AT+CGMR")))
                {
                    label12.Text = f[i + 1]; // Print firmware version of the modem
                }

How It Works

Visual Studio 2008 is used for developing this code.

  1. On clicking "Find Port" button, it lists names of port to which USB modem is connected.
  2. Select the appropriate port from dropdownlist.
  3. Then click on "Connect and Get Modem Info" button to get the information from modem.
  4. It retrieves the information such as Manufacturer, IMSI Number, IMEI Number, Model of USB Device, Port to which modem is connected, Firmware version. 
  5. On clicking "Disconnect" button, it disconnects the modem from the port.
  6. Clicking on "Exit" button, it exits the application.

This code works Fine on EC122 CDMA USB Modem.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here