Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > .NET

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-26-2016, 10:23 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 286
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
  #2  
Old 07-03-2016, 11:01 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 286
Default

Dear Expert,

Kindly respond if you have any idea.

Thanks in advance.
__________________
Rmt
Reply With Quote
  #3  
Old 07-04-2016, 07:09 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,313
Default

What I can read upon the AT commands is that it's regulated per country.

If that's the problem you'll need another protocol.
Reply With Quote
  #4  
Old 07-04-2016, 07:28 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 286
Default

First of thanks for reply.

Is it because of Country? Because I am able to send and receive SMS using GSM modem with AT commands. Only the problem is, after reading few sms it gives error 'Response Receieve is Incomplete'. Thanks.
__________________
Rmt
Reply With Quote
  #5  
Old 07-04-2016, 08:10 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,313
Default

That is what I get when I google it.
I have no experience with SMS from .NET, but I see a lot of people having this problem with AT commands.
At first it seems ok, but after a few they get the same error.
Reply With Quote
  #6  
Old 07-04-2016, 08:27 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 286
Default

ok anyways tnx.
__________________
Rmt
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:33 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.