I’m just trying to get a Cerbuino + Enc28 Module to work. I’m not interested in using the Gadgeteer platform just NetMF.
I initialized the module with
public class Program
{
private static EthernetENC28J60 netif;
public static void Main()
{
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
netif = new EthernetENC28J60(SPI.SPI_module.SPI1, Generic.GetPin('A', 13), Generic.GetPin('A', 14), Generic.GetPin('B', 10));
netif.Open();
netif.EnableDhcp();
netif.EnableDynamicDns();
var led = new OutputPort(Generic.GetPin('B', 2), false);
while (true)
{
led.Write(true);
Thread.Sleep(500);
led.Write(false);
Thread.Sleep(500);
}
}
private static void NetworkChange_NetworkAddressChanged(object sender, Microsoft.SPOT.EventArgs e)
{
if (netif.IPAddress != "0.0.0.0")
{
//If I plug the network calbe into the socket netif.IPAddress = "0.0.0.0"
//If I unplug it, then netif.IPAddress has the last IP it had before
}
}
private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
Debug.Print("Network availability: " + e.IsAvailable.ToString());
}
}
It seems the network address changed event is firing before the netif.IPAddress property is set with the latest value. Seems it always is the old value?
Yes, but I think this is not the intended use case of the NetworkAddressChanged event.
I think everyone would expect that the IP in the event would have the correct value. Even in your sample code this is expected… I think this has to be treated like a bug? What do you think?
@ John - I have been using the RS21 module recently, and I noticed that the IP address is not available when the network changed or IP address changed event occurs. When I get the IP address changed event, I start a thread which waits for the address to appear.
@ Alex111 - The network interface object that NETMF exposes is not update in real time. It’s a static copy. To update it, you must recreate the interface through a call to GetNetworkInterfaces that NETMF provides. We do make a call internally in the interface in the same event handler you use. Since NETMF events are not deterministic, your event handler can be called before ours meaning you will see the old stale data. The next SDK will add a RefreshNetworkInterface method that you can call to manually update the interface object before checking its properties.