I can run Ethernet code using a NETMF project, as in code from GHI documentation, but when I run a NET Gadgeteer project using Ethernet J11D module, the network up event never fires.
Output of code that follows:
Ethernet cable is connected!
Enable DHCP
Network settings:
IP Address: 192.168.1.200
Subnet Mask: 255.255.255.0
Default Getway: 192.168.1.1
DNS Server: 192.168.1.1
Test Request
Status code: 200 Status description: OK
Content type: text/html Content length: 6237
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using GHIElectronics.NETMF.Net;
using System.Net;
namespace EthernetNETMF
{
public class Program
{
static public bool ethernet_event = false;
static public bool ethernet_last_status = false;
static public bool network_is_read = false;
static public ManualResetEvent NetworkAvailablityBlocking = null;
public static void Main()
{
if (!Ethernet.IsEnabled)
{
Ethernet.Enable();
}
NetworkChange.NetworkAvailabilityChanged +=
new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();
// Ethernet is always the first interface netif[0]
NetworkAvailablityBlocking = new ManualResetEvent(false);
if (!Ethernet.IsCableConnected)
{
Debug.Print("Cable is not connected!");
NetworkAvailablityBlocking.Reset();
while (!NetworkAvailablityBlocking.WaitOne(5000, false))
{
if (!Ethernet.IsCableConnected)
{
Debug.Print("Cable is not connected!");
Debug.Print("Still waiting.");
}
else
break;
}
}
Debug.Print("Ethernet cable is connected!");
Debug.Print("Enable DHCP");
try
{
if (!netif[0].IsDhcpEnabled)
netif[0].EnableDhcp(); // This function is blocking
else
{
netif[0].RenewDhcpLease(); // This function is blocking
}
network_is_read = true;
Debug.Print("Network settings:");
Debug.Print("IP Address: " + netif[0].IPAddress);
Debug.Print("Subnet Mask: " + netif[0].SubnetMask);
Debug.Print("Default Getway: " + netif[0].GatewayAddress);
Debug.Print("DNS Server: " + netif[0].DnsAddresses[0]);
}
catch
{
Debug.Print("DHCP Faild");
}
Debug.Print("Test Request");
try
{
using (HttpWebRequest request = HttpWebRequest.Create("http://cle-services.net") as HttpWebRequest)
{
HttpWebResponse response;
try
{
response = request.GetResponse() as HttpWebResponse;
Debug.Print("Status code: " + response.StatusCode + " Status description: " + response.StatusDescription);
Debug.Print("Content type: " + response.ContentType + " Content length: " + response.ContentLength);
response.Close();
}
catch (Exception ex)
{
string exception = ex.Message + " Inner:" + ex.InnerException;
Debug.Print(exception);
}
}
}
catch
{
Debug.Print("Faild to Get the host entry of the FQN from DNS server!");
}
Thread.Sleep(Timeout.Infinite);
}
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
if (Ethernet.IsCableConnected)
{
if (ethernet_last_status != true)
{
ethernet_last_status = true;
NetworkAvailablityBlocking.Set();
}
}
}
else
{
if (!WiFi.IsLinkConnected)
{
if (ethernet_last_status != false)
{
ethernet_last_status = false;
network_is_read = false;
}
}
}
}
}
}