Main Site Documentation

Can the static IP of EMX be set automatically?


It is possible that EMX set the passed static IP by itself according to the gateway in the network, if the ethernet connect to a network

In my project the static IP setting is as follow:


            static string myIP = "";


            Eth1.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
            Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);

            Eth1.NetworkInterface.EnableStaticIP(myIP, "", "");

I like something as follow:

static-iP = static-iP_Function();
subnet-Mask = subnet-mask_Function()
gateway = gateway_Function()
Eth1.NetworkInterface.EnableStaticIP(static-iP, subnet-Mask, gateway");

I don’t want to set the software fpr IP always manually, if I connect EMX board to a new network. Is it possible?


Let dhcp assign a ip

        if (!Eth1.NetworkInterface.IsDhcpEnabled)

       //  Wait for DHCP (on LWIP devices)
        while (true)
            IPAddress ip = IPAddress.GetDefaultLocalAddress();

            if (ip != IPAddress.Any) break;



DHCP is the correct protocol to use for different networks.

Without knowing specifics of the new network you connect to, a fixed IP address can be incorrect (it will either not be for that network, or it might clash with an existing device), and if it is incorrect the default gateway will likely be incorrect (there is no standard that requires a gateway to be defined as the “.1” or “.254” address for example, and the subnet mask is a function of the addressable network device range without passing through a router. Assuming you have no control of those things, for example going to a friends’ house who has different network hardware to you, your device might cause a problem with another device, it might not be able to connect to the network, it might not be able to see any other devices on the network, or it might not be able to reach the internet. DHCP is designed to address this, so letting it do it’s job will mean you don’t have to think about this. And if you’re always wanting your device to have a particular address on a network that you control, define a “reserved” IP address in your DHCP server so that it always issues the same address.


If you have the space code wise for it, set the system to use either through a setting somewhere.

I have a setting in my code that allows to use DHCP or STATIC. Sometimes I need a static IP for some installations and sometimes just use DHCP. Having the option for both means you cover all bases.

I also have code that falls back to a fixed STATIC IP should DHCP fail. This is ideal for systems where you have Ethernet but not connected or wish later to plug in a laptop with a cross over cable to do testing or updates etc.