G120HDR - ENC28 Module Issues

Hello,
I have problems with ENC28 Module. Almost every attempt to communicate over the network fails. Before running Http server I am trying to get time from NTP server which fails. I am using newest NETMF SDK with Visual Studio 2013. Could you help me please? It si possible that there is some HW issue? Thanks!

My code looks like this:


eth = new GHI.Networking.EthernetENC28J60(SPI.SPI_module.SPI2,
            G120.P1_17, // chip select
            G120.P0_5,  // external interrupt
            G120.P1_14  // reset
            );
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
            eth.EnableStaticIP("192.168.1.100", "255.255.255.0", "192.168.1.1");
            eth.EnableStaticDns(new string[] { "192.168.1.1" });
            //eth.EnableDhcp();
            //eth.EnableDynamicDns();
            eth.Open();

static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            if (eth.Opened)
                WriteMessageToLog(Captions.Info, GetActDate(), "Ethernet interface is opened and assigned to TCP/IP stack.");
            else
                WriteMessageToLog(Captions.Info, GetActDate(), "Ethernet interface is closed. TCP/IP stack is not assigned.");

            if (HasNetwork = e.IsAvailable)
            {
                WriteMessageToLog(Captions.Info, GetActDate(), "Network available.");
            }
            else
            {
                WriteMessageToLog(Captions.Error, GetActDate(), "Network is not available!");
                if (HttpServer != null)
                {
                    HttpServer.SuspendServer = true;
                }
            }
        }
        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {

            if (HasIpAddress = eth.IPAddress != "0.0.0.0")
            {
                WriteMessageToLog(Captions.Info, GetActDate(), "Network address changed. New address is = " + eth.IPAddress + ".");
                try
                {
                    getNetworkTime();                   //zda se, ze je sit dostupna, nastavime cas
                    if (HttpServer == null)
                    {
                        HttpServer = new HTTPServer(80, requestHandler);
                    }
                    HttpServer.SuspendServer = false;
                }
                catch (Exception)
                {
                    WriteMessageToLog(Captions.Error, GetActDate(), "Can't start http server.");
                }
            }
            else
            {
                WriteMessageToLog(Captions.Error, GetActDate(), "No IP address!");
            }
        }

Output:
Info: SD Card mounted.
Info: Ethernet interface is opened and assigned to TCP/IP stack.
Info: Network available.
Info: Network address changed. New address is = 192.168.1.100.
Error: Waiting for CAN bus… Available messages: 0.
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (3) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
#### System.Net.Sockets.Socket::Receive [IP: 0018] ####
#### System.Net.Sockets.Socket::Receive [IP: 0010] ####
#### MF_FVE.NtpClient::GetNetworkTime [IP: 0046] ####
#### MF_FVE.NtpClient::GetNetworkTime [IP: 003b] ####
#### MF_FVE.Program::getNetworkTime [IP: 001c] ####
#### MF_FVE.Program::NetworkChange_NetworkAddressChanged [IP: 0034] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkAddressChangedEventHandler::Invoke [IP: a0e7ff3c] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange::OnNetworkChangeCallback [IP: 0040] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange+NetworkChangeListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10060
#### SocketException ErrorCode = 10060
Error: NTP time synchronization failed.

are you able to ping the board?

you say almost every attempt to communicate fails. when does it succeed?

if you do not try to set the time, does the http server start?

10060 means that there was a timeout connecting to ntp server. is the ntp server address correct?

Ping says: “Destination host unreachable”.
Code which i have was running several months. Sometimes I had had to restart device but overall reliability was good. Recently I noticed that http server not respond, so I had to solve this issue. Sometimes restart helped, sometimes not. Cables are good, router si new… I decided to upgrade firmware and my software built on newest .NETMF SDK. Now, situation is (maybe) even worse. Even ten reboots do not bring Sockets to work. If I retrieve NTP time, it is sure that http server comes to live too. It seems that something is wrong with drivers or HW. (?) I had had some problems with Sockets half year ago also. That time I concluded that it was my fault. As regards the method asking for the NTP time, it throws exception at “GetHostEntry()”. (Sorry, English is not my native language)


