简体   繁体   English

C#IMEI检测通过USB连接的任何设备

[英]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. 我正在使用C#桌面应用程序来检测通过USB连接的设备的IMEI。 I got it working with Samsung device after installing driver and running AT commands. 安装驱动程序并运行AT命令后,我将其与Samsung设备一起使用。 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 . 任何支持at命令的设备都可以使用C# 串行端口类进行访问。 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. 这样,我们就可以通过统一接口针对支持AT命令并且可以通过COM端口访问的所有类型的设备,发送用于IMEI编号的AT命令,并通过at接口获得该编号作为at命令的响应。

I have found following example on codeproject to get the IMEI number and made little modification. 在codeproject上找到了以下示例来获取IMEI编号,并进行了少量修改。

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 "";
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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