【问题标题】:C# GSMcomm extract SMS when MessageReceived is triggeredC# GSMcomm 在触发 MessageReceived 时提取 SMS
【发布时间】:2016-02-18 10:24:45
【问题描述】:

有没有办法在 MessageReceived 事件触发时提取新收到的消息的内容并将其放入字符串变量中?

【问题讨论】:

  • 你需要什么来提取那些传入的消息?将其存储到数据库或其他?给个解释

标签: c# gsmcomm


【解决方案1】:

很简单,只需添加这样的方法:

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    var obj = e.IndicationObject;
    if (obj is MemoryLocation)
    {
        var loc = (MemoryLocation)obj;
        var msg = string.Format("New message received in storage \"{0}\", index {1}.",
                                loc.Storage, loc.Index);
        MessageBox.Show(msg);

        DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
        foreach (DecodedShortMessage message in messages)
        {
            DisplayMessage(message.Data);
        }
        return;
    }
}

private void DisplayMessage(SmsPdu pdu)
{
    if (pdu is SmsDeliverPdu)
    {
        SmsDeliverPdu data = (SmsDeliverPdu)pdu;
        var phoneNumber = data.OriginatingAddress; 
        var msg = data.UserDataText;
        var date = string.Format("{0:dd/MM/yyyy}", data.SCTimestamp.ToDateTime());
        var time = string.Format("{0:HH:mm:ss}", data.SCTimestamp.ToDateTime());

        //read message in listBox1
        listBox1.Items.Add(string.Format("{0}, {1}, {2}, {3}", date, time, phoneNumber, msg));
    }
}

但不要忘记在连接打开时注册此事件:

comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);

希望对你有所帮助:D

【讨论】:

  • 试图实现这一点。这里的“CommSetting”是什么?它显示错误
  • CommSetting 是用于设置与 gsm 通信库的连接的类。转到此链接并下载示例项目link
  • class CommSetting { public static string Comm_Port = ""; public static Int64 Comm_BaudRate = 0; public static Int64 Comm_TimeOut = 0; public static GsmCommMain comm; public CommSetting() { // // TODO: Add constructor logic here // } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2018-02-09
  • 1970-01-01
  • 2016-06-27
  • 2012-08-05
  • 1970-01-01
相关资源
最近更新 更多