FEZ Raptor + ENC28 ethernet, problem with local endpoint/network stack

I’m trying to get some network stuff up and running on the Raptor.

Seems no matter what I do, .NET/libraries keeps using the first interface when it does network stuff. Where the first interface available is the internal on-chip stuff that’s not available on the Raptor.

The second is the ENC28 and 3th is WiFi from what I can find out.

I want the network stack to use the ENC28 as my “default” network adapter so stuff that uses network and assume/take the default/first adapter could work. Seems like it always picks the 1st adapter right now - while the 2nd (Ethernet module) is the one I want it to use.

When debugging, the socket (or other network related stuff I’ve been testing) has local endpoint set to 0.0.0.0:0

Could anyone point me in the right direction?

Program.cs

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 Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Text;
using Gadgeteer;

using Microsoft.SPOT.Net.NetworkInformation;
using GHI.Premium.Net;
using Gadgeteer.Modules.Seeed;
using System.Net;
using Microsoft.SPOT.Hardware;
using System.Net.Sockets;


namespace GadgeteerApp1
{
    public partial class Program
    {
        // static EthernetENC28J60 Eth1;
        // ********** G400 Raptor socket 1
        // public static EthernetENC28J60 Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Hardware.G400.Pin.PB5, GHI.Hardware.G400.Pin.PB0, GHI.Hardware.G400.Pin.PA7, 4000);
        // public static Ethernet_ENC28 Eth1 = new Ethernet_ENC28(1);

        public static ManualResetEvent NetworkAvailablityBlocking = null;

        static string _repeaterIp = "10.0.0.216";
        static string _boardIp = "10.0.0.217";
        static string _boardNetmask = "255.255.255.0";
        static string _boardGw = "10.0.0.2";

        void ProgramStarted()
        {
            var timer = new GT.Timer(500);
            timer.Tick += timer_Tick;
            timer.Start();

            Thread initThread = new Thread(new ThreadStart(InitDevices));

            initThread.Start();

            Debug.Print("Program Started");
        }

        // private static WebServer webServer;

        System.Net.Sockets.Socket socket_30003;

        private void InitDevices()
        {
            StartEthernet();

            EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(_repeaterIp), 30003);
            EndPoint localEndPoint  = new IPEndPoint(IPAddress.Parse(_boardIp), 30003);
            
            socket_30003 = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket_30003.Bind(remoteEndPoint);

            while (true)
            {
                if (socket_30003.Poll(-1, SelectMode.SelectRead))
                {
                    byte[] inBuf = new byte[socket_30003.Available];
                    int count = socket_30003.ReceiveFrom(inBuf, ref remoteEndPoint);
                    
                    Debug.Print(new String(Encoding.UTF8.GetChars(inBuf)));
                }

            }
        }




        // public static void StartEthernet(EthernetENC28J60 eth)
        void StartEthernet()
        {
            // Eth1.Open();
            ethernet_ENC28.Interface.Open();

            // NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);
            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);

            // Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            // Eth1.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            ethernet_ENC28.Interface.CableConnectivityChanged += Interface_CableConnectivityChanged;

            // Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
            ethernet_ENC28.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;
            
            // Eth1.NetworkInterface.EnableStaticIP(_boardIp, _boardNetmask, _boardGw); // class C net
            string[] DnsAddresses = new string[] { "10.0.0.2", "8.8.8.8" }; //Test so hardwired in code
            
            // Eth1.NetworkInterface.EnableStaticDns(DnsAddresses);
            ethernet_ENC28.Interface.NetworkInterface.EnableStaticDns(DnsAddresses);

            // Eth1.NetworkInterface.PhysicalAddress = new byte[] { 0x00, 0x21, 0x03, 0x00, 0x00, 0x0B }; // test MAC
            ethernet_ENC28.Interface.NetworkInterface.PhysicalAddress = new byte[] { 0x00, 0x21, 0x03, 0x00, 0x00, 0x0B }; // test MAC

            // if (!Eth1.IsCableConnected)
            if (!ethernet_ENC28.Interface.IsCableConnected)
            {
                NetworkAvailablityBlocking = new ManualResetEvent(false);
                do
                {
                    // if (!Eth1.IsCableConnected)
                    if (!ethernet_ENC28.Interface.IsCableConnected)
                    {
                        Debug.Print("Ethernet cable is not connected yet.");
                    }
                    else
                        break;
                }
                while (!NetworkAvailablityBlocking.WaitOne(5000, false));
            }

