G120 ENC28J60 NetworkAddressChanged

When should the NetworkAddressChanged be fired?



I connect my ethernet connection with DHCP  -> event is triggered when IP is assigned -> Perfect

Then i change my NetworkInterface to static IP -> NetworkAddressChanged is NOT triggered ?

Is this normal or not? the IP address is changed.

I made a small test program to verify this functionality:


using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using GHI.Networking;
using GHI.IO;
using GHI.Pins;
using GHI.Processor;
using GHI.Utilities ;
using System.Threading;


namespace ethernet
{
    public class Program
    {

        static EthernetENC28J60 MyEthernet;

        public static void Main()
        {
            MyEthernet = new EthernetENC28J60(SPI.SPI_module.SPI2, GHI.Pins.G120.P1_26, GHI.Pins.G120.P0_28);

            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;

            MyEthernet.Open();

            MyEthernet.NetworkInterface.EnableDhcp();
            MyEthernet.NetworkInterface.EnableDynamicDns();

            int _timeout = 0;
            while (MyEthernet.NetworkInterface.IPAddress.ToString() == "0.0.0.0")
            {
                Thread.Sleep(1000);
                _timeout++;

                Debug.Print("Ethernet waiting for DHCP");
                Debug.Print("Adrress is " + MyEthernet.NetworkInterface.IPAddress.ToString());  

                
                if (_timeout > 30)
                {
                    Debug.Print("Ethernet DHCP timed out after 30 seconds");
                    MyEthernet.NetworkInterface.EnableStaticIP("192.168.0.1", "255.255.255.0", "192.168.0.1"); 
                    break;
                }
            }


            while (true )
            {
                Thread.Sleep(1000);
                Debug.Print("Adrress is " + MyEthernet.NetworkInterface.IPAddress.ToString());  
            }
        }


        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Network Address has changed! ");
        }

        static void NetworkChange_NetworkAvailabilityChanged(object sender, Microsoft.SPOT.Net.NetworkInformation.NetworkAvailabilityEventArgs e)
        {
            Debug.Print("Network has changed! ");
        }
    }
}

And the result :


Scenario 1 connect cable to router
**********************************

Adrress is 0.0.0.0
Ethernet waiting for DHCP
Adrress is 0.0.0.0
Ethernet waiting for DHCP
Adrress is 0.0.0.0
Network has changed!                      <<<---- connect cable
Network Address has changed!              <<<---- event fires
Ethernet waiting for DHCP
Adrress is 10.40.5.22
Adrress is 10.40.5.22
Adrress is 10.40.5.22
Adrress is 10.40.5.22
Adrress is 10.40.5.22
Network has changed!                     <<<<---- disconnect cable
Network Address has changed!             <<<<---- event fires
Adrress is 0.0.0.0
Adrress is 0.0.0.0
Adrress is 0.0.0.0

Scenario 2 no connection, fallback to fixed IP address
******************************************************

Adrress is 0.0.0.0
Ethernet waiting for DHCP
Adrress is 0.0.0.0
Ethernet waiting for DHCP
Adrress is 0.0.0.0
Ethernet DHCP timed out after 30 seconds
Adrress is 192.168.0.1                        <<<---- no event is firing
Adrress is 192.168.0.1
Adrress is 192.168.0.1
Adrress is 192.168.0.1
Network has changed!                          <<<---- cable connected
Network Address has changed!  				  <<<---- now is the event fired 
Adrress is 192.168.0.1                    (but address is changed long before this)           
Adrress is 192.168.0.1
Adrress is 192.168.0.1
Adrress is 192.168.0.1
Adrress is 192.168.0.1

This shows me that the event is actualy binded to cable connect?

I think it just has a conditional - if the cable is connected AND the address changes, then it is a real change. If the cable isn’t connected it doesn’t count. I actually think that makes sense - you don’t want to act on the address change if the cable isn’t connected, for example you don’t want to try to go and get the time from the network…

@ Brett - true… true… but then the event name should be NetworkAddressChanged_and_CableConnected :whistle:

1 Like