Click here to Skip to main content
15,887,361 members
Articles / Programming Languages / C#
Tip/Trick

Detecting USB Modem Device using C#

Rate me:
Please Sign up or sign in to vote.
4.17/5 (9 votes)
3 Jul 2015GPL32 min read 46.7K   5.8K   12   10
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.
    C#
    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.
    C#
    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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
India India
Designation:- Senior Systems Analyst
Pune.
This is a Social Group (No members)


Comments and Discussions

 
Questionyou are good Pin
saqi31314-Aug-18 3:42
saqi31314-Aug-18 3:42 
GeneralLinks update Pin
Mostafa A. Ali7-Jul-15 20:08
professionalMostafa A. Ali7-Jul-15 20:08 
GeneralRe: Links update Pin
Shivachalappa Gotur8-Jul-15 1:18
Shivachalappa Gotur8-Jul-15 1:18 
GeneralRe: Links update Pin
Shivachalappa Gotur8-Jul-15 1:21
Shivachalappa Gotur8-Jul-15 1:21 
GeneralMy vote of 4 Pin
Humayun Kabir Mamun5-Jul-15 23:11
Humayun Kabir Mamun5-Jul-15 23:11 
QuestionSource files cannot be downloaded Pin
fredatcodeproject5-Jul-15 1:04
professionalfredatcodeproject5-Jul-15 1:04 
AnswerRe: Source files cannot be downloaded Pin
Shivachalappa Gotur8-Jul-15 1:17
Shivachalappa Gotur8-Jul-15 1:17 
GeneralRe: Source files cannot be downloaded Pin
fredatcodeproject8-Jul-15 2:15
professionalfredatcodeproject8-Jul-15 2:15 
QuestionGood Pin
Santhakumar Munuswamy @ Chennai4-Jul-15 20:40
professionalSanthakumar Munuswamy @ Chennai4-Jul-15 20:40 
GeneralReally Helpful article, I implement this myself, result 100% Pin
Tahir Alvi4-Jul-15 11:17
Tahir Alvi4-Jul-15 11:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.