Main Site Documentation

Snippet - TCP Cellular Radio Driver


#1

TCP Cellular Radio Driver

Contains extensions and bugfixes to the CellularRadio drivers that let you send and receive data over a TCP connection.
Only NETMF 4.2 is supported, 4.3 will be supported following release and we will likely drop 4.2.
This project is available on Codeplax at https://tcpcellularradio.codeplex.com/ and you’ll need to get the drivers from here.
The maintained project and documentation will remain on Codeplex, this is here as a signpost!


#2

Hi Byron,

Thanks for your Codeshare ! it’s very hard to find information about this module …

I try to use your example code and I have the error
"Error 2 ‘GadgeteerApp1.CellularRadioManager.RadioState’ is a ‘property’ but is used like a ‘type’"
any ideas ?

Thanks


#3

Replied in your other thread at http://www.tinyclr.com/forum/topic?id=10047


#4

Dear Byron:
I am studying the Cellular Radio module.
I read your documentation on codeplex but still don’t understand how to use your driver.
I download the install file and installed it.
Add the reference GTM.DLSys.CellularRadio to my project.
I see your documentation use CellularRadioManager but when I add

private CellularRadioManager _radioManager;

shows error.
Error 1 The type or namespace name ‘CellularRadioManager’ could not be found (are you missing a using directive or an assembly reference?) F:\GadgeteerGPRSTest(Byron driver)\GadgeteerGPRSTest\Program.cs 21 17 GadgeteerGPRSTest

Thanks


#5

Hi Tzu,

Please make sure you download the source code and reference that, the installer is out of date and does not include the CeluularRadioManager in any case.


#6

HI Byron:
Thanks

I have do download the source code but there are 1 sln and 3 projects.
how to reference it?


#7

You can only reference a .csproj file.
So, in your solution, add the .csproj files and you’ll be able to add project references to your project.
I recommend you read the starter guides about NETMF, e.g. http://www.ghielectronics.com/downloads/FEZ/Beginners%20guide%20to%20NETMF.pdf


#8

Thanks, you have 3 projects ,all need to be added?


#9

Minimum required is a reference to CellularRadio_42.csproj
CellularRadioManager.csproj is optional if you want to try it out


#10

Ok, I add CellularRadio and CellularRadioManager. it seem works now.
I will keep try.
Thanks


#11

