简体   繁体   中英

C# IMEI detection of any device connected via USB

I am working on a C# desktop application to detect IMEI of a device that is connected via USB. I got it working with Samsung device after installing driver and running AT commands. Is there any generic way to this for all the devices.

Any device that supports at command could be access using the C# serial port class . Once we get hold of the device through serial port we can send at commands and response of those commands. This is how we can send the AT command for IMEI number and get the number as a response of at command through uniform interface for all type of devices those support AT commands and could be access through COM port.

I have found following example on codeproject to get the IMEI number and made little modification.

private string GetIMEINumber(string PortName) //Serial
{
    string key = "";
    SerialPort serialPort = new SerialPort();
    serialPort.PortName = PortName;
    serialPort.BaudRate = 9600;
    try
    {
        if (!(serialPort.IsOpen))
            serialPort.Open();
        serialPort.Write("AT\r\n");
        Thread.Sleep(3000);
        key = serialPort.ReadExisting();
        serialPort.Write("AT+CGSN\r\n");
        Thread.Sleep(3000);
        key = serialPort.ReadExisting();
        serialPort.Close();
        string Serial = "";
        for (int i = 0; i < key.Length; i++)
            if (char.IsDigit(key[i]))
                Serial += key[i];
        return Serial;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error in opening/writing to serial port :: " + ex.Message, "Error!");
        return "";
    } 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM