private void btnATCommand_Click(object sender, EventArgs e)//发送AT指令
{
if (txtATCommand.Text == "")
{
txtMsg.AppendText("\r\nAT指令为空!");
return;
}
if (!sp.IsConnect())
{
txtMsg.AppendText("\r\n串口打开失败!");
return;
}
string result = sp.ExeATCommand(delspace(txtATCommand.Text));
if (result != "")
{
txtMsg.AppendText("\r\n"+result);
}
sp.Close();
}
public bool IsConnect()
{
bool opened = IsOpen();
bool isScc;
isScc = false; if (opened)
{
readBuffer = ExeATCommand("AT\r");
if (readBuffer.IndexOf("OK") != -1)
{
isScc = true;
} if (IsListen == false)
{
Close();
}
} if (isScc)
{
return true;
}
else
{
return false;
} }private bool IsOpen()
{
try
{
if (!s_port.IsOpen)
{
s_port.Open();
}
Opened = s_port.IsOpen;
SetPDU();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 设置短信PDU方式
/// </summary>
private void SetPDU()
{
ExeATCommand("AT+CMGF=0\r");
}
/// <summary>
/// 执行AT命令
/// </summary>
/// <param name="p_ATCommand">命令</param>
/// <returns>结果字符串</returns>
public string ExeATCommand(string p_ATCommand)
{
string result = string.Empty; // 返回结果内容
int SendCommandCount = 0; // 重试次数
if (p_ATCommand != string.Empty && s_port.IsOpen == true)
{
s_port.Write(p_ATCommand); // 输出命令到串口
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
while (SendCommandCount <= retryCount)
{
// 没有达到重试次数时,始终执行
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
result += s_port.ReadExisting(); // 读取结果
if (result.Length > 0 && result.Length > p_ATCommand.Length)
{
result = result.Replace(p_ATCommand + "\r\r\n", string.Empty); // 去掉发送过来的命令,关心结果
break;
}
SendCommandCount++;
}
}
return result;
}
我单步调试时发现,设置PDU格式的时候,不论是0或1,返回的都是error,请大家帮帮忙!
{
if (txtATCommand.Text == "")
{
txtMsg.AppendText("\r\nAT指令为空!");
return;
}
if (!sp.IsConnect())
{
txtMsg.AppendText("\r\n串口打开失败!");
return;
}
string result = sp.ExeATCommand(delspace(txtATCommand.Text));
if (result != "")
{
txtMsg.AppendText("\r\n"+result);
}
sp.Close();
}
public bool IsConnect()
{
bool opened = IsOpen();
bool isScc;
isScc = false; if (opened)
{
readBuffer = ExeATCommand("AT\r");
if (readBuffer.IndexOf("OK") != -1)
{
isScc = true;
} if (IsListen == false)
{
Close();
}
} if (isScc)
{
return true;
}
else
{
return false;
} }private bool IsOpen()
{
try
{
if (!s_port.IsOpen)
{
s_port.Open();
}
Opened = s_port.IsOpen;
SetPDU();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 设置短信PDU方式
/// </summary>
private void SetPDU()
{
ExeATCommand("AT+CMGF=0\r");
}
/// <summary>
/// 执行AT命令
/// </summary>
/// <param name="p_ATCommand">命令</param>
/// <returns>结果字符串</returns>
public string ExeATCommand(string p_ATCommand)
{
string result = string.Empty; // 返回结果内容
int SendCommandCount = 0; // 重试次数
if (p_ATCommand != string.Empty && s_port.IsOpen == true)
{
s_port.Write(p_ATCommand); // 输出命令到串口
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
while (SendCommandCount <= retryCount)
{
// 没有达到重试次数时,始终执行
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
result += s_port.ReadExisting(); // 读取结果
if (result.Length > 0 && result.Length > p_ATCommand.Length)
{
result = result.Replace(p_ATCommand + "\r\r\n", string.Empty); // 去掉发送过来的命令,关心结果
break;
}
SendCommandCount++;
}
}
return result;
}
我单步调试时发现,设置PDU格式的时候,不论是0或1,返回的都是error,请大家帮帮忙!
private void btnATCommand_Click(object sender, EventArgs e)//发送AT指令
{
if (txtATCommand.Text == "")
{
txtMsg.AppendText("\r\nAT指令为空!");
return;
}
if (!sp.IsConnect())
{
txtMsg.AppendText("\r\n串口打开失败!");
return;
}
string result = sp.ExeATCommand(delspace(txtATCommand.Text));
if (result != "")
{
txtMsg.AppendText("\r\n"+result);
}
sp.Close();
}
public bool IsConnect()
{
bool opened = IsOpen();
bool isScc;
isScc = false; if (opened)
{
readBuffer = ExeATCommand("AT\r");
if (readBuffer.IndexOf("OK") != -1)
{
isScc = true;
} if (IsListen == false)
{
Close();
}
} if (isScc)
{
return true;
}
else
{
return false;
} } private bool IsOpen()
{
try
{
if (!s_port.IsOpen)
{
s_port.Open();
}
Opened = s_port.IsOpen;
SetPDU();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 设置短信PDU方式
/// </summary>
private void SetPDU()
{
ExeATCommand("AT+CMGF=0\r");
}
/// <summary>
/// 执行AT命令
/// </summary>
/// <param name="p_ATCommand">命令 </param>
/// <returns>结果字符串 </returns>
public string ExeATCommand(string p_ATCommand)
{
string result = string.Empty; // 返回结果内容
int SendCommandCount = 0; // 重试次数
if (p_ATCommand != string.Empty && s_port.IsOpen == true)
{
s_port.Write(p_ATCommand); // 输出命令到串口
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
while (SendCommandCount <= retryCount)
{
// 没有达到重试次数时,始终执行
System.Threading.Thread.Sleep(retryTimeOut); // 延迟指定时间
result += s_port.ReadExisting(); // 读取结果
if (result.Length > 0 && result.Length > p_ATCommand.Length)
{
result = result.Replace(p_ATCommand + "\r\r\n", string.Empty); // 去掉发送过来的命令,关心结果
break;
}
SendCommandCount++;
}
}
return result;
}
我单步调试时发现,设置PDU格式的时候,不论是0或1,返回的都是error,请大家帮帮忙!这样好读一些!