/// <summary>
    /// Static class to receive the time from a NTP server.
    /// </summary>
    public class NtpClient
    {
        /// <summary>
        /// Gets the current DateTime from time-a.nist.gov.
        /// </summary>
        /// <returns>A DateTime containing the current time.</returns>
        public static DateTime GetNetworkTime()
        {
            return GetNetworkTime("time-a.nist.gov");
        }

        /// <summary>
        /// Gets the current DateTime from <paramref name="ntpServer"/>.
        /// </summary>
        /// <param name="ntpServer">The hostname of the NTP server.</param>
        /// <returns>A DateTime containing the current time.</returns>
        public static DateTime GetNetworkTime(string ntpServer)
        {
            IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;

            if (address == null || address.Length == 0)
                throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");

            IPEndPoint ep = new IPEndPoint(address[0], 123);

            return GetNetworkTime(ep);
        }

        /// <summary>
        /// Gets the current DateTime form <paramref name="ep"/> IPEndPoint.
        /// </summary>
        /// <param name="ep">The IPEndPoint to connect to.</param>
        /// <returns>A DateTime containing the current time.</returns>
        public static DateTime GetNetworkTime(IPEndPoint ep)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
            s.ReceiveTimeout = 1000;
            s.Connect(ep);

            byte[] ntpData = new byte[48]; // RFC 2030 
            ntpData[0] = 0x1B;
            for (int i = 1; i < 48; i++)
                ntpData[i] = 0;

            s.Send(ntpData);
            s.Receive(ntpData);

            byte offsetTransmitTime = 40;
            ulong intpart = 0;
            ulong fractpart = 0;

            for (int i = 0; i <= 3; i++)
                intpart = 256 * intpart + ntpData[offsetTransmitTime + i];

            for (int i = 4; i <= 7; i++)
                fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];

            ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
            s.Close();

            TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);

            DateTime dateTime = new DateTime(1900, 1, 1);
            dateTime += timeSpan;

            TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;
        }
    }

@ Adam - you said “router is new…” so it probably means before you changed router everything was OK?

Before changing router it was bad too. Sometimes it worked sometimes not.
OK, I have created simple test program to check ping response.

Test program:


 public class Program
    {
        public static void Main()
        {
            EthernetENC28J60 eth = new GHI.Networking.EthernetENC28J60(SPI.SPI_module.SPI2,
            G120.P1_17, // chip select
            G120.P0_5,  // external interrupt
            G120.P1_14  // reset
            );

            eth.EnableStaticIP("192.168.1.100", "255.255.255.0", "192.168.1.1");
            eth.EnableStaticDns(new string[] { "192.168.1.1" });
            //eth.EnableDhcp();
            //eth.EnableDynamicDns();
            eth.Open();

            while (eth.IPAddress == "0.0.0.0")
            {
                Debug.Print("Waiting for DHCP");
                Thread.Sleep(250);
            }

            Debug.Print("Eth OK - " + eth.IPAddress);

            while (true)
            {
                Thread.Sleep(500);
            }
        }
    }

After some manipulation with board I have got what I needed. :clap: Response within 1ms. It must be some cold joint! Unfortunately I don’t know where. Maybe on module, maybe on Gadgeteer cable…

It looks like this:
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=1872ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=2114ms TTL=255
Reply from 192.168.1.100: bytes=32 time=32ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1460ms TTL=255
Request timed out.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=2919ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=2701ms TTL=255
Reply from 192.168.1.100: bytes=32 time=2ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=317ms TTL=255
Reply from 192.168.1.100: bytes=32 time=313ms TTL=255
Reply from 192.168.1.100: bytes=32 time=159ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.100: bytes=32 time=698ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=650ms TTL=255
Request timed out.
Reply from 192.168.1.100: bytes=32 time=1366ms TTL=255
Reply from 192.168.1.100: bytes=32 time=313ms TTL=255
Reply from 192.168.1.100: bytes=32 time=573ms TTL=255
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Request timed out.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Reply from 192.168.1.2: Destination host unreachable.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=3ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=233ms TTL=255
Reply from 192.168.1.100: bytes=32 time=200ms TTL=255
Reply from 192.168.1.100: bytes=32 time=100ms TTL=255
Reply from 192.168.1.100: bytes=32 time=76ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=2567ms TTL=255
Reply from 192.168.1.100: bytes=32 time=363ms TTL=255
Reply from 192.168.1.100: bytes=32 time=158ms TTL=255
Reply from 192.168.1.100: bytes=32 time=138ms TTL=255
Reply from 192.168.1.100: bytes=32 time=6ms TTL=255
Reply from 192.168.1.100: bytes=32 time=319ms TTL=255
Reply from 192.168.1.100: bytes=32 time=109ms TTL=255
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=227ms TTL=255
Request timed out.
Request timed out.
Reply from 192.168.1.100: bytes=32 time=932ms TTL=255
Reply from 192.168.1.100: bytes=32 time=2234ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1544ms TTL=255
Reply from 192.168.1.100: bytes=32 time=17ms TTL=255
Reply from 192.168.1.100: bytes=32 time=590ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255
Reply from 192.168.1.100: bytes=32 time=1ms TTL=255

make sure each GHI board has its own MAC address. If you are using more than one GHI board, you might have a duplicate MAC address conflict/