Main Site Documentation

Send SMS with CellularRadio module


#1

Hi,

i have Cerbuino Net and CellularRadio module. I updated bord to the newest firmware and installed newst SDK. I need two functions for my project, to send and to receive SMS.
I dont have experience with gadeteer developing but i managed to start up module and to receive SMS. I can’t send SMS and i can only receive one SMS and when i send another one i get exception and i have to reset bord and then i can receive one message. I can also receive one call and after that i have to reset.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace Cerbuino_SMS_test
{
    public partial class Program
    {
        void ProgramStarted()
        {
            Debug.Print("Program started");

            cellularRadio.DebugPrintEnabled = true;
            
            cellularRadio.PowerOn(40);

            cellularRadio.GsmNetworkRegistrationChanged += cellularRadio_GsmNetworkRegistrationChanged;
            cellularRadio.ImeiRetrieved += cellularRadio_ImeiRetrieved;
            cellularRadio.ModuleInitialized += cellularRadio_ModuleInitialized;
            cellularRadio.OperatorRetrieved += cellularRadio_OperatorRetrieved;
            cellularRadio.PhoneActivityRetrieved += cellularRadio_PhoneActivityRetrieved;
            cellularRadio.PinStateRetrieved += cellularRadio_PinStateRetrieved;
            cellularRadio.SignalStrengthRetrieved += cellularRadio_SignalStrengthRetrieved;
            cellularRadio.ClockRetrieved += cellularRadio_ClockRetrieved;

            cellularRadio.SmsListRetrieved += cellularRadio_SmsListRetrieved;
            cellularRadio.SmsReceived += cellularRadio_SmsReceived;
            cellularRadio.SmsRetrieved += cellularRadio_SmsRetrieved;

            cellularRadio.IncomingCall += cellularRadio_IncomingCall;
            cellularRadio.CallConnected += cellularRadio_CallConnected;
            cellularRadio.CallEnded += cellularRadio_CallEnded;

            GT.Timer t = new GT.Timer(100000);
            t.Tick += t_Tick;
            t.Start();
        }

        void cellularRadio_GsmNetworkRegistrationChanged(CellularRadio sender, CellularRadio.NetworkRegistrationState networkState)
        {
            switch (networkState)
            {
                case CellularRadio.NetworkRegistrationState.Error:
                    Debug.Print("NETWORK_STATE: Error");
                    break;
                case CellularRadio.NetworkRegistrationState.NotSearching:
                    Debug.Print("NETWORK_STATE: NotSearching");
                    break;
                case CellularRadio.NetworkRegistrationState.Registered:
                    Debug.Print("NETWORK_STATE: Registered");
                    break;
                case CellularRadio.NetworkRegistrationState.RegistrationDenied:
                    Debug.Print("NETWORK_STATE: RegistrationDenied");
                    break;
                case CellularRadio.NetworkRegistrationState.Roaming:
                    Debug.Print("NETWORK_STATE: Roaming");
                    break;
                case CellularRadio.NetworkRegistrationState.Searching:
                    Debug.Print("NETWORK_STATE: Searching");
                    break;
                case CellularRadio.NetworkRegistrationState.Unknown:
                    Debug.Print("NETWORK_STATE: Unknown");
                    break;
                default:
                    Debug.Print("NETWORK_STATE: default");
                    break;
            }
        }
        private void cellularRadio_ImeiRetrieved(CellularRadio sender, string imei)
        {
            Debug.Print("IMEI: " + imei);
        }
        private void cellularRadio_ModuleInitialized(CellularRadio sender)
        {
            Debug.Print("MODULE_INITIALIZED");
        }
        private void cellularRadio_OperatorRetrieved(CellularRadio sender, string operatorName)
        {
            Debug.Print("OPERATOR_RETRIEVED: " + operatorName);
        }
        private void cellularRadio_PhoneActivityRetrieved(CellularRadio sender, CellularRadio.PhoneActivityType activity)
        {
            switch (activity)
            {
                case CellularRadio.PhoneActivityType.CallInProgress:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: CallInProgress");
                    break;
                case CellularRadio.PhoneActivityType.CommLineBusy:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: CommLineBusy");
                    break;
                case CellularRadio.PhoneActivityType.Ready:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: Ready");
                    break;
                case CellularRadio.PhoneActivityType.Ringing:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: Ringing");
                    break;
                case CellularRadio.PhoneActivityType.Unknown:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: Unknown");
                    break;
                default:
                    Debug.Print("PHONE_ACTIVITY_RETRIEVED: default");
                    break;
            }
        }
        private void cellularRadio_PinStateRetrieved(CellularRadio sender, CellularRadio.PINState pinState)
        {
            switch (pinState)
            {
                case CellularRadio.PINState.NotPresent:
                    Debug.Print("PIN_STATE_RETRIEVED: NotPresent");
                    break;
                case CellularRadio.PINState.PH_PIN:
                    Debug.Print("PIN_STATE_RETRIEVED: PH_PIN");
                    break;
                case CellularRadio.PINState.PH_PUK:
                    Debug.Print("PIN_STATE_RETRIEVED: PH_PUK");
                    break;
                case CellularRadio.PINState.PIN:
                    Debug.Print("PIN_STATE_RETRIEVED: PIN");
                    break;
                case CellularRadio.PINState.PIN2:
                    Debug.Print("PIN_STATE_RETRIEVED: PIN2");
                    break;
                case CellularRadio.PINState.PUK:
                    Debug.Print("PIN_STATE_RETRIEVED: PUK");
                    break;
                case CellularRadio.PINState.PUK2:
                    Debug.Print("PIN_STATE_RETRIEVED: PUK2");
                    break;
                case CellularRadio.PINState.Ready:
                    Debug.Print("PIN_STATE_RETRIEVED: Ready");
                    break;
                default:
                    Debug.Print("PIN_STATE_RETRIEVED: default");
                    break;
            }
        }
        private void cellularRadio_SignalStrengthRetrieved(CellularRadio sender, CellularRadio.SignalStrengthType signalStrength)
        {
            switch (signalStrength)
            {
                case CellularRadio.SignalStrengthType.Error:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: Error");
                    break;
                case CellularRadio.SignalStrengthType.Strong:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: Strong");
                    break;
                case CellularRadio.SignalStrengthType.Unknown:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: Unknown");
                    break;
                case CellularRadio.SignalStrengthType.VeryStrong:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: VeryStrong");
                    break;
                case CellularRadio.SignalStrengthType.VeryWeak:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: VeryWeak");
                    break;
                case CellularRadio.SignalStrengthType.Weak:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: Weak");
                    break;
                default:
                    Debug.Print("SIGNAL_STRENGTH_RETRIEVED: default");
                    break;
            }
        }
        private void cellularRadio_ClockRetrieved(CellularRadio sender, DateTime clock)
        {
            Debug.Print("CLOCK_RETRIEVED: " + clock.ToString("dd.MM.yyyy  HH:mm:ss"));
        }

        private void cellularRadio_SmsListRetrieved(CellularRadio sender, ArrayList smsList)
        {
            Debug.Print("SMS_LIST_RETRIEVED - " + smsList.Count.ToString());
            foreach (CellularRadio.Sms sms in smsList)
            {
                Debug.Print("   FROM: " + sms.TelephoneNumber + " MSG: " + sms.TextMessage);
            }
        }
        private void cellularRadio_SmsReceived(CellularRadio sender, CellularRadio.Sms message)
        {
            Debug.Print("SMS_RECEIVED");
            Debug.Print("   TIME: " + message.Timestamp.ToString("dd.MM.yyyy  HH:mm:ss"));
            Debug.Print("   FROM: " + message.TelephoneNumber);
            Debug.Print("   MESSAGE: " + message.TextMessage);

            switch (message.Status)
            {
                case CellularRadio.SmsState.All:
                    Debug.Print("   STATUS: All");
                    break;
                case CellularRadio.SmsState.ReceivedRead:
                    Debug.Print("   STATUS: ReceivedRead");
                    break;
                case CellularRadio.SmsState.ReceivedUnread:
                    Debug.Print("   STATUS: ReceivedUnread");
                    break;
                case CellularRadio.SmsState.StoredSent:
                    Debug.Print("   STATUS: StoredSent");
                    break;
                case CellularRadio.SmsState.StoredUnsent:
                    Debug.Print("   STATUS: StoredUnsent");
                    break;
                default:
                    Debug.Print("   STATUS: default");
                    break;
            }
        }    
        private void cellularRadio_SmsRetrieved(CellularRadio sender, CellularRadio.Sms message)
        {
            Debug.Print("SMS_RETREIVED");
            Debug.Print("   TIME: " + message.Timestamp.ToString("dd.MM.yyyy  HH:mm:ss"));
            Debug.Print("   FROM: " + message.TelephoneNumber);
            Debug.Print("   MESSAGE: " + message.TextMessage);

            switch (message.Status)
            {
                case CellularRadio.SmsState.All:
                    Debug.Print("   STATUS: All");
                    break;
                case CellularRadio.SmsState.ReceivedRead:
                    Debug.Print("   STATUS: ReceivedRead");
                    break;
                case CellularRadio.SmsState.ReceivedUnread:
                    Debug.Print("   STATUS: ReceivedUnread");
                    break;
                case CellularRadio.SmsState.StoredSent:
                    Debug.Print("   STATUS: StoredSent");
                    break;
                case CellularRadio.SmsState.StoredUnsent:
                    Debug.Print("   STATUS: StoredUnsent");
                    break;
                default:
                    Debug.Print("   STATUS: default");
                    break;
            }
        }

        private void cellularRadio_IncomingCall(CellularRadio sender, string caller)
        {
            Debug.Print("INCOMING_CALL: " + caller);
        }
        private void cellularRadio_CallConnected(CellularRadio sender, string number)
        {
            Debug.Print("CALL_CONNECTED: " + null);
        }
        private void cellularRadio_CallEnded(CellularRadio sender, CellularRadio.CallEndType reason)
        {
            switch (reason)
            {
                case CellularRadio.CallEndType.Busy:
                    Debug.Print("CALL_ENDED: Busy");
                    break;
                case CellularRadio.CallEndType.NoCarrier:
                    Debug.Print("CALL_ENDED: NoCarrier");
                    break;
                case CellularRadio.CallEndType.NoDialTone:
                    Debug.Print("CALL_ENDED: NoDialTone");
                    break;
                default:
                    Debug.Print("CALL_ENDED: default");
                    break;
            }
        }

        void t_Tick(GT.Timer timer)
        {
            Debug.Print("TEST METODA");

            cellularRadio.RetrieveImei();
            cellularRadio.RetrieveOperator();
            cellularRadio.RetrievePhoneActivity();
            cellularRadio.RetrievePinState();
            cellularRadio.RetrieveSignalStrength();
            cellularRadio.RetrieveClock();

            cellularRadio.SendSms("098805066", "probna poruka");
        }
    }

}

