Im trying to get IMEI of a Samsung S7 device. I tried with AT+CGSN
via Putty
which worked perfectly. When I tried the same with C# SerialPort
returns empty string.
Sending AT\r\n on C# SerialPort
giving "OK" as expected and all other AT commands are also working except this
It looks bit wired for me on why it was not working for the specific commands where others are working.
Here is the sample.
private static string GetMobileSerialNumber(string PortName)
{
string Serial = "";
SerialPort serialPort = new SerialPort();
serialPort.PortName = PortName;
serialPort.BaudRate = 154200;
serialPort.Handshake = Handshake.None;
serialPort.ReadBufferSize = 16384;
try
{
if (!(serialPort.IsOpen))
serialPort.Open();
serialPort.Write("AT+CGSN\r\n");
Thread.Sleep(1000);
Serial = serialPort.ReadExisting();
serialPort.Close();
Console.WriteLine(Serial);
return Serial;
}
catch (Exception ex)
{
//MessageBox.Show("Error in opening/writing to serial port :: " + ex.Message, "Error!");
return "";
}
}
Sample also available here