I am trying to get the Wi-Fi WiFi RS21 Module working on my Cobra board.
I get exceptions when i run it.
In the example before the try catch of the WiFi.Enable there is this line:
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
when stepping through the code, it jumps to this method before the try catch WiFi.Enable can finish.
so if there is an error in try it will not jump to the catch of WiFi.Enable, it just stops.
So my question is, shouldn’t that NetworkChange, be moved elsewhere after the success of the try ?
This is an event handler. What I think you’re experiencing is that the event is getting fired from within the try and the debugger is jumping out to the handler. The code is actually still within the try block. However, if this event fires often you may have a hard time stepping back into the try block. Try putting a stop within the try and then hit F5 to see if you can get back in.
What happens is before it completes the try for WiFi.Enable. it fires the even handler. see below.
i dont think they should set up the event hadler before its confirmed that WiFi.Enable is ok.
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
if (WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was established!");
}
}
else
{
if (!WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was dropped or disconnected!");
network_is_read = false;
}
}
}
It sounds to me that the order of things is correct but the firing of the event is not. You would want to attach the event handler before the Enable because enabling WiFi essentially changes the state of network availability (assuming it works). It sounds to me that the problem is that the event is falsely being fired in anticipation of the network availability changing and not on it actually changing. I wouldn’t expect the event to fire until after Enable completed successfully. But, if you didn’t attach the handler until after Enable was called then you would miss the event of the network first becoming active. Of course, that could work if you made the assumption that you were starting in a state where the network was already available but that seems like the wrong way to do it to me.
ok. but when the module needs a firmware update, this event was keeping it from getting it.
In the example anyway. Not saying that one could not write code to get around that. it was more of a FYI about the example.
So the example code has to be modified. it should check wifi.isEnabled. Or as you mentioned, the event handler should be subscribed to after you make sure it is enabled.
yes, i was just letting you know that the example should be tweaked like you suggested because if others try that example verbatim and their module needs a firmware update, it will not allow it to happen.
hmm, I see that the even might fire before the the wifi is enabled because it is simply for Ethernet.
But I don’t see why would this fired event block the device from updating the firmware!
Do you mean that
WiFi.IsLinkConnected
is raising the exception? in
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
if (WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was established!");
}
}
else
{
if (!WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was dropped or disconnected!");
network_is_read = false;
}
}
}
In this case, you are absolutely right. the example has to be modified to
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if(WiFi.isEnabled)
{
if (e.IsAvailable)
{
if (WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was established!");
}
}
else
{
if (!WiFi.IsLinkConnected)
{
Debug.Print("WiFi connection was dropped or disconnected!");
network_is_read = false;
}
}
}
}
the second snippet should work. I would try it but i have no idea how to roll the firmware back.
If you roll back your firmware on your module and try the example the way it sits you will see what i mean.
when the code first starts it subscribes the event first. then it tries to enable the wi-fi. Before it can complete this process the “NetworkChange_NetworkAvailabilityChanged” even fires and stops the whole thing. so it never gets to the catch where it will then allow it to update the firmware.