Reinitialize W5100

Used device is a Domino in combination with a WIZNET Ethernet shield v.1.1 by Seeed.

The idea is to open a TCP connection and let the connection open as long the Domino is powered on. On an exception the network should be reinitialized and the connection should be opened again, just like the first time.

With the code below the connection is not recovering from an exception.
Debug info is below the code.


using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.NetworkInformation;
using GHIElectronics.NETMF.Net.Sockets;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System;
using System.Threading;

// References
// FEZDomino_GHIElectronics.NETMF.FEZ
// GHIElectronics.NETMF.W5100
// Microsoft.SPOT.Hardware
// Microsoft.SPOT.Native
// mscorlib

namespace NetworkTest
{
    public class Program
    {

        static bool _firstInit = true;
        static Socket _socket;

        public static void Main()
        {
            Debug.EnableGCMessages(true);

            while (true)
            {
                try
                {
                    Init();

                    Connect();

                    while (true)
                    {
                        // Do some tasks and keep port open.

                        Thread.Sleep(5000);

                        Debug.Print("Throwing exception");
                        throw new Exception("My exception");

                    }

                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message) ;
                    Debug.Print("Trying again in 5 seconds");
                    Thread.Sleep(5000);

                }
                finally
                {
                    Disconnect();
                }
            }
        }

        public static void Init()
        {
            if (_firstInit)
            {
                FirstInit();
                _firstInit = false;
            }
            else
            {
                ReInit();
            }
        }

        public static void FirstInit()
        {
            byte[] mac = { 40, 0, 0, 0, 254, 237 };
            byte[] ip = { 192, 168, 253, 213 };     // Static IP address of the ethernet shield
            byte[] subnet = { 255, 255, 255, 0 };   // Subment Mask
            byte[] gateway = { 192, 168, 253, 1 };  // Gateway IP address
            byte[] dns = { 192, 168, 253, 1 };      // DNS server address

            // Connect using Static IP address
            Debug.Print("FirstInit: Enabling");
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, true); // WIZnet interface on FEZ Panda
            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(dns);
            Debug.Print("FirstInit: Enabled");
        }

        public static void ReInit()
        {
            Debug.Print("ReInit: ReInitializing");
            WIZnet_W5100.ReintializeNetworking();
            Debug.Print("ReInit: ReInitialized");
        }

        protected static void Connect()
        {
            string remoteIP = "192.168.253.91";
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(remoteIP), 12000);
            Debug.Print("Connect: Connecting to " + remoteEndPoint.ToString());
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp);
            _socket.Connect(remoteEndPoint);
            Debug.Print("Connect: Connected");
        }

        protected static void Disconnect()
        {
            Debug.Print("Disconnect: Closing");
            _socket.Close();
            _socket = null;
            Debug.Print("Disconnect: Closed");
        }

    }
}

Debug output:


FirstInit: Enabling
FirstInit: Enabled
Connect: Connecting to 192.168.253.91:12000
Connect: Connected
Throwing exception
    #### Exception System.Exception - 0x00000000 (1) ####
    #### Message: My exception
    #### NetworkTest.Program::Main [IP: 002b] ####
A first chance exception of type 'System.Exception' occurred in NetworkTest.exe
My exception
Trying again in 5 seconds
Disconnect: Closing
Disconnect: Closed
ReInit: ReInitializing
ReInit: ReInitialized
Connect: Connecting to 192.168.253.91:12000
    #### Exception System.Exception - 0x00000000 (1) ####
    #### Message: Timeout occurs during connection establishment

    #### GHIElectronics.NETMF.Net.SocketNative::connect [IP: 0135] ####
    #### GHIElectronics.NETMF.Net.Sockets.Socket::Connect [IP: 002c] ####
    #### NetworkTest.Program::Connect [IP: 0030] ####
    #### NetworkTest.Program::Main [IP: 0011] ####
A first chance exception of type 'System.Exception' occurred in GHIElectronics.NETMF.W5100.dll
Timeout occurs during connection establishment

