G400D - Built In Ethernet Locks Up Board

Using the code below on a G400D v1.3 will cause the module to lock up after the third connection attempt, it has to be reset in order for it to respond again. No exception or debug output is sent out.

The MySocket section is taken from this post https://www.ghielectronics.com/community/forum/topic?id=12496&page=5#msg132262 as a work around for the connection hang issue in MF 4.2.


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;

using GHI.Premium.Net;

namespace MFConsoleApplication1
{
   public class Program
   {

      static EthernetBuiltIn eNet = new EthernetBuiltIn();
      static int ConnectTryCount = 0;

      public static void Main()
      {
         eNet.Open();
         NetworkInterfaceExtension.AssignNetworkingStackTo(eNet);

         eNet.NetworkInterface.EnableStaticIP("10.0.26.18", "255.255.255.0", "0.0.0.0");
         eNet.NetworkInterface.EnableStaticDns(new string[] { "0.0.0.0", "0.0.0.0" });

         Debug.Print("IP Address: " + eNet.NetworkInterface.IPAddress);
         Debug.Print("Subnet Mask: " + eNet.NetworkInterface.SubnetMask);
         Debug.Print("Gateway: " + eNet.NetworkInterface.GatewayAddress);

         IPAddress RemoteIPAdrs = IPAddress.Parse("10.0.26.1");
         IPEndPoint mRemoteEndPoint = new IPEndPoint(RemoteIPAdrs, 14220);
         bool IsConnected = false;

         do
         {
            MySocket mySock = new MySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySock.TimeOutConnection = 10 * 1000;
            ConnectTryCount++;
            Debug.Print("Connect Try #: " + ConnectTryCount.ToString() + " - " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
            if (mySock.Connect(mRemoteEndPoint))
            {
               IsConnected = true; //mSocket = mySock.GetSocket;
            }
            else
            {
               mySock = null;
               IsConnected = false;
            }
         } while (!IsConnected);


         System.Threading.Thread.Sleep(-1);
      }

   }

   public class MySocket
   {
      private Socket _socket;
      private Thread _myThread;
      private EndPoint _remoteEP;
      private Boolean isSocketConnected = false;
      private int _connectTimeOut = 15000; // 15 second


      public MySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
      {
         _socket = new Socket(addressFamily, socketType, protocolType);
      }
      ~MySocket()
      {
         // Destroy
      }
      public int TimeOutConnection
      {
         get { return _connectTimeOut; }
         set { _connectTimeOut = value; }
      }
      public Socket GetSocket
      {
         get { return _socket; }
      }
      private void CancelRequestConnect()
      {
         if (_myThread != null)
         {
            try
            {
               if (_myThread.IsAlive || _myThread.ThreadState == ThreadState.Running)
               {
                  try
                  {
                     _myThread.Abort();
                  }
                  catch
                  {

                  }
               }
               _myThread = null;
            }
            catch
            {
            }
         }
      }
      public Boolean Connect(EndPoint remoteEP)
      {
         CancelRequestConnect();
         _remoteEP = remoteEP;
         isSocketConnected = false;
         _myThread = new Thread(Connect);
         _myThread.Start();
         int timeout = _connectTimeOut;
         while (timeout > 0)
         {
            Thread.Sleep(10);
            timeout -= 10;
            if (isSocketConnected == true)
               break;

         }
         if (timeout == 0) // expired
         {
            CancelRequestConnect();
         }

         return isSocketConnected;
      }
      private void Connect()
      {
         try
         {
            _socket.Connect(_remoteEP);
            isSocketConnected = true;
         }
         catch (Exception ex)
         { 
            Debug.Print("Connect Exception: " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
         }
      }


   }

}

I’m not using a beta G400D, I’m using the production v1.3 G400D.

@ Andy.NApex -
Try this + try WinUSB driver


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
 
using GHI.Premium.Net;

namespace G400_TestEthenetbuildInLookup
{
   public class Program
   {
 
