0

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

Gopichandar
  • 2,742
  • 2
  • 24
  • 54
  • 1
    Try only writing "AT\r\n" to the SerialPort first, maybe it needs this as some sort of primer – MindSwipe Feb 11 '19 at 14:24
  • If you are using same baud rate as putty, then the issue must be the PortName. Make sure the port name is exactly same as putty. – jdweng Feb 11 '19 at 14:27
  • @MindSwipe AT\r\n giving "OK" as expected and all other AT commands are also working except this. – Gopichandar Feb 11 '19 at 14:30
  • @jdweng Configuration is same as putty and commands like AT\r\n response "OK" as expected and all other AT commands are also working except this. – Gopichandar Feb 11 '19 at 14:30
  • I just can think of a timing issue. Maye it takes more than 1 Second to respond to `AT+CGSN`. You can try to increase the `Thread.Sleep` or do a loop until `OK` is part of the result. – G Wimpassinger Feb 11 '19 at 14:41
  • @GWimpassinger , I.m pretty much sure that this isn't timing issue as putty takes less than 500ms. anyway, I tried even 10 secs but response is empty.. – Gopichandar Feb 11 '19 at 14:44
  • 2
    AT commands only require the carriage return character ("\r") on sending, it is possible the linefeed ("\n") is aborting the reply. See if "AT+CGSN\r" works. – PaulF Feb 11 '19 at 15:08
  • 2
    PaulF is right in that `\r` is the only correct command line termination character. You will learn this by reading the V.250 specification. And by all means, **[you should never use `Thread.sleep` as a substitute for reading and parsing the responses](https://stackoverflow.com/a/46064206/23118)**. Please update your code accordingly and then see if you still have problems. – hlovdal Feb 11 '19 at 18:50
  • @PaulF , thanks for the sugesstion, I tried the same with "AT+CGSN\r" still response is same as empty string – Gopichandar Feb 12 '19 at 05:56
  • Possibly try writing each char at a time (like typing in a terminal program like Putty). Maybe sending it all as one string is throwing it off. – Baddack Feb 14 '19 at 00:42

1 Answers1

0

PuTTY and teraterm filtering /n /r similar parameters CR LF byte commands.

your cmd wrong