            while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
            {
                Debug.Print("IP address is not set yet.");
            }
            Debug.Print("IP address is set");
        }

        void Interface_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
        // static void Eth1_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
        //static void Eth1_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            Debug.Print("Built-in Ethernet Cable is " + (e.IsConnected ? "Connected!" : "Disconnected!"));

            if (e.IsConnected)
            {
                if (NetworkAvailablityBlocking != null)
                    NetworkAvailablityBlocking.Set();
                // restart tcp listner thread
                //StartEthernet();
                Thread.Sleep(5000);
                PowerState.RebootDevice(false);
                //SMSHttpListenerThread = new Thread(new ThreadStart(TcpThreadManager));
                //SMSHttpListenerThread.Start();
            }
        }

        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        // static void Eth1_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("New address for The built-in Ethernet Network Interface ");

            Debug.Print("Is DhCp enabled: " + ethernet_ENC28.Interface.NetworkInterface.IsDhcpEnabled); // Eth1.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Is DynamicDnsEnabled enabled: " + ethernet_ENC28.Interface.NetworkInterface.IsDynamicDnsEnabled); // Eth1.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType " + ethernet_ENC28.Interface.NetworkInterface.NetworkInterfaceType); // Eth1.NetworkInterface.NetworkInterfaceType);
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + ethernet_ENC28.Interface.NetworkInterface.IPAddress); // Eth1.NetworkInterface.IPAddress);
            Debug.Print("Subnet Mask: " + ethernet_ENC28.Interface.NetworkInterface.SubnetMask); // Eth1.NetworkInterface.SubnetMask);
            Debug.Print("Default Getway: " + ethernet_ENC28.Interface.NetworkInterface.GatewayAddress); // Eth1.NetworkInterface.GatewayAddress);
            Debug.Print("Number of DNS servers:" + ethernet_ENC28.Interface.NetworkInterface.DnsAddresses.Length); // Eth1.NetworkInterface.DnsAddresses.Length);

            // for (int i = 0; i < Eth1.NetworkInterface.DnsAddresses.Length; i++)
                // Debug.Print("DNS Server " + i.ToString() + ":" + Eth1.NetworkInterface.DnsAddresses[i]);

            for (int i = 0; i < ethernet_ENC28.Interface.NetworkInterface.DnsAddresses.Length; i++)
                Debug.Print("DNS Server " + i.ToString() + ":" + ethernet_ENC28.Interface.NetworkInterface.DnsAddresses[i]);

            Debug.Print("----------------------------------------------");
        }



        private void NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Network Address Changed");
        }

        void timer_Tick(GT.Timer timer)
        {
            Mainboard.SetDebugLED(true);
            Thread.Sleep(50);
            Mainboard.SetDebugLED(false);
        }

        

    }
}

Program.generated.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace GadgeteerApp1 {
    using Gadgeteer;
    using GTM = Gadgeteer.Modules;
    
    
    public partial class Program : Gadgeteer.Program {
        
        /// <summary>The UsbClientDP module using socket 8 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.UsbClientDP usbClientDP;
        
        /// <summary>The CellularRadio module using socket 4 of the mainboard.</summary>
        private Gadgeteer.Modules.Seeed.CellularRadio cellular;
        
        /// <summary>The SDCard module using socket 9 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.SDCard sdCard;
        
        /// <summary>The Ethernet_ENC28  (Premium) module using socket 1 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.Ethernet_ENC28 ethernet_ENC28;
        
        /// <summary>This property provides access to the Mainboard API. This is normally not necessary for an end user program.</summary>
        protected new static GHIElectronics.Gadgeteer.FEZRaptor Mainboard {
            get {
                return ((GHIElectronics.Gadgeteer.FEZRaptor)(Gadgeteer.Program.Mainboard));
            }
            set {
                Gadgeteer.Program.Mainboard = value;
            }
        }
        
        /// <summary>This method runs automatically when the device is powered, and calls ProgramStarted.</summary>
        public static void Main() {
            // Important to initialize the Mainboard first
            Program.Mainboard = new GHIElectronics.Gadgeteer.FEZRaptor();
            Program p = new Program();
            p.InitializeModules();
            p.ProgramStarted();
            // Starts Dispatcher
            p.Run();
        }
        
        private void InitializeModules() {
            this.usbClientDP = new GTM.GHIElectronics.UsbClientDP(8);
            this.cellular = new GTM.Seeed.CellularRadio(4);
            this.sdCard = new GTM.GHIElectronics.SDCard(9);
            this.ethernet_ENC28 = new GTM.GHIElectronics.Ethernet_ENC28(1);
        }
    }
}

I’ve been searching the forums some more now - and it seems to be several people that have problems with Raptor + ENC28 + UDP.

Are there known issues with these?

None that we confirmed.

Ok, thanks Gus.

Strange is, I’ve rewritten the same code 3-4 times, different ways, and still can’t get the UDP listeners up and running.

Are there no sample projects on using the ENC28 with the Raptor? This should be a walk in the park, but still seems to be a hassle to get working.

Here is another topic, with same HW, that also have the same problem - local endpoint IP is 0.0.0.0:0 when you look at it in the debugger - after the code throws an exception when you try to bind the UDP socket

https://www.ghielectronics.com/community/forum/topic?id=14986

This post also describes issues/trouble with raptor + enc28 + UDP

https://www.ghielectronics.com/community/forum/topic?id=14715

We will test on our end

1 Like

@ Gus - thanx :slight_smile:

@ danibjor

I don’t see a call to EnableStaticIP or EnableDHCP in your code. Does it still fail if you remove all other modules from the designer and add either of those calls?

1 Like

Totally my bad. I set the socket to bind to the remote IP, not the local (followed the code in another topic here on the forum - whos also failed…). Well, that explains why neither my nor the other guy’s code would work… :-[