      static EthernetBuiltIn eNet = new EthernetBuiltIn();
      static int ConnectTryCount = 0;
      public static void Main()
      {

         eNet.Open();
       
 
         eNet.NetworkInterface.EnableStaticIP("10.0.26.18", "255.255.255.0", "0.0.0.0");
         eNet.NetworkInterface.EnableStaticDns(new string[] { "0.0.0.0", "0.0.0.0" });
         NetworkInterfaceExtension.AssignNetworkingStackTo(eNet);
         Debug.Print("IP Address: " + eNet.NetworkInterface.IPAddress);
         Debug.Print("Subnet Mask: " + eNet.NetworkInterface.SubnetMask);
         Debug.Print("Gateway: " + eNet.NetworkInterface.GatewayAddress);
 
         IPAddress RemoteIPAdrs = IPAddress.Parse("10.0.26.1");
         IPEndPoint mRemoteEndPoint = new IPEndPoint(RemoteIPAdrs, 14220);
         bool IsConnected = false;
 
         do
         {
            MySocket mySock = new MySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySock.TimeOutConnection = 2 * 1000;
            ConnectTryCount++;
            Debug.Print("Connect Try #: " + ConnectTryCount.ToString() + " - " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
            if (mySock.Connect(mRemoteEndPoint))
            {
               IsConnected = true; //mSocket = mySock.GetSocket;
            }
            else
            {
               mySock = null;
               IsConnected = false;
            }
         } while (!IsConnected);
 
 
         System.Threading.Thread.Sleep(-1);
      }
 
   }
 
   public class MySocket
   {
      private Socket _socket;
      private Thread _myThread;
      private EndPoint _remoteEP;
      private Boolean isSocketConnected = false;
      private int _connectTimeOut = 15000; // 15 second
 
 
      public MySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
      {
         _socket = new Socket(addressFamily, socketType, protocolType);
      }
      ~MySocket()
      {
         // Destroy
       
          //_socket = null;
         
          
      }
      public int TimeOutConnection
      {
         get { return _connectTimeOut; }
         set { _connectTimeOut = value; }
      }
      public Socket GetSocket
      {
         get { return _socket; }
      }
      private void CancelRequestConnect()
      {
         if (_myThread != null)
         {
            try
            {
               if (_myThread.IsAlive || _myThread.ThreadState == ThreadState.Running)
               {
                  try
                  {
                     _myThread.Abort();
                    
                  }
                  catch
                  {
 
                  }
               }
               _myThread = null;
            }
            catch
            {
            }
         }
         Thread.Sleep(100);
      }
      public Boolean Connect(EndPoint remoteEP)
      {
         CancelRequestConnect();
         _remoteEP = remoteEP;
         isSocketConnected = false;
         _myThread = new Thread(Connect);
         _myThread.Start();
         int timeout = _connectTimeOut;
         while (timeout > 0)
         {
            Thread.Sleep(10);
            timeout -= 10;
            if (isSocketConnected == true)
               break;
 
         }
         if (timeout == 0) // expired
         {
            CancelRequestConnect();
         }
 
         return isSocketConnected;
      }
      private void Connect()
      {
         try
         {
            _socket.Connect(_remoteEP);
            isSocketConnected = true;
         }
         catch (Exception ex)
         { 
            Debug.Print("Connect Exception: " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
         }
      }
 
 
   }
 
}


@ Dat -

Unfortunately, that made no difference it still locks up after 3 connect attempts.

@ Andy.NApex -

Did you try WinUSB driver?

It looked up, got blue screen then I switched to WinUSB driver and it is gone.

I am already using the WinUSB drivers.

The G400 will lock up with or without it being attached to a debugger.

Is there any update to this issue?
Is this something only I’m seeing or have you been able to reproduce it?

I’m at a bit of a stand still as this is a required for my project.

Our apologies for the delayed response. We are currently working on the issue. We were able to reproduce the issue only while using the legacy drivers. Once we switched to WinUSB drivers the problem went away. We will keep you updated as information on this issue comes to light.

Here is what happens when I run the sample code given in the original post. This is running on a ChipworkX Development board v1.2 with no USB or debugger attached it only has power and ethernet. After about 2 or 3 tries to connect it starts to in an inconsistent manner message shown in the picture.
I am also currently running the WinUSB drivers on my system.

@ Andy.NApex -

Add _myThread.Suspend() before _myThread.Abort();
It should work.


private void CancelRequestConnect()
      {
         if (_myThread != null)
         {
            try
            {
               if (_myThread.IsAlive || _myThread.ThreadState == ThreadState.Running)
               {
                  try
                  {

                      _myThread.Suspend();   ////==> Added
                     _myThread.Abort();
                    
                  }
                  catch
                  {
                      Debug.Print("Exception when kill a thread");
                  }
               }
               _myThread = null;
              // Once called socket.Connect, should call socket.Close() if failed.
               _socket.Close();
               _socket = null;
            }
            catch
            {
            }
         }
      }

Edit:

Once called socket.Connect, should call socket.Close() if failed…

Adding the Suspend() before the Abort() did make a difference, as it now will run a longer period up to around 25 minutes, but then it will still start to crash as described before.

@ Andy.NApex -

did you add?


