Click here to Skip to main content
13,597,589 members
Click here to Skip to main content
Add your own
alternative version

Stats

22.3K views
3.1K downloads
12 bookmarked
Posted 3 Jul 2015
Licenced GPL3

Detecting USB Modem Device using C#

, 3 Jul 2015
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Social Group (No members)



You may also be interested in...

Pro
Pro

Comments and Discussions

 
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
groupShivachalappa Gotur8-Jul-15 1:18 
GeneralRe: Links update Pin
Shivachalappa Gotur8-Jul-15 1:21
groupShivachalappa Gotur8-Jul-15 1:21 
GeneralMy vote of 4 Pin
Humayun Kabir Mamun5-Jul-15 23:11
memberHumayun Kabir Mamun5-Jul-15 23:11 
QuestionSource files cannot be downloaded Pin
fredatcodeproject5-Jul-15 1:04
memberfredatcodeproject5-Jul-15 1:04 
AnswerRe: Source files cannot be downloaded Pin
Shivachalappa Gotur8-Jul-15 1:17
groupShivachalappa Gotur8-Jul-15 1:17 
GeneralRe: Source files cannot be downloaded Pin
fredatcodeproject8-Jul-15 2:15
memberfredatcodeproject8-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
memberTahir 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180621.3 | Last Updated 3 Jul 2015
Article Copyright 2015 by Shivachalappa Gotur
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid