FEZ Cobra 2 Net + Cellular Radio | 4.3

Hi guys,

I’m writing code (4.3 framework) that works with EthernetENC28J60 (to surf the internet) and Cellular Radio (to send/receive SMS and calls).

I set the ethernet on cobra 2 net taking a look at this document: https://www.ghielectronics.com/docs/318/fez-cobra-ii-developers-guide#3303

When I try to set static IP address to the ethernet module I get this exception:

[quote] #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (6) ####
#### Message:
#### Gadgeteer.Modules.GHIElectronics.CellularRadio::get_IsNetworkConnected [IP: 0008] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkAvailabilityChangedEventHandler::Invoke [IP: a0e6f3b6] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange::OnNetworkChangeCallback [IP: 002e] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange+NetworkChangeListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
Eccezione first-chance di tipo ‘System.NullReferenceException’ in GTM.GHIElectronics.CellularRadio.dll
[/quote]

I think that the exception is thrown because both ethernet enc28j60 and cellular radio module are “network interfaces” and I haven’t set the default network interface. Am I wrong?

When I wrote project on 4.2 framework I used to call the method



Now, this method doesn't exists in the GHI.Networking.EthernetENC28J60 class that the document linked abote suggest to use.

Can you help me? :D

Can you post some basic code showing when this happens?

Yes, this is the code:

Public Sub ProgramStarted()
            '*******************************************************************************************
            ' Hardware modules added in the Program.gadgeteer designer view are used by typing 
            ' their name followed by a period, e.g.  button.  or  camera.
            '
            ' Many hardware modules generate useful events. To set up actions for those events, use the 
            ' left dropdown box at the top of this code editing window to choose a hardware module, then
            ' use the right dropdown box to choose the event - an event handler will be auto-generated.
            '*******************************************************************************************/
            '##### FEZ COBRA EXTENDER
            RegisterExtenderSockets()

            '##### ETHERNET ENC28J60
            AddHandler NetworkChange.NetworkAvailabilityChanged, AddressOf NetworkChange_NetworkAvailabilityChanged
            AddHandler NetworkChange.NetworkAddressChanged, AddressOf NetworkChange_NetworkAddressChanged
            ethernetENC = New EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9)
            ethernetENC.Open()

            Thread.Sleep(50)

            '##### CELLULAR RADIO MODULE SOCKET 8
            cellularRadio = New GTM.GHIElectronics.CellularRadio(8)
            cellularRadio.DebugPrintEnabled = True

            Thread.Sleep(50)

            ethernetENC.EnableStaticIP("192.168.2.64", "255.255.255.0", "192.168.2.1")
            ethernetENC.EnableStaticDns(New String() {"192.168.2.4", "8.8.8.8"})


            ' Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started")
        End Sub

Exception thrown here:

im no expert in this but can you try to call ethernetENC.open() AFTER you set static ip

I tried but the exception still occurs…

currently MF does not support two IP network interfaces at the same time.

@ Mike - so I can’t use cellular radio for receive SMS when I use also an Ethernet module???
It seems too strange…

Just to clarify, I want to use ethernet enc28 module (soldered on the fez cobra 2 net, not as external module) to surf the internet.
Cellular radio module will be useful to send and receive SMS, I won’t use it as network interface, never!

I’m using the last source code of Cellular Radio driver downloaded from bitbucket. The exception is thrown in the Cellular Radio code, here:

/// <summary>
/// Whether or not the network is connected. Make sure to also check the NetworkUp property to verify network state.
/// </summary>
public override bool IsNetworkConnected
{
	get
	{
		return this.networkInterface.LinkConnected;
	}
}

“this.networkInterface” is null and asking for “LinkConnected” property throws the exception (see also the attached image).

So, or I find how to call the method ethernetENC28.UseThisNetworkInterface() (but GHI.Networking.EthernetENC28J60 class doesn’t have it), or I can check that “this.networkInterface” isn’t null, or… or I don’t know!!

@ BigFoot - Can you post the contents of the NetworkAvailabilityChanged and NetworkAddressChanged event handlers?