              // Once called socket.Connect, should call socket.Close() if failed.
               _socket.Close();
               _socket = null;

_socket.Dispose()?

@ Dat -

I did add the


// Once called socket.Connect, should call socket.Close() if failed.
               _socket.Close();
               _socket = null;

@ Gus -

Dispose() is not available on the socket class

protected virtual void Dispose(bool disposing)
1 Like

@ Andy.NApex

I changed a bit and implement Dispose() function,
Try it please

-
public class MySocket
   {
      static Socket[] socket;
      
      private int socket_id = 0;
      private Thread myThread;
      private EndPoint remoteEP;
      private Boolean isSocketConnected = false;
      private int connectTimeOut = 15000; // 15 second

      const int MAX_SOCKET = 40;
      const int NOT_AVAILABLE = -1;
      static int avalable_socket = MAX_SOCKET;
      static UInt64 flag = 0;

      public MySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
      {
        int i;
        if (socket == null)
        {
            socket = new Socket[MAX_SOCKET];
        }
        if (avalable_socket == 0)
        {
            throw new Exception("avalable_socket = " + avalable_socket); 
        }
        for (i=0; i < MAX_SOCKET; i++)
        {
            UInt64 tmp = (UInt64)(1 << i);
            if ((flag & tmp) == 0)
            {
                socket_id = i;
                socket[socket_id] = new Socket(addressFamily, socketType, protocolType);
                avalable_socket--;
                flag |= tmp;
                break;
            }
        }
      }
      public void Dispose()
      {
          if (socket != null)
          {
              if (socket_id != NOT_AVAILABLE)
              {
                  socket[socket_id].Close();
                  socket[socket_id] = null;
                  avalable_socket++;

                  UInt64 tmp = (UInt64)(1 << socket_id);
                  flag &= ~tmp;
                  socket_id = NOT_AVAILABLE;

              }
          }
         
      }
      ~MySocket()
      {

          this.Dispose();
         
          
      }
      public int TimeOutConnection
      {
         get { return connectTimeOut; }
         set { connectTimeOut = value; }
      }
      static public int SocketAvailable
      {
          get { return avalable_socket; }
      }
      public Socket GetSocket
      {
         get 
         {
             if (socket_id != NOT_AVAILABLE)
             {
                 if (socket[socket_id] != null)
                     return socket[socket_id];
                     
             }
             return null; 
         }
      }
      private void CancelRequestConnect()
      {
         if (myThread != null)
         {
            try
            {
               if (myThread.IsAlive || myThread.ThreadState == ThreadState.Running)
               {
                  try
                  {

                      myThread.Suspend();
                     myThread.Abort();
                    
                  }
                  catch
                  {
                      Debug.Print("Exception when kill a thread");
                  }
               }
               myThread = null;
               if (socket != null && socket_id != NOT_AVAILABLE && socket[socket_id] != null)
               {
                   this.Dispose();                 
               }
            }
            catch
            {
            }
         }
        
      }
      public Boolean Connect(EndPoint ep)
      {
         CancelRequestConnect();
         remoteEP = ep;
         isSocketConnected = false;
         myThread = new Thread(Connect);        
         myThread.Start();
         int timeout = connectTimeOut;
         while (timeout > 0)
         {
            Thread.Sleep(10);
            timeout -= 10;
            if (isSocketConnected == true)
               break;
 
         }
         if (timeout == 0) // expired
         {
            CancelRequestConnect();
         }
 
         return isSocketConnected;
      }
      private void Connect()
      {
         try
         {
             if (socket!=null && socket_id != NOT_AVAILABLE && socket[socket_id] != null)
             {
                 socket[socket_id].Connect(remoteEP);
             }
            isSocketConnected = true;
         }
         catch (Exception ex)
         { 
            Debug.Print("Connect Exception: " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
         }
      }
 
 
   }
 

can you try your very first code without the Debug.Print() statement anywhere? also try the release mode and tell us if it locks up…

I think it has something to do with the Debug.Print Statement… this is what I’ve found during my stress test on the Cerbuino…

cheers,
Jay.

@ Jay Jay - didn’t make a difference.

@ Dat -

When I run the new code you supplied it fails now after 1 try with a different error

@ Andy.NApex -

Hello,

Last time I tried these code below, it ran overnight than I stopped it.
Today I am re-testing again it it is still running. So please copy exactly them and run on your side.

And one more thing is very important, test on last SDK R3.
Led1 => will toggle after every 10 seconds
led2 => will be blinked after 100 ms.

Both are working it means the board is running!

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
 using Microsoft.SPOT.Hardware;
using GHI.Premium.Net;

namespace G400_TestEthenetbuildInLookup
{
   public class Program
   {
 
