View Single Post
  #1  
Old 06-26-2016, 10:23 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 299
Default C# .Net : Response Receive is Incomplete

Dear Expert,

I am developing SMS Application using c# Dotnet. I am using AT commands for sending and receiving SMS.

The code is written with reference to the code in

http://www.codeproject.com/Articles/...gh-a-GSM-Modem

I can successfully send and receive SMS using GSM modem. WhenI execute the code first time, it will work perfectly (send 4 sms). but later it shows "Response received is incomplete."!! why it happens? Plz help me as soon as possible.

Below is the code:
HTML Code:
public void CallReadSMS()
        {
            try
            {
                //count SMS                                 
                int uCountSMS = CountSMSmessages(this.port);
                
                    #region Command
                    string strCommand = "AT+CMGL=\"ALL\"";
                
                    #endregion
                
                    #region Read SMS and write all SMS in a Port in a Text File
                    //.............................................. Read all SMS ....................................................                                      
                                        
                    objShortMessageCollection = ReadSMS(this.port, strCommand);

                    string sPathName = @"SMSapplicationReadMsgs";
                                        
                    StreamWriter sw = null;
                    sw = new StreamWriter(sPathName + ".txt", false);

                   

                    foreach (ShortMessage msg in objShortMessageCollection)
                    {
                        sw.WriteLine(msg.Index + "|" + msg.Sent + "|" + msg.Sender + "|" + msg.Message);

                        ErrorLog(msg.Index + "|" + msg.Sent + "|" + msg.Sender + "|");

                    }
                    
                    
                    if (sw != null)
                    {
                        sw.Flush();
                        sw.Dispose();
                        sw.Close();
                    }
                     
                    
                    #endregion

            }
            catch(Exception ex)
            {
                ErrorLog(ex.Message);
            }
        }



public ShortMessageCollection ReadSMS(SerialPort port, string p_strCommand)
        {
            // Set up the phone and read the messages
            ShortMessageCollection messages = null;
            try
            {
                #region Execute Command
                
                
                ExecCommand(port, "AT", 3000, "No phone connected");
                
                
                ExecCommand(port, "AT+CMGF=1", 3000, "Failed to set message format.");


                ExecCommand(port, "AT+CPMS=\"SM\"", 3000, "Failed to select message storage.");

                // Read the messages
                string input = ExecCommand(port, p_strCommand, 5000, "Failed to read the messages.");

                #endregion

                #region Parse messages
                messages = ParseMessages(input);
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (messages != null)
                return messages;
            else
                return null;
        }

public ShortMessageCollection ParseMessages(string input)
        {
            ShortMessageCollection messages = new ShortMessageCollection();
            try
            {

                Regex r = new Regex(@"\+CMGL: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n");

                
                input = System.Text.RegularExpressions.Regex.Replace(input, @"\r\r\n+", "");
                

                Match m = r.Match(input);
                while (m.Success)
                {
                    ShortMessage msg = new ShortMessage();
                    
                    msg.Index = m.Groups[1].Value;
                    msg.Status = m.Groups[2].Value;
                    msg.Sender = m.Groups[3].Value;
                    msg.Alphabet = m.Groups[4].Value;
                    msg.Sent = m.Groups[5].Value;
                    msg.Message = m.Groups[6].Value;
                    messages.Add(msg);

                    m = m.NextMatch();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return messages;
        }


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

        public string ReadResponse(SerialPort port, int timeout)
        {
            string buffer = string.Empty;
            try
            {
                do
                {
                    if (receiveNow.WaitOne(timeout, false))
                    {
                        string t = port.ReadExisting();
                        buffer += t;
                    }
                    else
                    {
                        
                        if (buffer.Length > 0)
                            throw new ApplicationException("Response received is incomplete.");
                        else
                            throw new ApplicationException("No data received from phone.");
                        
                        
                    }
                }
                while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return buffer;
        }



public bool CallSendSMS(string PhoneNo,string Message)
        {
            bool isSend = false;
            try
            {
             
                if (sendMsg(this.port, PhoneNo, Message))                
                    isSend = true;
                else if (!sendMsg(this.port, PhoneNo, Message))                
                    isSend = false;
                    return isSend;
                
            }
            
            catch (Exception ex)
            {
                
                ErrorLog(ex.Message);
                return isSend;
                
            }
           
        }


public bool sendMsg(SerialPort port, string PhoneNo, string Message)
        {
            bool isSend = false;
            try
            {
                string recievedData = ExecCommand(port, "AT", 2000, "No phone connected");
                

                recievedData = ExecCommand(port, "AT+CMGF=1", 2000, "Failed to set message format.");

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

                command = Message + char.ConvertFromUtf32(26) + "\r";
                recievedData = ExecCommand(port, command, 4000, "Failed to send message"); //decreased to 4 seconds

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

        }
        
Thanks a lot in advance.
__________________
Rmt
Reply With Quote