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)
{
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;
}
}
}
}