“响应接收不完整”使用命令发送和接收消息时 [英] "Response receive is incomplete" while sending and receiving message using at commands

查看:89
本文介绍了“响应接收不完整”使用命令发送和接收消息时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,



我正在开发使用GSM调制解调器发送和接收消息的SMS应用程序。我正在使用本网站中的代码使用AT命令发送和接收短信。



我可以使用GSM调制解调器成功发送和接收短信与本项目中的代码。当我第一次执行代码时,它完美地工作(发送几个短信)。但后来它显示收到的回复不完整。!!为什么会这样? Plz尽快帮助我。我想有人面临同样的问题,如果有任何想法请帮忙。



我尝试过的事情:



public void CallReadSMS()

{

试试

{

/ / count短信

int uCountSMS = CountSMSmessages(this.port);



#region命令

string strCommand =AT + CMGL = \ALL\;



#endregion



#区域读取短信并在文本文件的端口中写入所有短信

// .......................... ....................阅读所有短信........................... .........................



ob jShortMessageCollection = ReadSMS(this.port,strCommand);



string sPathName = @SMSapplicationReadMsgs;



StreamWriter sw = null;

sw = new StreamWriter(sPathName +。txt,false);







foreach(objShortMessageCollection中的ShortMessage消息)

{

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(例外情况)

{

ErrorLog(ex.Message);

}

}







public ShortMessageCollection ReadSMS(SerialPort port,string p_strCommand)

{

//设置手机并阅读消息

ShortMessageCollection messages = null;

尝试

{

#region执行命令





ExecCommand(端口,AT,3000,没有连接电话);





ExecCommand(端口,AT + CMGF = 1,3000,无法设置消息格式。);





ExecCommand( port,AT + CPMS = \SM\,3000,无法选择消息存储。;



//阅读消息

string input = ExecCommand(port,p_strCommand,5000,无法读取消息。);



#endregion



#region解析消息

messages = ParseMessages(输入);

#endregion

}

catch(exception ex)

{

throw ex;

}

if(messages!= null)

返回消息;

else

返回null;

}



public ShortMessageCollection ParseMessages(st响铃输入)

{

ShortMessageCollection messages = new ShortMessageCollection();

试试

{



正则表达式r =新正则表达式(@\ + CMGL:(\d +),(。+),(。+),( 。*),(。+)\\ n(。+)\ r \ n);





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





匹配m = r.Match(输入);

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] .Va lue;

msg.Sent = m.Groups [5] .Value;

msg.Message = m.Groups [6] .Value;

messages.Add(msg);



m = m.NextMatch();

}

< br $>
}

catch(exception ex)

{

throw ex;

}

返回消息;

}





公共字符串ExecCommand(SerialPort端口) ,string命令,int responseTimeout,string errorMessage)

{

试试

{



port.DiscardOutBuffer();

port.DiscardInBuffer();



receiveNow.Reset();





port.Write(命令+\ r);





string input = ReadResponse(port,responseTimeout);

if((input.Length == 0)|| ((!input.EndsWith(\\ nn>))&&(!input.EndsWith(\\\\\\\\)))))

抛出新的ApplicationException(没有收到成功消息。);

返回输入;

}

catch(Exception ex )

{

抛出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;

}

否则

{



if(buffer.Length> 0)

抛出新的ApplicationException(收到的响应不完整。);

else

抛出新的ApplicationException(没有从手机收到数据。);





}

}

while(!buffer .EndsWith(\\\\\\\)&&!buffer.EndsWith(\\\\ n>>)&&!buffer.EndsWith(\ r \ nERROR \\ n));

}

catch(例外情况)

{

throw ex;

}

返回缓冲区;

}







public bool CallSendSMS(string PhoneNo,string message)

{

bool isSend = false;

尝试
{



if(sendMsg(this.port,PhoneNo,Message))

isSend = true;

else if(!sendMsg(this.port,PhoneNo,Message))

isSend = false;

return isSend;



}



catch(例外情况)

{



ErrorLog(ex.Message);

返回isSend;



}



}





public bool sendMsg(SerialPort port,string PhoneNo,string Message)

{

bool isSend = false;

try

{

string recievedData = Exec命令(端口,AT,2000,没有连接手机);





recievedData = ExecCommand(端口,AT + CMGF = 1,2000,无法设置消息格式。);



字符串命令=AT + CMGS = \+ PhoneNo +\\ \\;

recievedData = ExecCommand(端口,命令,4000,无法接受phoneNo);



command = Message + char.ConvertFromUtf32(26)+\ r;

recievedData = ExecCommand(port,command,4000,无法发送消息); //减少到4秒





if(recievedData.EndsWith(\\\\ nNOK \\\\ n) ))

{

isSend = true;

}

else if(recievedData.Contains(ERROR) ))

{

isSend = false;

}

else

{

isSend = true;

}

返回isSend;

}

catch (例外情况)

{

抛出前任;

}



}

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

}

推荐答案

请将您的问题发布在文章末尾的论坛中。
Please post your question in the forum at the end of the article.


这篇关于“响应接收不完整”使用命令发送和接收消息时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