AT命令传递消息c#

问题描述 投票:0回答:1

我创建了winform应用程序以使用USB调制解调器发送SMS,它可以正常工作,但是我想获取传递消息并确认消息已正确发送。

这是我的程序

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# at-command
1个回答
1
投票

此问题不是C#特定的;而是一个AT命令问题。

发送短信后,您将收到调制解调器的响应,如下所示:

+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。

我根据this answer亲自使用了这种方法,并且有效。

编辑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();
        }

现在在您的send函数中,您应该检查最终是否包含+ CMGS :,]的响应>

© www.soinside.com 2019 - 2024. All rights reserved.