使用C#中的GSMcomm库提取收到的短信内容 [英] extracting recieved sms content using the GSMcomm library in C#

查看:79
本文介绍了使用C#中的GSMcomm库提取收到的短信内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我正在一个需要接收短信并由我的C#应用​​程序提取内容的项目中.

我正在使用GSMcomm库与连接到计算机的telit EZ-10 GSM调制解调器进行通信.

启用了新的消息通知,因此,每当收到新的SMS时,调制解调器就会向PC发送命令(包含新短信的位置).

我能够收到短信,但我无法提取所需的短信内容.我在foreach语句中收到以下错误:

"NullReferenceException未由用户代码处理
对象引用未设置为对象的实例."

调试时,收到的短信不会显示在"sms"文本框中,并且不会显式发生异常.仅当我在foreach语句上设置断点,然后告诉调试器执行下一条语句时,才会发生该异常.异常发生在此行:SmsPdu smsrec = message.Data;



表单代码如下:

Hi all.

I am working on a project that requires an sms to be received and the contents extracted by my C# application.

I am using the GSMcomm library to communicate with a telit EZ-10 GSM modem connected to my computer.

New message notifications are enabled, hence the modem sends a command(containing the location of the new sms) to the PC whenever a new SMS is received.

I am able to receive the SMS fine but i cannot extract the required sms content. I receive the following error during the foreach statement:

"NullReferenceException was unhandled by user code
Object reference not set to an instance of an object."

While debugging, the received sms does not show in the "sms" textbox and the exception does not explicitly occur. The exception only occurs when I set a breakpoint at the foreach statment and then tell the debugger to execute the next statement . The exception occurs at this line: SmsPdu smsrec = message.Data;



Form Code follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using GsmComm.GsmCommunication;
using GsmComm.Interfaces;
using GsmComm.PduConverter;
using GsmComm.Server;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        GsmCommMain comm;
        string actualtext;
        
        public Form1()
        {
         InitializeComponent();
        comm = new GsmCommMain(9, 115200);
        comm.Open();
        comm.EnableMessageNotifications();
        comm.MessageReceived += new   MessageReceivedEventHandler(comm_MessageReceived);

        }

        private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            IMessageIndicationObject obj = e.IndicationObject;
            MemoryLocation loc = (MemoryLocation)obj;
            DecodedShortMessage[] messages;
            messages = comm.ReadMessages(PhoneMessageStatus.ReceivedUnread, loc.Storage);

            foreach (DecodedShortMessage message in messages)
            {
                SmsDeliverPdu data = new SmsDeliverPdu();

                SmsPdu smsrec = message.Data;
                ShowMessage(smsrec);

            }
        }

        private void ShowMessage(SmsPdu pdu)
        {// Received message
            SmsDeliverPdu data = (SmsDeliverPdu)pdu;

            actualtext = data.UserDataText;
            SetSomeText(actualtext);

            return;
        }
       
        delegate void SetTextCallback(string text);
        private void SetSomeText(string text)
        {
            if (sms.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetSomeText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                sms.Text = text;
            }
        }
    }
}

推荐答案

这很可能是因为调用ReadMessages()方法后messages数组设置为null.
因此,问题可能出在上述方法中.我无法告诉您如何解决它,因为我完全不了解特定库...我建议仔细阅读文档.

现在我注意到您在"foreach语句期间"写了错误.如果这意味着它位于foreach的正文中,则我的回答是错误的...请更好地指定错误.
This is most probably because the messages array is set to null after calling the ReadMessages() method.

Thus the problem is probably in the method mentioned above. I can''t tell you how to fix it since I have absolutely no knowledge of the particlular library... I suggest reading the documentation carefully.

Now I noticed you wrote the error happens "during the foreach statement". If that means it''s inside the body of the foreach, then my answer is wrong... Please specify the error a bit better.


这篇关于使用C#中的GSMcomm库提取收到的短信内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