Debug output (on timer event i tried to send sms and failed, then i received one message and after i wanted to receive one more i get exception:

[quote]Program started
The thread ‘’ (0x3) has exited with code 0 (0x0).
CellularRadio : Turning ON
CellularRadio : Turning module on
CellularRadio : <ÿ>
CellularRadio : <ÿÿ>
CellularRadio : <ÿ>
CellularRadio : SENT: AT

CellularRadio : <AT

OK

CellularRadio : SENT: AT+CMGF=1

CellularRadio : <AT+CMGF=1

OK

CellularRadio : SENT: AT+CSDH=0

CellularRadio : <AT+CSDH=0

OK

CellularRadio : SENT: AT+CPBS=“SM”

CellularRadio : <AT+CPBS=“SM”

OK

CellularRadio : SENT: AT+CPMS=“SM”

CellularRadio : <AT+CPMS=“SM”

ERROR
AT+C>
CellularRadio : SENT: AT+CNMI=2,1,0,1,0

CellularRadio : <NMI=2,1,0,1,0

OK
AT+COLP=1

OK

CellularRadio : SENT: AT+COLP=1

CellularRadio : <AT+CGREG=1

OK

CellularRadio : SENT: AT+CGREG=1

CellularRadio : <AT+CREG=1

OK

CellularRadio : SENT: AT+CREG=1

The thread ‘’ (0x5) has exited with code 0 (0x0).
CellularRadio : <
+CREG: 0

NETWORK_STATE: NotSearching
CellularRadio : <
+CREG: 2

NETWORK_STATE: Searching
CellularRadio : <
+CR>
CellularRadio : <EG: 1

+CGREG: 0

CellularRadio : <
+CGREG: 1

CellularRadio : <
+CREG: 1

NETWORK_STATE: Registered
TEST METODA
CellularRadio : SENT: AT+GSN

CellularRadio : <AT+GSN

013227005458744

OK

CellularRadio : SENT: AT+COPS?

CellularRadio : <AT+COPS?

+COPS: 0,0,“HR VIP”

OK

CellularRadio : SENT: AT+CPAS

CellularRadio : <AT+CPAS

+CPAS: 0

OK

CellularRadio : R:[0]
CellularRadio : SENT: AT+CPIN?

CellularRadio : <AT+CPIN?

CellularRadio : SENT: AT+CSQ

CellularRadio : <
+CPIN: READY

OK
AT+CSQ

+CSQ: 31,0

OK

CellularRadio : SENT: AT+CCLK?

CellularRadio : <AT+CCLK?

+CCLK: “00/01/01,06:44:48+00”

OK

CellularRadio : <AT+CMGS="+098805066"

IMEI: 013227005458744
OPERATOR_RETRIEVED: "HR VIP"
PHONE_ACTIVITY_RETRIEVED: Ready
PIN_STATE_RETRIEVED: Ready
SIGNAL_STRENGTH_RETRIEVED: VeryStrong
CLOCK_RETRIEVED: 01.01.2000 06:44:48
CellularRadio :
CellularRadio : <
ERROR

CellularRadio : <
+CMTI: “SM”,8

CellularRadio : SENT: AT+CMGR=8,1

CellularRadio : <AT+CMGR=8,1

CellularRadio : <
+CMGR: “REC UNREAD”,"+38598805066","","14/09/20,20:22:54+08"
Test

OK

CellularRadio : REQ: 8
SMS_RECEIVED
TIME: 20.09.2014 20:22:54
FROM: +38598805066
MESSAGE: Test
STATUS: ReceivedUnread
CellularRadio : <
+CMTI: “SM”,9

CellularRadio : SENT: AT+CMGR=9,1

CellularRadio : <AT+CMGR=9,1

CellularRadio : <
+CMGR: “REC UNREAD”,"+38598805066","",>
A first chance exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll
An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll
Uncaught exception
The thread ‘’ (0x4) has exited with code 0 (0x0).
[/quote]


#2

Are you running 4.3.4 and have ported the driver yourself?


#3

No need to port the driver as it’s included in 4.3.4


#4

Ahh sorry my mistake. It’s the GPRS driver which needs porting.

Anyway, this error reminds me very much about the struggles I was going through one year ago with the default module driver.

Not sure why this error only pops up for some people.


#5

should i switch to 4.2 version?


#6

The error you are getting is not related to the framework. It is a strange error that arises in a simple swtich/case in the driver, where the protocol does not do the same as when the driver was written. The return value from the driver is not handled correctly.

I suspect that that firmware on the SIM900 module is different, or the operators are adjusting parameters that makes the return values different.

I seriously hope that GHI are having a new driver in progress for 4.3.4 with matching firmware, GPRS/PPP support and other nice and cool things…@ Gus - ??


#7

Hi,
I get a similar error as well when I try to get time from the CelluarRadio ClockRetrieved Event.
Using the lastest 4.3 SDK from, GHI.

Here is the error:

#### Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (4) ####
#### Message: 
#### System.String::Substring [IP: 0000] ####
#### Gadgeteer.Modules.GHIElectronics.CellularRadio::run [IP: 05cd] ####

Exception System.ArgumentOutOfRangeException - CLR_E_OUT_OF_RANGE (4)

#### Message: 
#### System.String::Substring [IP: 0000] ####
#### Gadgeteer.Modules.GHIElectronics.CellularRadio::run [IP: 05cd] ####

A first chance exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll
An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll

I can not find the source code for the CelluarRadio module at:


Anyone know where I can find it?

Thanks,
R.E


#8

Source code is here

https://bitbucket.org/ghi_elect/gadgeteer/src/2567b2403077bdc7235f3697308d0f8966f6c6de/Modules/GHIElectronics/CellularRadio/?at=master


#9

Thanks,
It was a greate help!!
I was able to find the bug and fix it, if some one is interested, just give me few more days to play with the CelluarRadio code, the issues I did find was typical parsing strings with index out of range errors, easy fix but time consuming…LOL.

/R.E


#10

Posted my “fix” in a new topic.
htt*ps://www.ghielectronics.com/community/forum/topic?id=17132

R.E


#11

thanks for sharing, but does it work for GPRS as well ?