111,129
社区成员
发帖
与我相关
我的任务
分享
string strCmd = "AT+CMMS=3\r";
sp.Write(strCmd);
Thread.Sleep(this.TimeCmdSleep);
strCmd = "AT+CMGF=1\r";
sp.Write(strCmd);
Thread.Sleep(this.TimeCmdSleep);
strCmd = "AT+CMGS=" + tel + "\r";
sp.Write(strCmd);
Thread.Sleep(this.TimeCmdSleep);
strCmd = PublicMethod.EncodingUCS2(content) + "\x1A";
sp.Write(strCmd);

sp.DiscardInBuffer();//清除接受缓存
sp.WriteLine("AT+CMMS=3");//发命令
int tick = Environment.TickCount;//记录当前时间戳
int timeout = 2000;//你这个自己定,我假设2秒延迟等待应答,超过则提示超时无应答。
bool ack_found = false;//标志是否收到应答
StringBuilder buf = new StringBuilder();//定义缓存
while(Environment.TickCount - tick < timeout && !ack_found)//如果没收到应答或尚未超时,则循环
{
buf.Append(sp.ReadExisting());//将当前串口数据记录进缓存
//ack_found表示是否收到应答,没收到的话再继续循环就可以了。
ack_found = buf.ToString().IndexOf("OK") != -1;//假设应答为OK,检查收到数据中是否包含应答。
Application.DoEvents();//处理消息,避免界面卡死
}
if(!ack_found)
{
MessageBox.Show("step 1 error response. Try later.");
return;
}
sp.WriteLine("AT+CMMS=3");
int tick = Environment.TickCount;
int timeout = 2000;
bool ack_found = false;
StringBuilder buf = new StringBuilder();
while(Environment.TickCount - tick < timeout && !ack_found)
{
buf.Append(sp.ReadExisting());
ack_found = buf.ToString().IndexOf("OK") != -1;//假设应答为OK
Application.DoEvents();//处理消息
}
if(!ack_found)
{
MessageBox.Show("step 1 error response. Try later.");
return;
}
依次类推的分析所有数据的返回值