      static EthernetBuiltIn eNet = new EthernetBuiltIn();
      static OutputPort led2 = new OutputPort((Cpu.Pin)(1 * 32 + 18), true); // pb18
      static OutputPort led1 = new OutputPort((Cpu.Pin)(3 * 32 + 0), true); // pd0
      static int ConnectTryCount = 0;
       public static void myThreadLed()
       {
           while (true)
           {
               led1.Write(true);
               Thread.Sleep(100);
               led1.Write(false);
               Thread.Sleep(100);
           }
       }
      public static void Main()
      {

         eNet.Open();
       
 
         eNet.NetworkInterface.EnableStaticIP("10.0.26.18", "255.255.255.0", "0.0.0.0");
         eNet.NetworkInterface.EnableStaticDns(new string[] { "0.0.0.0", "0.0.0.0" });
         NetworkInterfaceExtension.AssignNetworkingStackTo(eNet);
         Debug.Print("IP Address: " + eNet.NetworkInterface.IPAddress);
         Debug.Print("Subnet Mask: " + eNet.NetworkInterface.SubnetMask);
         Debug.Print("Gateway: " + eNet.NetworkInterface.GatewayAddress);
 
         IPAddress RemoteIPAdrs = IPAddress.Parse("10.0.26.1");
         IPEndPoint mRemoteEndPoint = new IPEndPoint(RemoteIPAdrs, 14220);
         bool IsConnected = false;
         Thread mythreadled = new Thread(myThreadLed);
         mythreadled.Start();
   
         do
         {
             Debug.Print("Socket available: " + MySocket.SocketAvailable);
            MySocket mySock = new MySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySock.TimeOutConnection = 10 * 1000;
            ConnectTryCount++;
            Debug.Print("Connect Try #: " + ConnectTryCount.ToString() + " - " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
            if (mySock.Connect(mRemoteEndPoint))
            {
               IsConnected = true; //mSocket = mySock.GetSocket;
            }
            else
            {
               mySock = null;
               IsConnected = false;
            }
            mySock = null;
            //Thread.Sleep(100);
            led2.Write(ConnectTryCount%2==0);
         } while (!IsConnected);
 
 
         System.Threading.Thread.Sleep(-1);
      }
 
   }
 
