-1

I'm experiencing difficulty using AT commands. I've a GSM Modem connected as Com port with my pc. My code works fine when sending sms but the sent message is not in correct format. For example I sent 'Hello World' but the sent message shows '??' in cellphone. Plz guide me where I'm wrong. Here is my code.

try
        {

            string recievedData = ExecCommand(port, "AT", 300, "No phone connected");

            recievedData = ExecCommand(port, "AT+CMGF?", 300, "Failed to accept phoneNo");
            recievedData = ExecCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");

            string command = "AT+CMGS=\"" + PhoneNo + "\"";
            recievedData = ExecCommand(port, command, 300, "Failed to accept phoneNo");

            command = "hello world!" + "\x1A";
            recievedData = ExecCommand(port, command, 3000, "Failed to send message"); //3 seconds

            if (recievedData.EndsWith("\r\nOK\r\n"))
            {
                isSend = true;
            }
            else if (recievedData.Contains("ERROR"))
            {
                isSend = false;
            }
            return isSend;
        }
        catch (Exception ex)
        {
            throw ex; 
        }

Here is Execute Method

public string ExecCommand(SerialPort port,string command, int responseTimeout, string errorMessage)
    {
        try
        {

            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            receiveNow.Reset();
            port.Write(command + "\r");

            string input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
                throw new ApplicationException("No success message was received.");
            return input;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
  • 1
    Have you tried sending SMS using a program like putty, socat, zoc, hyperterminal or one of those that can open a serial socket? Just to separate possible errors in coding with possible errors in AT commands – dubafek Mar 02 '16 at 03:40
  • Thanx for replying. I've tested it with the software came with the usb gsm modem device. It is sending message properly. – Syed Ahmer Mar 02 '16 at 17:48
  • I've tested it with putty using AT commands but sending msg with body ?? only. same issue – Syed Ahmer Mar 02 '16 at 18:15

1 Answers1

1

I've solved my problem. Actually my problem was that my modem Character set was set to "IRA" International Reference Alphabet. I changed it to GSM by the following command

recievedData = ExecCommand(port, "AT+CSCS=\"GSM\"", 300, "Set Charset to GSM");

After that i send message. Wow It was okay. The above code with correction is as follows

try
        {

            string recievedData = ExecCommand(port,"AT", 300, "No phone connected");
            recievedData = ExecCommand(port, "AT+CSCS=\"GSM\"", 300, "Set Charset to GSM");
            recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
            String command = "AT+CMGS=\"" + PhoneNo + "\"";
            recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo");         
            command = Message + char.ConvertFromUtf32(26) + "\r";
            recievedData = ExecCommand(port,command, 3000, "Failed to send message"); //3 seconds
            if (recievedData.EndsWith("\r\nOK\r\n"))
            {
                isSend = true;
            }
            else if (recievedData.Contains("ERROR"))
            {
                isSend = false;
            }
            return isSend;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }