如何在gsmcomm库中查看消息发送成功或失败

本文关键字:消息 成功 失败 gsmcomm | 更新日期: 2023-09-27 17:54:36

我使用gsmcomm库和c#构建了一个简单的发送消息的应用程序。当我通过调制解调器发送消息时,我将其存储在oracle数据库SENTMESSAGE表中。当发送消息失败时,我提供了一个表,即FAILEDMESSAGE表(ID, DATE, TIME, PHONENUMBER, message),但我仍然不明白如何实现它。

谁可以建议我如何区分发送消息成功和失败在gsmcomm库?

这是我发送消息的代码:

private void btnSentSMS_Click(object sender, EventArgs e)
{
    var msg = txtMessage.Text;
    var phoneNumber = txtNumber.Text;
    var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
    comm.SendMessage(pdu);
    MessageBox.Show("sms sent");
    //STORE SEND SMS TO DATABASE
    OracleCommand cmd = new OracleCommand();
    cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                       (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                        + phoneNumber + "', '" + msg + "')";
    cmd.Connection = koneksi_manual.con;
    koneksi_manual.con.Open();// <= Open connection before executing the command.
    cmd.ExecuteNonQuery();
    koneksi_manual.con.Close(); //closing connection
}

如何在gsmcomm库中查看消息发送成功或失败


请尝试此代码:

private void btnSentSMS_Click(object sender, EventArgs e)
{
    var msg = txtMessage.Text;
    var phoneNumber = txtNumber.Text;
    var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
    if(comm.SendMessage(pdu))
    {
        MessageBox.Show("sms sent");
        //STORE SEND SMS TO DATABASE
        OracleCommand cmd = new OracleCommand();
        cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                   (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                    + phoneNumber + "', '" + msg + "')";
cmd.Connection = koneksi_manual.con;
koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
else
{
MessageBox.Show("sms not sent");
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO FAILEDMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES 
                   (SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
                    + phoneNumber + "', '" + msg + "')";
            cmd.Connection = koneksi_manual.con;
        koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
}

SemdMessage方法如下:

public bool SendMessage(SerialPort port, string phoneNo, string message)
    {
        bool isSend = false;
        try
        {                
            string recievedData = SendATCommand(port,"AT", 300, "No phone connected");
            string command = "AT+CMGF=1" + char.ConvertFromUtf32(13);
            recievedData = SendATCommand(port,command, 300, "Failed to set message format.");
            // AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/
            command = "AT+CMGS='"" + phoneNo + "'"" + char.ConvertFromUtf32(13);
            recievedData = SendATCommand(port, command, 300,
                "Failed to accept phoneNo");
            command = message + char.ConvertFromUtf32(26);
            recievedData = SendATCommand(port, command, 3000,
                "Failed to send message"); //3 seconds
            if (recievedData.EndsWith("'r'nOK'r'n"))
                isSend = true;
            else if (recievedData.Contains("ERROR"))
                isSend = false;
            return isSend;
        }
        catch (Exception ex)
        {
            throw ex; 
        }          
    }