AT命令传递消息c#
我已经创建了winform应用程序来使用USB调制解调器发送短信,它工作正常,但我想获得传递消息并确认消息已正确发送 这是我的节目AT命令传递消息c#,c#,at-command,C#,At Command,我已经创建了winform应用程序来使用USB调制解调器发送短信,它工作正常,但我想获得传递消息并确认消息已正确发送 这是我的节目 private void button1_Click(object sender, EventArgs e) { try { SerialPort sp = new SerialPort(); sp.PortName = textBox1.Text; sp.Open(); sp.Writ
private void button1_Click(object sender, EventArgs e)
{
try
{
SerialPort sp = new SerialPort();
sp.PortName = textBox1.Text;
sp.Open();
sp.WriteLine("AT" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CSCS=\"GSM\"" + Environment.NewLine);
Thread.Sleep(100);
sp.WriteLine("AT+CMGS=\"" + mobile + "\"" + Environment.NewLine);
Thread.Sleep(100);
sp.Write(message);
Thread.Sleep(100);
sp.Write(new byte[] { 26 }, 0, 1);
Thread.Sleep(100);
var response = sp.ReadExisting();
if (response.Contains("ERROR: 500"))
{
MessageBox.Show("Please check credits");
}
sp.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
请帮助我如何阅读带有上述代码的交付状态此问题并非针对C;相反,这是一个问题
发送短信息后,您将收到调制解调器的响应,如下所示:
+CMGS: {sms id, 0 to 255}
OK
在这种情况下,如果服务中心已成功发送SMS,调制解调器将返回以下响应:
+cds: {some id which does not matter} {PDU status report}
您只需解码此PDU即可获得状态报告、原始SMS ID和其他有用数据。如果发送的SMS的ID与状态报告中的ID相等,则您的消息具有完全相同的状态报告
注意:如果您在收到传递报告之前从调制解调器存储器中删除消息,您将得到包含所有常用信息的报告,但传递状态很可能是71而不是0
我自己也使用了这种方法,基于,它是有效的
编辑1:
您正在同步处理RS232读取,我不建议这样做,当端口中的数据可用时,读取功能应自动启动,类似于:
private string SerialDataReceived = string.Empty
private void button1_Click(object sender, EventArgs e)
{
// new instance of the COM port
port = new SerialPort(ComPort, 115200, Parity.None, 8, StopBits.One);
// start port lessener
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
// Begin communications and wait for nad to reboot completly
port.Open();
//send your AT Commands
port.Write("ATE0\r\n");
// check the response 'if Command is successfull it reply with something +Ok'
if(SerialDataReceived.ToLower().Contains("ok"))
}
//event will be fired each time a new data are available in the port
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
SerialDataReceived += port.ReadExisting();
}
现在,在发送功能中,您应该检查您是否最终有一个包含+CMGS:,的响应
response
是否包含任何数据?是的,它包含但没有关于交付的信息我不知道我的回答是否对您有帮助。这是根据OP的+CNMI命令问题定制的解释。。您必须读取并解析从调制解调器接收的响应。
var response = sp.ReadExisting();
while (!response.Contains("OK") && !response.Contains("ERROR"))
{
// check if message has been sent before exiting
response = sp.ReadExisting();
}