Search code examples
c#at-command

AT+CMGL="ALL" return OK only


I have 1 gsm 32 port with name is xr21v1414 USB UART

I have tested another AT COMMAND which works fine like

AT USSD

but if i use COMMAND :

  serialPort.WriteLine("AT" + System.Environment.NewLine);
  Thread.Sleep(200);
  serialPort.WriteLine("AT+CMGF=1" + System.Environment.NewLine);
  Thread.Sleep(200);
  serialPort.WriteLine("AT+CMGL=\"ALL\"" + System.Environment.NewLine);
  Thread.Sleep(200);

responded with

serialPort.ReadExisting();

is empty (OK only)

AT

OK
AT+CMGF=1

OK
AT+CMGL="ALL"
OK

But if I switch to another gsm, Command work fine. I have list SMS.

This is my setting :

            SerialPort serialPort = new SerialPort();
            serialPort.PortName = m.ToString();
            serialPort.BaudRate = 115200;
            serialPort.DataBits = 8;
            serialPort.Handshake = Handshake.XOnXOff;
            serialPort.StopBits = StopBits.One;
            serialPort.Parity = Parity.None;
            serialPort.ReadTimeout = 20000;
            serialPort.WriteTimeout = 20000;
            serialPort.WriteBufferSize = 1024;
            serialPort.DtrEnable = true;
            serialPort.RtsEnable = true;

How could i resolve the problem ,please ?


Solution

  • I am quite sure that the OK response you get is because you have aborted the current command line by using Thread.Sleep(200) instead of reading and parsing the response you get back from the modem, waiting for the Final Result Code before sending the next command.

    The first linked answer above is to a question whose code which is slightly worse because it does not even have a sleep, but even having one is utterly wrong. Just as you would not write a HTTP client that ignores the responses a HTTP server send it, you should not write a AT command program that ignores the responses the modems sends it.

    TL;DR You MUST change your code to read and parse every single response line that the modem sends you, waiting for the Final Result Code before sending the next command. See the linked answers for more details.