ion
ion

Reputation: 79

GSMComm SMS delivery report

I've been trying to use GSMComm library in a C# app, in order to sent SMS messages using a mobile phone (connected via USB cable) as a GSM modem. I've read all similar threads in SO, but didn't help.

Everything works well, except the delivery reports. I set RequestStatusReport to true and i enabled notifications (EnableMessageNotifications()).

The problem is that i am not able to read the received delivery report, although i know its storage (it's always "SR") and index number. I keep getting a 321 error code (invalid index) because SR storage looks empty when i try to read from it.

The MessageReceived event code and the corresponding report are as following, any help would be greatly appreciated!

private static void Comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    IMessageIndicationObject obj = e.IndicationObject;
    if (obj is MemoryLocation)
    {
        MemoryLocation loc = (MemoryLocation)obj;
        Util.AddLog(string.Format("New message received in storage \"{0}\", index {1}.", loc.Storage, loc.Index));

        DecodedShortMessage msg = Comm.ReadMessage(loc.Index, loc.Storage);

        if (((SmsPdu)msg.Data) is SmsStatusReportPdu)
        {
            SmsStatusReportPdu data = (SmsStatusReportPdu)msg.Data;
            Util.AddLog("rec msg ref #: " + data.MessageReference.ToString());
        }
    }
    else
    {
        Util.AddLog("Error: Unknown notification object!");
    }
}

Report:

New message received in storage "SR", index 0.
[GSM_LOG]  17:08:49.528 Reading message...
[Catch in MessageReceived]  ##### ERROR: Message service error 321 occurred.
[GSM_LOG]  17:08:49.501 [gsmphone] >> 
[GSM_LOG]  17:08:49.501 [gsmphone]    +CDSI: "SR",0
[GSM_LOG]  17:08:49.501 [gsmphone]    
[GSM_LOG]  17:08:49.501 [gsmphone] Unsolicited message: New SMS-STATUS-REPORT received (indicated by memory location)
[GSM_LOG]  17:08:49.501 [gsmphone] Firing async MessageReceived event.
[GSM_LOG]  17:08:49.528 [gsmphone] Selecting "SR" as read storage...
[GSM_LOG]  17:08:49.528 [gsmphone] << AT+CPMS="SR"
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone] >> AT+CPMS="SR"
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone]    +CPMS: 0,0,0,23,1,40
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone]    OK
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone] Memory status: 0/0 (0% used)
[GSM_LOG]  17:08:49.528 [gsmphone] Activating PDU mode...
[GSM_LOG]  17:08:49.528 [gsmphone] << AT+CMGF=0
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone] >> AT+CMGF=0
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone]    OK
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone] Reading message from index 0...
[GSM_LOG]  17:08:49.528 [gsmphone] << AT+CMGR=0
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone] >> AT+CMGR=0
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.528 [gsmphone]    +CMS ERROR: 321
[GSM_LOG]  17:08:49.528 [gsmphone]    
[GSM_LOG]  17:08:49.545 [gsmphone] Failed. Phone reports message service (MS) error 321.
[GSM_LOG]  17:08:49.545 [gsmphone] AT+CMGR=0
[GSM_LOG]  17:08:49.545 [gsmphone] 
[GSM_LOG]  17:08:49.545 [gsmphone] +CMS ERROR: 321
[GSM_LOG]  17:08:49.545 [gsmphone] 
[GSM_LOG]  17:08:49.548 [gsmphone] Ending async MessageReceivedEventHandler call

Upvotes: 2

Views: 1661

Answers (1)

Scrappy
Scrappy

Reputation: 132

Indication object can be MemoryLocation or ShortMessage. There was no need to read from memory location again for the ShortMessage received (which is for delivery status) as you already have the object and just need to decode it. The below works for me

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        try
        {
            IMessageIndicationObject obj = e.IndicationObject;
            if (obj is MemoryLocation)
            {
                MemoryLocation loc = (MemoryLocation)obj;
                log.InfoFormat("New message received in storage {0}, index {1}.", loc.Storage, loc.Index);

            }
            else if (obj is ShortMessage)
            {
                ShortMessage msg = (ShortMessage)obj;
                SmsPdu pdu = comm.DecodeReceivedMessage(msg);                  

                //Here can be delivery status or received message so cast to the right type by doing type checks for SmsDeliverPdu or SmsStatusReportPdu


            }
            else
            {
                log.ErrorFormat("Unknown notification message received");
                log.InfoFormat("Message indication object is {0}", e.IndicationObject);
            }
        }
        catch (Exception ex)
        {
            log.ErrorFormat("An error occurred while message ws received. Error: {0}", ex.Message);
            log.Error(ex);
        }

Upvotes: 1

Related Questions