Trying again in 5 seconds
Disconnect: Closing
Disconnect: Closed
ReInit: ReInitializing
ReInit: ReInitialized
Connect: Connecting to 192.168.253.91:12000
    #### Exception System.Exception - 0x00000000 (1) ####
    #### Message: Timeout occurs during connection establishment

    #### GHIElectronics.NETMF.Net.SocketNative::connect [IP: 0135] ####
    #### GHIElectronics.NETMF.Net.Sockets.Socket::Connect [IP: 002c] ####
    #### NetworkTest.Program::Connect [IP: 0030] ####
    #### NetworkTest.Program::Main [IP: 0011] ####
A first chance exception of type 'System.Exception' occurred in GHIElectronics.NETMF.W5100.dll
Timeout occurs during connection establishment

Trying again in 5 seconds
....

And it will continue looping.

What does the WIZnet_W5100.ReintializeNetworking() command exactly do? Does it result in a short flash of the RST LED on the network board?

I’ve read on older models of the network board pin9 and the reset pin should be connected.

Is the reset pin from your W5100 connected to FEZ? To what pin?

I don’t know exactly how ReinitializeNetworking() works but if I add this after it:


            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(dns);

it works for me

The reset pin of the network board was connected with the rst pin on the Domino board (the pin below with the normal header).

I’ve modified the network board as described in [url]http://www.tinyclr.com/downloads/Shield/Broch_EthernatShield.pdf[/url].
(connecting Domino.Di9 to Network.Reset and removing the Network.Reset pin at the bottom of the network board).

Calling the WIZnet_W5100.ReintializeNetworking() will now flash the RST LED on the network board.

In addition the two lines of code from Gralin were added to get it working.
Moving the ip, subnet, gateway, mac, dns to global scope and changing the ReInit function will result to a working solution;

        
        static byte[] mac = { 40, 0, 0, 0, 254, 237 };
        static byte[] ip = { 192, 168, 253, 213 };     // Static IP address of the ethernet shield
        static byte[] subnet = { 255, 255, 255, 0 };   // Subment Mask
        static byte[] gateway = { 192, 168, 253, 1 };  // Gateway IP address
        static byte[] dns = { 192, 168, 253, 1 };      // DNS server address

        public static void FirstInit()
        {
            // Connect using Static IP address
            Debug.Print("FirstInit: Enabling");
            WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, true);
            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(dns);
            Debug.Print("FirstInit: Enabled");
        }

        public static void ReInit()
        {
            Debug.Print("ReInit: ReInitializing");
            WIZnet_W5100.ReintializeNetworking();
            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(dns);
            Debug.Print("ReInit: ReInitialized");
        }

I’ve tried it again with a network board without the hardware modification and in the end the code will work on that as well.
According to above pdf documentation the hardware modification will prevent you from pushing the reset button deploying new applications to the Domino.

Link to the board
[url]http://www.seeedstudio.com/depot/wiznet-ethernet-shield-w5100-p-518.html?cPath=102[/url]

[quote]I’ve tried it again with a network board without the hardware modification and in the end the code will work on that as well.
According to above pdf documentation the hardware modification will prevent you from pushing the reset button deploying new applications to the Domino.
[/quote]

connecting the wiznet reset pin to a GPIO (controlled byWIZnet_W5100.Enable()).is very important. even if worked without connecting the reset pin. This is a requirement made by the manufacturer of W5100 chipset.

The pdf [url]http://www.tinyclr.com/downloads/Shield/Broch_EthernatShield.pdf[/url] states it’s an optional modification, but it’s not really optional then. Thank you Joe.

[quote]The pdf http://www.tinyclr.com/downloads/Shield/Broch_EthernatShield.pdf states it’s an optional modification, but it’s not really optional then. Thank you Joe.
[/quote]

from the document

[quote]If you don’t do this hardware fix, you will have to manually reset the system (press reset button) every time you deploy a new
application.[/quote]
And this applys to reinitialize the networking in the code more than once after the hardware reset you do.