   public class MySocket
   {
      static Socket[] socket;
      
      private int socket_id = 0;
      private Thread myThread;
      private EndPoint remoteEP;
      private Boolean isSocketConnected = false;
      private int connectTimeOut = 15000; // 15 second

      const int MAX_SOCKET = 40;
      const int NOT_AVAILABLE = -1;
      static int avalable_socket = MAX_SOCKET;
      static UInt64 flag = 0;

      public MySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
      {
        int i;
        if (socket == null)
        {
            socket = new Socket[MAX_SOCKET];
        }
        if (avalable_socket == 0)
        {
            throw new Exception("avalable_socket = " + avalable_socket); 
        }
        for (i=0; i < MAX_SOCKET; i++)
        {
            UInt64 tmp = (UInt64)(1 << i);
            if ((flag & tmp) == 0)
            {
                socket_id = i;
                socket[socket_id] = new Socket(addressFamily, socketType, protocolType);
                avalable_socket--;
                flag |= tmp;
                break;
            }
        }
      }
      public void Dispose()
      {
          if (socket != null)
          {
              if (socket_id != NOT_AVAILABLE)
              {
                  socket[socket_id].Close();
                  socket[socket_id] = null;
                  avalable_socket++;

                  UInt64 tmp = (UInt64)(1 << socket_id);
                  flag &= ~tmp;
                  socket_id = NOT_AVAILABLE;

              }
          }
         
      }
      ~MySocket()
      {

          this.Dispose();
         
          
      }
      public int TimeOutConnection
      {
         get { return connectTimeOut; }
         set { connectTimeOut = value; }
      }
      static public int SocketAvailable
      {
          get { return avalable_socket; }
      }
      public Socket GetSocket
      {
         get 
         {
             if (socket_id != NOT_AVAILABLE)
             {
                 if (socket[socket_id] != null)
                     return socket[socket_id];
                     
             }
             return null; 
         }
      }
      private void CancelRequestConnect()
      {
         if (myThread != null)
         {
            try
            {
               if (myThread.IsAlive || myThread.ThreadState == ThreadState.Running)
               {
                  try
                  {

                     //myThread.Suspend();
                     myThread.Abort();
                    
                  }
                  catch
                  {
                      Debug.Print("Exception when kill a thread");
                  }
               }
               myThread = null;
               if (socket != null && socket_id != NOT_AVAILABLE && socket[socket_id] != null)
               {
                   this.Dispose();                 
               }
            }
            catch
            {
            }
         }
        
      }
      public Boolean Connect(EndPoint ep)
      {
         CancelRequestConnect();
         remoteEP = ep;
         isSocketConnected = false;
         myThread = new Thread(Connect);        
         myThread.Start();
         int timeout = connectTimeOut;
         while (timeout > 0)
         {
            Thread.Sleep(10);
            timeout -= 10;
            if (isSocketConnected == true)
               break;
 
         }
         if (timeout == 0) // expired
         {
            CancelRequestConnect();
         }
 
         return isSocketConnected;
      }
      private void Connect()
      {
         try
         {
             if (socket!=null && socket_id != NOT_AVAILABLE && socket[socket_id] != null)
             {
                 socket[socket_id].Connect(remoteEP);
             }
            isSocketConnected = true;
         }
         catch (Exception ex)
         { 
            Debug.Print("Connect Exception: " + Microsoft.SPOT.Hardware.PowerState.Uptime.ToString());
         }
      }
 
 
   }
 
}

@ Andy.NApex - what did you set the mac address to?

@ Gus -
I had originally not set the MAC Address, so it was set to the default. I then set the MAC Address of the sticker on the unit and retested with same results.

@ Dat -
I copied the code you posted and it fails. I’ve tried with and without the USB connected, either way it eventually stops trying to connect and the LED stops flashing.