Cant get J11D work with Spider using static IP

Hi All,
I’m learning how to using spider communicate with computer through Ethernet. The code i’m using work perfectly on Cerberus and I think it’s a universe one.

   private static void InitializeNetwork()
        {
            Debug.Print("Initializing network...");
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            Debug.Print(interfaces.Length.ToString());
            if (interfaces != null && interfaces.Length > 0)
            {
                NetworkInterface networkInterface = interfaces[0];

                // dynamic IP address
                if (networkInterface.IsDhcpEnabled == false) networkInterface.EnableDhcp();
                // Wait for DHCP (on LWIP devices)
                for (int i = 0; i < 5; i++)
                {

                    networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
                    Debug.Print("Waiting for an IP Address...");
                    Thread.Sleep(1000);
                }



                if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU != 3)
                {

                    networkInterface.EnableStaticIP("169.254.0.100", "255.255.0.0", "169.254.0.1");
                    networkInterface.EnableStaticDns(new[] { "192.168.0.1" });
                }

                Debug.Print("Network ready.");
                Debug.Print("  IP Address: " + networkInterface.IPAddress);
                Debug.Print("  Subnet Mask: " + networkInterface.SubnetMask);
                Debug.Print("  Default Getway: " + networkInterface.GatewayAddress);
                Debug.Print("  DNS Server: " + networkInterface.DnsAddresses[0]);
            }
            else
            {
                Debug.Print("No network device found.");
            }
        }

it came from the codeshare in this forum. When i’m running it on my spider project, it shows no error, and showed it has got the IP address.
But when I’m ping this address 169.254.0.100, it never get response and when i’m using nbtscan to check if there any device linked in 169.254.0.1-255, nothing is found.
Anyone have an idea about that? the SDK is 4.2.
Many thanks.

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

@ pf.zcoder - Are you running 4.1 or 4.2 SDK? The code you posted is for 4.1.

Thanks a lot, I’ll do it next time!

@ Mike - I’m running it on 4.2. and this code works for cerberus in 4.2 as well~

@ andre.marschalek - Thanks! let me try it soon!

Setting up IP stack on Spider is different than Cerberus. The Spider uses the Premium library, and the Cerberus uses the OSHW.

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

@ andre.marschalek -
Hi Andre, thanks for the code but the result is not very ideal, I got a exception that is:

[quote]Using mainboard GHI Electronics FEZSpider version 1.0
#### Exception GHI.Premium.Net.NetworkInterfaceExtensionException - 0x00000000 (1) ####
#### Message:
#### GHI.Premium.Net.NetworkInterfaceExtension::Activate [IP: 001a] ####
#### GadgeteerApp1.Program::ProgramStarted [IP: 0044] ####
A first chance exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll[/quote]
Actually I find some blogs introduce how to use DHCP, but seems all of the code get the same error.

@ Architect - Hi! I just found your reply last time is empty, if you have any useful code, could you paste it again?

@ andre.marschalek -

using System;
using System.Collections;
using System.Threading;
using System.Text;
using System.Net;
using System.Net.Sockets;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using GHI.Premium.Net;

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

namespace GadgeteerApp1
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint DestinationEndPoint = new IPEndPoint(new IPAddress(new byte[] { 169, 254, 209, 61 }), 2000);
        byte[] buf = Encoding.UTF8.GetBytes("Hello World from FEZ Panda");
        void ProgramStarted()
        {
            //ethernet_J11D.UseStaticIP("169.254.0.100", "255.255.0.0", "169.254.0.1");
            //ethernet_J11D.UseDHCP();
            //ethernet_J11D.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_J11D_NetworkUp);
            //ethernet_J11D.NetworkDown += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_J11D_NetworkDown);

            

            joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);
            joystick.JoystickReleased += new Joystick.JoystickEventHandler(joystick_JoystickReleased);

            ethernet_J11D.Interface.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Interface_CableConnectivityChanged);
            ethernet_J11D.Interface.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
            ethernet_J11D.Interface.Open();
            if (NetworkInterfaceExtension.AssignedNetwrokInterface == null)
            {
                NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
            }
            else
            {
                if (NetworkInterfaceExtension.AssignedNetwrokInterface.NetworkInterface.NetworkInterfaceType != Microsoft.SPOT.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
                    NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
            }

            if (!ethernet_J11D.Interface.NetworkInterface.IsDhcpEnabled)
            {
                ethernet_J11D.Interface.NetworkInterface.EnableDhcp();
            }


            Debug.Print("Program Started");
        }
        void joystick_JoystickReleased(Joystick sender, Joystick.JoystickState state)
        {
            buf = Encoding.UTF8.GetBytes("Joystick Released" + joystick.GetJoystickPosition().X.ToString() + "   " + joystick.GetJoystickPosition().Y.ToString());
            Debug.Print("Joystick Released");
            socket.SendTo(buf, DestinationEndPoint);
        }

        void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
        {
            buf = Encoding.UTF8.GetBytes("Joystick Pressed");
            socket.SendTo(buf, DestinationEndPoint);
            Debug.Print("Joystick Pressed");
        }
        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Address Changed");
            Debug.Print("Subnet mask:" + ethernet_J11D.Interface.NetworkInterface.SubnetMask);
            Debug.Print("IP address: " + ethernet_J11D.Interface.NetworkInterface.IPAddress);
        }

        void Interface_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            Debug.Print("The cable is " + (e.IsConnected ? "CONNECTED" : "DISCONNECTED"));
        }

        void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network up.");
        }
    }
}

