Main Site Documentation

Ethernet


#1

I am trying to get ENC28 1.1 working.

It blows up when I do the listenSocket.Bind(ep);

I also cannot get an IP address, but also tried to bind statically.

I tested the cable on another computer and got an IP address fine.

Stack Trace

Found debugger!

Create TS.

Loading start at a0e00000, end a0e1383c

Assembly: mscorlib (4.2.0.0) Assembly: Microsoft.SPOT.Native (4.2.0.0) Assembly: Microsoft.SPOT.Security.PKCS11 (4.2
.0.0) Assembly: System.Security (4.2.0.0) Loading Deployment Assemblies.

Attaching deployed file.

Assembly: Microsoft.SPOT.IO (4.2.0.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0) Attaching deployed file.

Assembly: Gadgeteer (2.42.0.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Graphics (4.2.0.0) Attaching deployed file.

Assembly: Microsoft.SPOT.TinyCore (4.2.0.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Hardware (4.2.0.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Net.Security (4.2.0.0) Attaching deployed file.

Assembly: GTM.GHIElectronics.Ethernet_ENC28 (1.1.3.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Touch (4.2.0.0) Attaching deployed file.

Assembly: System.Http (4.2.0.0) Attaching deployed file.

Assembly: System (4.2.0.0) Attaching deployed file.

Assembly: System.Net.Security (4.2.0.0) Attaching deployed file.

Assembly: GHI.Premium.Net (4.2.7.0) Attaching deployed file.

Assembly: AirStream (1.0.0.0) Attaching deployed file.

Assembly: System.IO (4.2.0.0) Attaching deployed file.

Assembly: Gadgeteer.SPI (2.42.0.0) Attaching deployed file.

Assembly: GHI.Premium.IO (4.2.7.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Net (4.2.0.0) Attaching deployed file.

Assembly: GHI.Premium.Hardware (4.2.7.0) Attaching deployed file.

Assembly: GHI.Premium.System (4.2.7.0) Attaching deployed file.

Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1) Attaching deployed file.

Assembly: GHIElectronics.Gadgeteer.FEZSpider (1.1.1.0) Resolving.

GC: 1msec 37296 bytes used, 7302372 bytes available

Type 0F (STRING ): 24 bytes

Type 15 (FREEBLOCK ): 7302372 bytes

Type 17 (ASSEMBLY ): 33156 bytes

Type 1E (BINARY_BLOB_HEAD ): 4044 bytes

Type 34 (APPDOMAIN_HEAD ): 72 bytes

GC: performing heap compaction…

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Native.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Security.PKCS11.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Security.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.IO.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Graphics.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.TinyCore.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.IO.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\Microsoft .NET Gadgeteer\Core\Assemblies.NET Micro Framework 4.2\le\Gadgeteer.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.Security.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI Premium NETMF v4.2 SDK\Assemblies\le\GHI.Premium.Net.dll’
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\Microsoft .NET Gadgeteer\Core\Assemblies.NET Micro Framework 4.2\le\Gadgeteer.SPI.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\Ethernet_ENC28\NETMF 4.2\le\GTM.GHIElectronics.Ethernet_ENC28.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Touch.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Net.Security.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.Http.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI Premium NETMF v4.2 SDK\Assemblies\le\GHI.Premium.System.dll’
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI Premium NETMF v4.2 SDK\Assemblies\le\GHI.Premium.IO.dll’
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI Premium NETMF v4.2 SDK\Assemblies\le\GHI.Premium.Hardware.dll’
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Mainboards\FEZSpider\NETMF 4.2\le\GHIElectronics.Gadgeteer.FEZSpider.dll’, Symbols loaded.
‘Microsoft.SPOT.Debugger.CorDebug.dll’ (Managed): Loaded ‘G:\Users\mweel\documents\visual studio 2010\Projects\AirStream\AirStream\bin\Debug\le\AirStream.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZSpider version 1.0
Step into: Stepping over non-user code 'Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces’
Step into: Stepping over non-user code 'Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces’
Step into: Stepping over non-user code 'Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces’
Step into: Stepping over non-user code 'Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces’
Step into: Stepping over non-user code 'Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces’
WARN: Total initialization time exceeds 10 seconds.
: ProgramStarted is blocking execution, which means events and timers will not run properly.
: Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
WARN: Total initialization time exceeds 20 seconds.
: ProgramStarted is blocking execution, which means events and timers will not run properly.
: Make sure not to use blocking code such as while(true) - use a GT.Timer instead.
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### AirStream.Program::ProgramStarted [IP: 0051] ####
#### AirStream.Program::Main [IP: 0015] ####
#### SocketException ErrorCode = 10022
#### SocketException ErrorCode = 10022
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10022
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

#### SocketException ErrorCode = 10022

Uncaught exception

Any help would be much appreciated.

Thank you,
Marty


 // First, make sure we actually have a network interface to work with!
            if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1)
            {
                Debug.Print("No Active network interfaces. Bombing out.");
                Thread.CurrentThread.Abort();
            }

            // OK, retrieve the network interface
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];

             //If DHCP is not enabled, then enable it and get an IP address, else renew the lease. Most iof us have a DHCP server
             //on a network, even at home (in the form of an internet modem or wifi router). If you want to use a static IP
             //then comment out the following code in the "DHCP" region and uncomment the code in the "fixed IP" region.
            #region DHCP Code
            //if (NI.IsDhcpEnabled == false)
            //{
            //    Debug.Print("Enabling DHCP.");
            //    NI.EnableDhcp();
            //    Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            //}
            //else
            //{
            //    Debug.Print("Renewing DHCP lease.");
            //    NI.RenewDhcpLease();
            //    Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            //}
            #endregion

            //#region Static IP code
            //// Uncomment the following line if you want to use a static IP address, and comment out the DHCP code region above
                NI.EnableStaticIP("10.0.1.56", "255.255.255.0", "10.0.1.1");
            //#endregion

            // Create the socket            
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the listening socket to the port
            IPAddress hostIP = IPAddress.Parse(NI.IPAddress);
            IPEndPoint ep = new IPEndPoint(hostIP, 80);
            listenSocket.Bind(ep);

            // Start listening
            listenSocket.Listen(1);

            // Main thread loop
            while (true)
            {
                try
                {
                    Debug.Print("listening...");
                    Socket newSock = listenSocket.Accept();
                    Debug.Print("Accepted a connection from " + newSock.RemoteEndPoint.ToString());
                    byte[] messageBytes = Encoding.UTF8.GetBytes("Hello, browser! I think the time is " + DateTime.Now.ToString());
                    newSock.Send(messageBytes);
                    newSock.Close();
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }


#2

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)

#3

Of course it will “blow up” when you bind to a socket, if you can’t get an IP address !

You need to tell us what device and what firmware version; there’s lots of issues with the 4.2 firmware, but at least knowing what device we’re talking about will help


#4

I have not done anything with an ENC28 yet. Does a MAC address need to be saved in the network configuration, or at execution time?


#5

@ Brett I tried to bind one statically also. The device is a ENC28 1.1. Not sure where to find the firmware information for you, or for what?

This is the main board:
http://www.ghielectronics.com/catalog/product/297

@ Mike I assume that question is for the group not me. I did nothing to set a MAC address on ethernet device.

Thanks fellas.

Marty


#6

Usually the spider board uses a J11D for Ethernet. The code you are using is for an OSHW device. The Spider is a premium device, which has a different initialization procedure.

This thread has sample code you could modify for ENC28. http://www.tinyclr.com/forum/topic?id=10446&page=2

You want to use the EthernetENC28J60 class.


#7

I also have the J11D, are you saying my code should work OK with that?

Thanks,
Marty


#8

Andre,

That did it.

Seems like there are a lot of ways to handle networking differently depending on the hardware.

Does anyone have any documentation on how that works and what the different classes might be?

Thanks,
Marty


#9

@ andre.m

Hi there. I’m Wondering do I have the right Ethernet_J11D library (mine is showing as v4.0.30319 in VS) ? I can match your coded solution up until.

ethernet_J11D.UseStaticIP(ip, mask, gw);

but I only have the following method call which comes close to this code (my Ethernet_J11D module is called ethernet)

ethernet.Interface.NetworkInterface.EnableStaticIP(ip,mask,gw);

and when the Interface_NetworkAddressChanged(object sender, EventArgs e) event fires I get a 0.0.0.0 ip address.

I am using .netmf 4.2 with appropriate updated firmware on a FEZ spider (4.2.7.0).

I have managed to assign a static address to the FEZ spider using the .netmf low level reference

NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

However I cannot get DHCP to work with any method with a Ethernet_J11D module. Is it possible to get DHCP running on a FEZ spider?


#10

@ andre.m.

Apologies.

Correction the ip address is set when the event fires. But I’m still wondering about the difference in methods and if DHCP is possible?

Thank you.


#11

looks like i’m answering my own posts here :slight_smile: . I Have got DHCP to configure with this call

ethernet.Interface.NetworkInterface.EnableDhcp();

and ip, sub mask and Gateway are right.

Now just wondering about the difference in the method calls before I go deeper into writing code against network functionality. There seems to be a few different versions of the Ethernet_J11D dll floating around inheriting from different classes?


#12

As part of the 4.2 Premium SDK, the process for initializing the J11D module changed. This was done to allow multiple IP hardware interfaces to
exist on a device, for example hardwire and Wifi. At the present time, only one IP device can be active, but they can be switch
during the running of a program.


#13

@ Mike

Thanks for that Mike.

So just to clarify. Is ethernet.UseDHCP() a .netmf 4.1 method and the new equivalent in 4.2 is ethernet.Interface.NetworkInterface.EnableDhcp();?

Thanks again.


#14

The SDK comes out this week adds the gadgeteer Ethernet wrappers back, just like 4.1


#15

I’ve noticed that nobody sets the mac-address from code. Does this mean that you all expect it to be present from the config.hex and mfdeploy tool?


#16

Ok, but when the device (for example fez-spider) is being updated (IFU or MFDeploy) will the mac-address overwritten?


#17

Since an erase of the device is done, during firmware installation, I would assume that the MAC address is erased and replaced with the default address.

I often set the MAC address in the code.


#18

I also set the MAC address in code, it is stored on a sd card. But lately I read a couple of older threads on the forum about the dhcp issues again and I noticed that I never saw setting the mac address in the provided sample code. The MAC address is essential for a DHCP server, so I was thinking if that could be a source of problems?


#19

Here is a network class that works for me.

using System.Text;

using GHI.Premium.Net;
using Microsoft.SPOT;
using GTM = Gadgeteer.Modules;
namespace NtpTesting.Network_J11D
{
    class Network
    {
        public event EventHandler CableChanged;
        public event EventHandler NetworkChanged;

        public bool DHCPEnabled { get; set; }
        public string Ip { get; set; }
        public string NetMask { get; set; }
        public string Gateway { get; set; }
        public string[] DNS { get; set; }
        public bool NetworkUp { get; set; }

        public Gadgeteer.Modules.GHIElectronics.Ethernet_J11D _ether;
        /// <summary>
        /// Start Ethernet using DHCP
        /// </summary>
        /// <param name="ethernet_J11D"></param>
        public Network(Gadgeteer.Modules.GHIElectronics.Ethernet_J11D ethernet_J11D)
        {
            this.DHCPEnabled = true;
            _ether = ethernet_J11D;
            startnet();
        }
        /// <summary>
        /// Start Ethernet using static IP
        /// </summary>
        /// <param name="ethernet_J11D"></param>
        /// <param name="ip">"10.2.1.1"</param>
        /// <param name="netmask">"255.255.255.0"</param>
        /// <param name="gateway">"10.2.1.254"</param>
        /// <param name="dns">new string[] {"10.2.1.67","10.2.1.68"}</param>
        public Network(Gadgeteer.Modules.GHIElectronics.Ethernet_J11D ethernet_J11D, string ip, string netmask, string gateway, string[] dns) 
        {
            this.Ip = ip;
            this.NetMask = netmask;
            this.Gateway = gateway;
            this.DNS = dns;

            this.DHCPEnabled = false;

            startnet();
        }

        void startnet()
        {
            //byte[] newMac = new byte[] { 0x00, 0x21, 0x03, 0x80, 0x53, 0x7C };

            _ether.Interface.Open();

            NetworkInterfaceExtension.AssignNetworkingStackTo(_ether.Interface);
            //(NetworkInterface.GetAllNetworkInterfaces())[0].PhysicalAddress = newMac;

            _ether.NetworkUp += OnNetworkUp;
            _ether.NetworkDown += OnNetworkDown;

            _ether.Interface.CableConnectivityChanged +=
                new EthernetBuiltIn.CableConnectivityChangedEventHandler(Interface_CableConnectivityChanged);


            if (_ether.Interface.IsCableConnected)
                SetupEthernet();
        }
        void Interface_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            
            // Invoke the Changed event; called whenever cable connectivity changes:
            if (CableChanged != null)
                CableChanged(this, e);
            
        }

        void OnNetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {

            MyEventArgs e = new MyEventArgs();

            e.IsUp = state == GTM.Module.NetworkModule.NetworkState.Up;
            this.NetworkUp = e.IsUp;

            if (NetworkChanged != null)
                NetworkChanged(sender, e);
        }

        
        void OnNetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            
            MyEventArgs e = new MyEventArgs();

            e.IsUp = state == GTM.Module.NetworkModule.NetworkState.Up;
            this.NetworkUp = e.IsUp;

            if (NetworkChanged != null)
                NetworkChanged(sender, e);
        }

        public void SetupEthernet()
        {
            if (this.DHCPEnabled)
                _ether.UseDHCP();
            else
                _ether.UseStaticIP(
                    this.Ip, //"10.2.1.10",            
                    this.NetMask, //"255.255.255.0",            
                    this.Gateway, //"10.2.1.63",
                    this.DNS);
        }

        public string ListNetworkInterfaces()
        {
            StringBuilder rstring = new StringBuilder();

            var settings = _ether.NetworkSettings;
            
            rstring.Append("------------------------------------------------\r");
            rstring.Append("MAC: " + ByteExtensions.ToHexString(settings.PhysicalAddress, "-") + "\r");
            rstring.Append("IP Address:   " + settings.IPAddress + "\r");
            rstring.Append("DHCP Enabled: " + settings.IsDhcpEnabled + "\r");
            rstring.Append("Subnet Mask:  " + settings.SubnetMask + "\r");
            rstring.Append("Gateway:      " + settings.GatewayAddress + "\r");
            for (int i = 0; i < settings.DnsAddresses.Length; i++)
            {
                rstring.Append("DNS:          " + settings.DnsAddresses[i] + "\r");
            }
            rstring.Append("------------------------------------------------" + "\r");

            return rstring.ToString();
        }
        
        
    }
    public class MyEventArgs : EventArgs
    {
        public bool IsUp { get; set; }
        
    }

}



#20

A unique MAC address is essential for all devices on the same network, regardless of DHCP!