@ John - This is the code:


     Private Sub NetworkChange_NetworkAddressChanged(sender As Object, e As Microsoft.SPOT.EventArgs)
	Debug.Print("Network address changed: " & ethernetENC.IPAddress)
	If (ethernetENC.IPAddress <> "0.0.0.0") Then
		'//The network is now ready to use.
	End If
End Sub

Private Sub NetworkChange_NetworkAvailabilityChanged(sender As Object, e As NetworkAvailabilityEventArgs)
	Debug.Print("Network availability: " & e.IsAvailable.ToString())
End Sub

It’s exactly the same code you can find in the document I read (https://www.ghielectronics.com/docs/318/fez-cobra-ii-developers-guide#3303 ).

I made also this test: I removed the event handlers to avoid event management (maybe there was an error in the code I wrote in event handlers I thought), but the exception persists.

Just one point, if I intialize only the soldered enc28 module, it works perfectly. Also, if I initialize only the cellular radio module, it works perfectly.
This exception is thrown only when I initialize both modules and, after that, I set the network configuration to the enc28 module.

I found this topic: https://www.ghielectronics.com/community/forum/topic?id=18295&page=2

I followed what Sprigo suggested… so I added the ENC28 through toolbox to avoid usage of GHI.Networking.EthernetENC28J60 class and connected the ENC28 to port 6 (as you can see in attachments).

But the exception is still thrown.

This is the code of this last test:


Public Sub ProgramStarted()
	'##### FEZ COBRA EXTENDER
	RegisterExtenderSockets()

	Thread.Sleep(50)

	'#####  CELLULAR RADIO MODULE 
	cellularRadio = New GTM.GHIElectronics.CellularRadio(8)
	cellularRadio.DebugPrintEnabled = True

	Thread.Sleep(50)

	ethernetENC28.UseThisNetworkInterface()

	Thread.Sleep(50)

	ethernetENC28.NetworkSettings.EnableStaticIP("192.168.2.64", "255.255.255.0", "192.168.2.1")
	ethernetENC28.NetworkSettings.EnableStaticDns(New String() {"192.168.2.4", "8.8.8.8"})

	' Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
	Debug.Print("Program Started")
End Sub

before you use the interface set the IP address and dns

@ BigFoot do you get the same issue if you remove the line



I was going to test it on my setup but my Cobra's are both WiFi.

@ Mike - Calling .UseThisNetworkInterface() after set the ip address and dns still throws the same exception. Same behavior.

@ Sprigo - Removing .UseThisNetworkInterface() seems to solve the issue and the exception is not thrown… but what a pity! The ethernet doesn’t work!
It doesn’t reply to ping, an exception is thrown if I try to start a webserver, etc.

All these test are made using an external ENC28 module… but having an ENC28 module “built in” on Cobra, I think it’s better to find how to make this working! ;D

Sincerely my project was working correctly using 4.2 NETMF but, due to cellular radio driver’s instability on 4.2, I decided to “evolve” the project to the 4.3 framework! But I didn’t expect to fall into this trouble…

@ BigFoot - In the CellularRadio source, can you comment out line 416 “this.NetworkSettings = this.networkInterface.NetworkInterface;” and change the base class on line 16 to be “GTM.Module” instead of “GTM.Module.NetworkModule”?

@ John - Changes done. VS2012 gives this error (see also the attached image, I’m sorry but Visual Studio is in italian language). The error says that is impossible to find a suitable method to override.
Do I need to comment also the “IsNetworkConnected” property? Or do I need to remove the “override” attribute only?
I think the second one question!

@ BigFoot - Either should work. I think the issue is that even though you are not using the network functionality of the module, since it derives from NetworkModule, some configuration occurs behind the scenes automatically eventually throwing that exception since we only support one interface at a time.

Thanks @ John, I’ve understood. Now works properly!

Just a question, in the next SDK release what will you do for this issue?
A code modification like the one you told me to do or some other solution?

@ BigFoot - Not sure yet. I’ll be looking into it.

@ BigFoot - Instead of the changes I mentioned above, if you change IsNetworkConnected to the below, does everything function? It looks like Gadgeteer calls this property internally for all network modules, active or not.


		public override bool IsNetworkConnected {
			get {
				if (this.networkInterface == null)
					return false;

				return this.networkInterface.LinkConnected;
			}
		}