Hi, this is the new code. Yes, all the codes which could not work show the same message.

@ andre - I tried your code a few days ago, and there is an exception when trying to assign the stack on a device which has had the stack assigned before. This is a known problem, and should be fixed in next SDK. The solution for now is to comment out the line that is causing the exception, and to restore it after the firmware is reloaded.

1 Like

@ andre - I tried out your code, but it is breaking on the first attempt at assigning the networking stack to the interface:

 
if (NetworkInterfaceExtension.AssignedNetworkInterface == null)
{
    // breaks on this following line
    NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
}
else
{
    if (NetworkInterfaceExtension.AssignedNetworkInterface.NetworkInterface.NetworkInterfaceType != Microsoft.SPOT.Net.NetworkInformation.NetworkInterfaceType.Ethernet)
        NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
}


And here’s the error being generated:

#### Exception GHI.Premium.Net.NetworkInterfaceExtensionException - 0x00000000 (1) ####
#### Message: 
#### GHI.Premium.Net.NetworkInterfaceExtension::Activate [IP: 001a] ####
#### MF42GadgeteerApp.Network::Initialize [IP: 0053] ####
#### MF42GadgeteerApp.Program::ProgramStarted [IP: 0008] ####

A first chance exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll
An unhandled exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll

Good Lord… might I say that this J11D issue is starting to get on our “collective nerves”, isn’t it?? LoL

If you’re asking if my local network infrastructure is working, I must then say yes! It’s all working OK here: router, broadband modem, hosts are talking to each other, network shares, the works. But, when it comes to this darn little piece of hardware, nothing seems to work! : (

I got my FEZ Spider kit recentely… flashed it to NETMF 4.2… and now this… no networking.

Andre:

Did you try to set any of the network parameters using MFDeploy just before you started to get the exception with assignnetworkinterfacestackto ?

My theory is the problem using MFDeploy for network parameters has an impact this issue. But, I have not done a lot of research.

Now the Initialize method looks like this:


        public static void Initialize(Ethernet_J11D LAN)
        {
            ethernet = LAN;
            ethernet.Interface.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(CableConnectivityChanged);
            ethernet.Interface.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(NetworkAddressChanged);

            ethernet.Interface.Open();

            ethernet.Interface.NetworkInterface.EnableStaticIP("192.168.1.200", "255.255.255.0", "192.168.1.1");
            ethernet.Interface.NetworkInterface.EnableStaticDns(new string[] { "8.8.8.8" });
        }

Right now this runs without exceptions, but the overall behavior is rather strange. The NetworkAddressChanged event handler is apparently hit, since I can see the debug info that’s being printed on the Output window, but I can’t do a ping.

Using mainboard GHI Electronics FEZSpider version 1.0
IPAddress: 192.168.1.200
SubnetMask: 255.255.255.0
GatewayAddress: 192.168.1.1
PrimaryDnsServer: 8.8.8.8
The thread ‘’ (0x3) has exited with code 0 (0x0).
The program ‘[17] Micro Framework application: Managed’ has exited with code 0 (0x0).

What’s even more strange is that apparently by stepping through code (F10) someting happens that the network then works, and I’m then able to ping the board. Guess there’s some sort of race condition hidden somewhere… we need to find out how and where to maybe give the board more time to get everything in place. Not sure though.

@ andre.marschalek -
Hi, sorry to reply few days later. I have installed the latest version of firmware and SDK, at least it fixed the spell problem like “network” but not “netwrok”. The the new one, thinks that your code works, but after 10 mins, when i did some changes on it. just like @ Mike mentioned, a exception comes when it’s trying to do “NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);” I have tried Mike’s solution that comment it first, and comment it out - but only comment it works, when you comment it out, that exception comes again. Anyhow, it seems even comment that line, that code works some how - when I’m monitoring the communication using wireshark, it shows the spider is sending UDP messages, but, the Processing never revived them, I totally have no idea about it now.

@ andre.marschalek -

            ethernet_J11D.Interface.NetworkInterface.EnableStaticIP("169.254.0.100", "255.255.0.0", "169.254.0.1");
            ethernet_J11D.Interface.NetworkInterface.EnableStaticDns(new[] { "192.168.0.1" }); 

I could ping this IP using static IP like this.

Oh boy, this is some rather odd behavior I’m getting here…

At first, I was able to ping only once, and this happened only after I “stepped-through” the code with F10. Letting the code run at “full-speed” (F5) seems to circumvent someting making the network unavailable on the board… despite the fact that the NetworkAddressChanged event handler does get hit and the (correct) debug info does get printed out.

I did try what you’re suggesting and I have powered off and on the board a couple of times already but that didn’t work either… my command window was spitting out ping commands like crazy but they never got back.

But… and this is a huge BUT… while I was writing this reply back to you, the ping commands started coming back. Isn’t this just absurdly strange? It took like minutes and minutes for the board to to become alive??

I’m in the uttter dark here.

@ andre.marschalek -
Yes, I use ping to test if the interface is opened and connection built. It works.
I use UDP message to send the joystick’s press, release event and position from Spider to the PC and using a Processing sketch to read it. The package capture software wireshark found this UDP message but Processing didn’t read them.