I got an error:
what I am missing?

    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        CellularRadio cellularRadio = new CellularRadio(4);
        private CellularRadioManager _radioManager;
        void ProgramStarted()
        {          

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
            //cellularRadio.DebugPrintEnabled = true;
            cellularRadio.PowerOn(5);
            cellularRadio.SendATCommand("AT");
            cellularRadio.SendATCommand("AT+CMGF=1");           
            cellularRadio.ModuleInitialized += new CellularRadio.ModuleInitializedHandler(cellularRadio_ModuleInitialized);
            cellularRadio.GprsNetworkRegistrationChanged += new CellularRadio.GprsNetworkRegistrationChangedHandler(cellularRadio_GprsNetworkRegistrationChanged);
           
        }

        void cellularRadio_GprsNetworkRegistrationChanged(CellularRadio sender, CellularRadio.NetworkRegistrationState networkState)
        {
            SendDataToServer("test");
        }       
       
        void cellularRadio_ModuleInitialized(CellularRadio sender)
        {
            Debug.Print("MODULE INITIALIZED");
            //cellularRadio.SendSms("0963354403","TEST");
            
                       
        }

       
        // use
        private bool SendDataToServer(string post)
        {
            Debug.Print("TcpSend: Connect");
            var retVal = true;
            if (!_radioManager.Connect("https://www.google.com.tw/"))
            {
                Debug.Print("Tcp Send: Connect Failed");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // send data
            Debug.Print("TcpSend: Start Send");
            if (!_radioManager.SendData(post))
            {
                Debug.Print("Tcp Send: SendData Failed");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // wait to flush
            string data;
            if (!_radioManager.ReceiveData(10000, out data))
            {
                Debug.Print("Tcp Send: ReceiveData Failed or empty");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            Debug.Print(data);

            // ReSharper disable StringIndexOfIsCultureSpecific.1
            if (data.IndexOf(@ "HTTP/1.1 201 Created") <= -1)
            {
                // unsuccessful server transaction, TODO: re-try?
                Debug.Print("Tcp Send: Server FAILED");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // successful server transaction
            Debug.Print("Tcp Send: Server OK - \"201 Created\"");
            TcpDisconnect();
            Thread.Sleep(2000);
            return true;

        }

        private void TcpDisconnect()
        {
            if (_radioManager.Disconnect())
            {
                Debug.Print("Tcp Send: Disconnected");
                return;
            }
            Debug.Print("Tcp Send: Disconnect Failed");
        }

    }

Debug.output

[quote]Program Started
The thread ‘’ (0x4) has exited with code 0 (0x0).
The thread ‘’ (0x5) has exited with code 0 (0x0).
MODULE INITIALIZED
TcpSend: Connect
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### GadgeteerGPRSTest.Program::SendDataToServer [IP: 0016] ####
#### GadgeteerGPRSTest.Program::cellularRadio_GprsNetworkRegistrationChanged [IP: 0008] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::OnGprsNetworkRegistrationChanged [IP: 0049] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in GadgeteerGPRSTest.exe
Error invoking method “Gadgeteer.Modules.Mekalogic.CellularRadio” (check arguments to Program.BeginInvoke are correct)
TcpSend: Connect
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### GadgeteerGPRSTest.Program::SendDataToServer [IP: 0016] ####
#### GadgeteerGPRSTest.Program::cellularRadio_GprsNetworkRegistrationChanged [IP: 0008] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::OnGprsNetworkRegistrationChanged [IP: 0049] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in GadgeteerGPRSTest.exe
Error invoking method “Gadgeteer.Modules.Mekalogic.CellularRadio” (check arguments to Program.BeginInvoke are correct)
[/quote]


#12

Please refer to http://tcpcellularradio.codeplex.com/documentation for usage examples.
You need to use CellularRadio OR CellularRadioManager


#13

Thanks Byron:
May be I am stupid. I see your documentation many times , but still not working.

The SMS is works use CellularRadio, but GPRS is not for using CellularRadioManager.
I don;t know the steps for post data.

I put

MyClass();
 SendDataToServer("");

in the

ProgramStarted()

I simple test the google in your SendDataToServer() method by
_radioManager.Connect(“https://www.google.com.tw/”)

But I get some errors.

[quote]Program Started
TcpSend: Connect
Tcp Send: Connect Failed
Tcp Send: Disconnect Failed
The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.Exception - 0x00000000 (4) ####
#### Message:
#### System.Convert::ToInt64 [IP: 00af] ####
#### System.Convert::ToInt32 [IP: 0011] ####
#### System.Int32::Parse [IP: 000b] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::ProcessATCommandResponse [IP: 01c9] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::SerialRead [IP: 0030] ####
A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
GSM Network Registration Error
GSM Network Registration Error
GSM Network Registration Error
The thread ‘’ (0x6) has exited with code 0 (0x0).
#### Exception System.Exception - 0x00000000 (4) ####
#### Message:
#### System.Convert::ToInt64 [IP: 00af] ####
#### System.Convert::ToInt32 [IP: 0011] ####
#### System.Int32::Parse [IP: 000b] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::ProcessATCommandResponse [IP: 01c9] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::SerialRead [IP: 0030] ####
A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
GSM Network Registration Error
GSM Network Registered
GPRS Network Registration Error
GPRS Network Registered
IMEI: 861785000232458
The thread ‘’ (0x5) has exited with code 0 (0x0).[/quote]


#14

You need to wait until GPRS Network Registered (NetworkRegistrationState is Registered or Roaming depending on your telco) is printed in your debug stream before you can send data using TCP.

You can’t use those methods directly in the program started method (it will warn you if it blocks for more than 10s).
The radio module (SIM900) can take upwards of 1 minute to power up and register with the network.
Here’s the procedure:


private CellularRadioManager _radioManager;

// ctor
public void ProgramStarted()
{
	 // for a FEZ Cerberus, using Socket 2, must be type "U"
 	_radioManager = new CellularRadioManager(2)
 	{
		DebugPrintEnabled = true,
		IsHttpModeEnabled = true
	};
}

// copy the following methods from the examples on CodePlex
private bool SendDataToServer(string post), replacing YOUR_SERVER_ADDRESS_HERE with your server address (name or IP)
private void TcpDisconnect()
private static void DebugPrint(string str)

Also in the SendDataToServer method above, replace if (data.IndexOf(@ "HTTP/1.1 201 Created") <= -1) with the correct response for your request. This particular test is expecting a 201 response from a web service.

Then use an event handler, say from a button, to call _radioManager.SendDataToServer("CORRECTLY FORMATTED HTTP REQUEST")
You should then be able to see the HTTP conversation

To get further help, please post your full source code and system configuration (modules, sockets, etc)


#15

Thank you Byron:
I will try it.


#16

Dear Byron:

Still can’t work.

My Setup Spider +radio module + Button.
socket is number 4.
After I see the “GPRS Network Registered”, I push the button to send data.

Debug.print

[quote]Program Started
The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.Exception - 0x00000000 (4) ####
#### Message:
#### System.Convert::ToInt64 [IP: 00af] ####
#### System.Convert::ToInt32 [IP: 0011] ####
#### System.Int32::Parse [IP: 000b] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::ProcessATCommandResponse [IP: 01c9] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::SerialRead [IP: 0030] ####
A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
GSM Network Registration Error
GSM Network Registration Error
GSM Network Registration Error
The thread ‘’ (0x6) has exited with code 0 (0x0).
#### Exception System.Exception - 0x00000000 (4) ####
#### Message:
#### System.Convert::ToInt64 [IP: 00af] ####
#### System.Convert::ToInt32 [IP: 0011] ####
#### System.Int32::Parse [IP: 000b] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::ProcessATCommandResponse [IP: 01c9] ####
#### Gadgeteer.Modules.Mekalogic.CellularRadio::SerialRead [IP: 0030] ####
A first chance exception of type ‘System.Exception’ occurred in mscorlib.dll
GSM Network Registration Error
GSM Network Registered
GPRS Network Registration Error
GPRS Network Registered
IMEI: 861785000232458
The thread ‘’ (0x5) has exited with code 0 (0x0).
TcpSend: Connect
Tcp Send: Connect Failed
Tcp Send: Disconnect Failed[/quote]

Full Code

namespace GadgeteerGPRSTest
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   

        private CellularRadioManager _radioManager; 
               
        void ProgramStarted()
        {
            
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
             
            _radioManager = new CellularRadioManager(4)            
            {
               
                DebugPrintEnabled = true,
                IsHttpModeEnabled = true
            };
          
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            

        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            SendDataToServer("");
        }

        private bool SendDataToServer(string post)
        {
            DebugPrint("TcpSend: Connect");
            var retVal = true;
            if (!_radioManager.Connect("www.google.com"))
            {
                DebugPrint("Tcp Send: Connect Failed");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // send data
            DebugPrint("TcpSend: Start Send");
            if (!_radioManager.SendData(post))
            {
                DebugPrint("Tcp Send: SendData Failed");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // wait to flush
            string data;
            if (!_radioManager.ReceiveData(10000, out data))
            {
                DebugPrint("Tcp Send: ReceiveData Failed or empty");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            DebugPrint(data);

            // ReSharper disable StringIndexOfIsCultureSpecific.1
            if (data.IndexOf(@ "HTTP/1.1 200 OK") <= -1)
            {
                // unsuccessful server transaction, TODO: re-try?
                DebugPrint("Tcp Send: Server FAILED");
                TcpDisconnect();
                Thread.Sleep(2000);
                return false;
            }
            // successful server transaction
            DebugPrint("Tcp Send: Server OK - \"201 Created\"");
            TcpDisconnect();
            Thread.Sleep(2000);
            return true;

        }

        private void TcpDisconnect()
        {
            if (_radioManager.Disconnect())
            {
                DebugPrint("Tcp Send: Disconnected");
                return;
            }
            DebugPrint("Tcp Send: Disconnect Failed");
        }

        private static void DebugPrint(string str)
        {
            Debug.Print(str);
        }
 
      

    }
}


#17

Hi Tzu, please try again, and this time wait until you see an IP in the debug stream.
Shortly I’ll post my testing configuration here.


#18

I’ve attached a sample application (I’ve also pushed a minor change to Codeplex, that was probably not affecting you, but please update)

Wait until you see the IP:XXX.XXX.XXX.XXX before you try to connect.

Also, please make sure you are setting your access point name (APN) otherwise it will look like it’s working, but actually not be able to use TCP.

Note if you try to GET from www.google.com, they will return a 302 response to your local google, not a 200.


CODE:
public partial class Program
{
	private CellularRadioManager _radioManager;
	void ProgramStarted()
	{
		Debug.Print("Program Started");
			joystick.JoystickPressed += joystick_JoystickPressed;
		_radioManager = new CellularRadioManager(2)
			{
				AccessPointName = "vfprepaymbb",
				DebugPrintEnabled = true,
				IsHttpModeEnabled = true
			};
	}
	private readonly string _crlf = "" + (char)13 + (char)10;
	private void joystick_JoystickPressed(Gadgeteer.Modules.GHIElectronics.Joystick sender, Gadgeteer.Modules.GHIElectronics.Joystick.JoystickState state)
	{
		_radioManager.Connect("www.google.com");
		var request =
			"GET / HTTP/1.1" + _crlf +
			"Host: www.google.com" + _crlf +
			_crlf;
		_radioManager.SendData(request);
		var response = "";
		_radioManager.ReceiveData(5000, out response);
		Debug.Print(response);
	}
}

DEBUG OUTPUT:
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
GSM Network Registration Error
GPRS Network Registration Error
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
A first chance exception of type 'System.Exception' occurred in mscorlib.dll
GSM Network Registration Error
GSM Network Registration Error
GSM Network Registration Error
The thread '<No Name>' (0x7) has exited with code 0 (0x0).
GSM Network Registered
GPRS Network Registration Error
GPRS Network Registered
IMEI: <REDACTED>
IP: <REDACTED>

HTTP/1.1 302 Moved Temporarily
Content-Length: 216      
Location: http://www.google.com.au/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=ca07caef880e295a:FF=0:TM=1363746929:LM=1363746929:S=zNI4HBU-QrLcmHjP; expires=Fri, 20-Mar-2015 02:35:29 GMT; path=/; domain=.google.com
Set-Cookie: NID=67=evenWcDOBxz2aMSsQKyUX9BAa6XN6Aly8FCLwk2WbhEmdeU-A0wHvzw6wmzEYp0tdm51F3v4ZC6d_DVqZ5Z4xRoFfDMI2cd-TDVUwkg6sqR-1NzKPIEQE2qFwGbJ8-xC; expires=Thu, 19-Sep-2013 02:35:29 GMT; path=/; domain=.google.com; HttpOnly
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Date: Wed, 20 Mar 2013 02:35:29 GMT
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Option


#19

(aside: Byron, Surry Hills NSW eh? I’m further south and a little west, by 10kms or thereabouts)


#20

Dear Byron:

Thank you very much. You help me so much.
After I try many times, suddenly its works.

Other question is the format for posting data.

For Get you use
private readonly string _crlf = “” + (char)13 + (char)10;
var request =
“GET / HTTP/1.1” + _crlf +
“Host: www.google.com” + _crlf +
_crlf;

I don’t really understand the request format.

If I have a WCF REST webservice

http://wra.xxx.org/WCF1/Service1/SendCO/{ITEMID_SENSORLIST}/{TIMESTAMP}/{CONCENTRATION}

How could I write the POST request string?

Thank you very much.