110,539
社区成员
发帖
与我相关
我的任务
分享
private string CommData = string.Empty;
public void Sp_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (Sp.IsOpen)
{
byte[] readBuffer = new byte[Sp.ReadBufferSize];
Sp.Read(readBuffer, 0, readBuffer.Length);
CommData = System.Text.Encoding.Default.GetString(readBuffer);
if (CommData.Length == 0)
return;
if (CommData.IndexOf("+CLIP:", 0) > 0)
{
if (CommData.IndexOf(",,", 0) > 0)
{
label1.Text = "来电号码:" + GetRingNumber(CommData);
}
}
if (CommData.IndexOf("\"MT\"", 0) > 0 || CommData.IndexOf("\"SM\"", 0) > 0 || CommData.IndexOf("\"ME\"", 0) > 0)
{
//读取短信内容
if (CommData.IndexOf("\"+CPMS\"", 0) < 0)
{
int smsIndex = GetSmsIndex(CommData);
string strOrder = "AT+CMGR=" + smsIndex.ToString() + "\r\n";
CommData = "";
Sp.Write(strOrder); //能不能立即触发 Sp_DataReceived,读到返回的数据
int currTime = 0;
int timeInc = 50;
while (currTime < 2000)
{
if (CommData.Trim() != "")
{
break;
}
else
{
Application.DoEvents();
System.Threading.Thread.Sleep(timeInc);
currTime = currTime + timeInc;
}
}
if (CommData.Trim() != "")
{
//处理数据,执行不到这句
}
}
}
if (CommData.IndexOf("+QMMNOTIFY:3", 0) > 0)
{
//ReadMms(SportData);
}
}
}