Following on from Chris' comments about waiting for a network connection on the Wifi version of the Netduino 3, I've written a Network helper class to use for both the Ethernet and Wifi boards:
  | ||||||||||||||
Waiting for a network connection
Started by ukkiwisurfer, Oct 16 2015 11:43 AM
2 replies to this topic
#1Posted 16 October 2015 - 11:43 AM Following on from Chris' comments about waiting for a network connection on the Wifi version of the Netduino 3, I've written a Network helper class to use for both the Ethernet and Wifi boards:
public NetworkInformation GetNetworkDetails(int interfaceIndex)
{
NetworkInformation information = null;
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
// Wait for the network to become availableand for a DHCP address to be allocated.
WaitHandle.WaitAll(new[] { m_WaitForAddressChange, m_WaitForNetwork });
....
}
The Ethernet version is configured to use DHCP. For reference the associated code to enable the WaitHandle.WaitAll() is below.
/// <summary>
/// Initialises an instance of the <see cref="NetworkHelper"/> class.
/// </summary>
public NetworkHelper()
{
m_WaitForNetwork = new AutoResetEvent(false);
m_WaitForAddressChange = new AutoResetEvent(false);
}
/// <summary>
/// Event handler to signal when the network address has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void OnNetworkAddressChanged(object sender, EventArgs eventArgs)
{
m_WaitForAddressChange.Set();
}
/// <summary>
/// Event handler to signal that the network is now available.
/// </summary>
/// <param name="sender"></param>
/// <param name="networkAvailabilityEventArgs"></param>
private void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs eventArgs)
{
if (eventArgs.IsAvailable)
{
m_WaitForNetwork.Set();
}
}
#2Posted 22 October 2015 - 08:21 PM In an interesting twist to this issue, running this code on a Netduino Plus 2 highlights a race condition. The issue is that the ethernet and Wifi networking capabilities on the Netduino 3 take a lot longer to initialise (3+ seconds) than the Netduino Plus 2's (4.3.2.1 Firmware) Ethernet does (milliseconds).
The consequence is, the above code will block indefinitely on Netduino Plus 2 at the WaitHandle.WaitAll() call because the NetworkChange event handlers never trigger. The network has already been initialised and configured (DHCP) even before the code has had a chance to set up the event handlers.
However the Netduino 3 (Ethernet and Wifi) take several seconds to initialise, allowing the NetworkChange event handlers to be set up and to fire. #3Posted 22 October 2015 - 08:30 PM The following code should remedy the above issue for the Netduino Plus 2 and will still work for the Netduino 3 (Ethernet and Wifi) boards. Short summary: specify a timeout in milliseconds and loop until the DHCP address has been assigned.
/// <summary> public NetworkInformation GetNetworkDetails(int interfaceIndex) 0 user(s) are reading this topic0 members, 0 guests, 0 anonymous users | ||||||||||||||
|
||||||||||||||
This webpage is licensed under a Creative Commons Attribution-ShareAlike License. | ||||||||||||||