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.
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.
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.
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());
}
}
}
}
@ 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.