Click here to Skip to main content
16,010,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Expert,

I am developing SMS application to send and receive messages using GSM modem. I am using the code in this site "Send and Receive SMS using AT command".

I can successfully send and receive SMS using GSM modem with the Code in this Project. When I execute the code for the first time, it works perfectly (send few sms). but later it shows "Response received is incomplete."!! why it happens? Plz help me as soon as possible. I suppose someone was facing the same problem, Please help if any idea.

What I have tried:

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

}
Posted
Updated 3-Jul-16 22:36pm

1 solution

Please post your question in the forum at the end of the article.
 
Share this answer
 
Comments
RashmitaS 21-Jul-16 2:11am    
I have already posted the question in the forum of the article. But there is no any response. So, I posted it here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900