Main Site Documentation

Cobra II - Possible corruption in code deployment


#1

Hello everybody.

Today I was testing my recent Cobra II (G120) with 4.2.9.1 firmware and 4.2.9.0 SDK. After a while I started to have weird CLR errors mostly in the beginning of the code (mine or in SDK).
Most of the time it was on inside the board initialization

Mainboard = new GHIElectronics.Gadgeteer.FEZCobra_II();

some other in different parts of the code like this one now:

int cfg_sntp_gmt_offset = 0;

giving this error:

and the debugger breaks at that instruction.

If I rebuild and redeploy, the stop might occur somewhere else or not at all. I have tried already a multitude of different configurations. I have read that it could be a power issue, so I have the board self powered by an external 1A 12VDC.

One way to a temporary solution I have found is to use the G120 Updater and Erase my application, then redeploy and debug. After some times, the problem comes again. Also, the code behaves in an erratic way: Right now, I have a timer set, and is not triggering. The board has stopped responding and no output is being displayed. I redeployed and the third time it started working.

I then decided to revert back to the interface version of USB drivers.

With this version the deploy succeeded everytime and the code ran without problems. I had however some other issues with this driver: While deploying and press “Reset”, It also resets the PC. Also, if the deployment takes some time, the board after a while (with wrong code) auto-resets taking the PC with it.
Another problem that I had was with the variable inspect while debugging. It crashed Visual Studio.

I went back to WinUSB drivers.

I don’t think this is normal, so I was wandering if someone had experienced the same behavior as I describe here… or any ideas of what might going on.

Could I need the 4.2.9.1 SDK to be compatible to the 4.2.1.9 firmware?

My configuration:
Windows 7 64bit, FEZ Cobra II (rev B), Visual Studio 2010, MicrosoftMicroFramework_42RTM_QFE2_SDK, GHI NETMF v4.2 and .NET Gadgeteer Package 2013-02-14, G120 Firmware Hotfix v4.2.9.1

Thanks.

PS: Right now, same code - no change, a different line:

Mainboard = new GHIElectronics.Gadgeteer.FEZCobra_II();

in the default code:


namespace GadgeteerApp1
{
    public partial class Program : Gadgeteer.Program
    {
        // GTM.Module definitions

        public static void Main()
        {
            //Important to initialize the Mainboard first
            Mainboard = new GHIElectronics.Gadgeteer.FEZCobra_II();			

            Program program = new Program();
            program.InitializeModules();
            program.ProgramStarted();
            program.Run(); // Starts Dispatcher
        }

the output from debug:


#2

Carlos, can you create a brand new project, and just add Cobra2 to the designer, and then in a timer flash the onboard LED? Does that work?

You will also need to tell us what other devices you might have on the designer of your original project.


#3

@ Brett - Hi Brett,

Yes. With just that code, it works everytime.

On the designer I don’t have anything else but the board itself. The board comes with a SD card reader and a Wifi module (RS9110N) and I was using them both.

With this brand new project I just added the lines for the WiFi operation, together with the library references GHI.Premium.IO, GHI.Premium.Net and GHI.Premium.Hardware.G120 and it started to happen again:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace GadgeteerApp2
{
    public partial class Program
    {

        public GHI.Premium.Net.WiFiRS9110 wifi;

        bool cfg_wifi_dhcp = true;
        string cfg_wifi_ssid = "MY_SSID";
        string cfg_wifi_psk = "MY_LESS_THAN_32C_PSK";

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");

                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
                timer.Start();


                wifi = new GHI.Premium.Net.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, GHI.Premium.Hardware.G120.Pin.P1_10, GHI.Premium.Hardware.G120.Pin.P2_11, GHI.Premium.Hardware.G120.Pin.P1_9, 4000);
                wifi.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);

                if (!wifi.IsOpen)
                    wifi.Open();

                if (cfg_wifi_dhcp && !wifi.NetworkInterface.IsDhcpEnabled)
                {
                    wifi.NetworkInterface.EnableDhcp();
                    wifi.NetworkInterface.EnableDynamicDns();
                }

                GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);

                GT.Timer tmWifi = new GT.Timer(30000); // every 30 seconds
                tmWifi.Tick += new GT.Timer.TickEventHandler(tmWifi_Tick);
                tmWifi.Start();

        }

        void timer_Tick(GT.Timer timer)
        {
            PulseDebugLED();
        }

        void tmWifi_Tick(GT.Timer timer)
        {
            timer.Stop();
            try
            {
                if (wifi != null && !wifi.IsLinkConnected)
                {
                    GHI.Premium.Net.WiFiNetworkInfo[] scanResult = wifi.Scan(cfg_wifi_ssid);
                    foreach (GHI.Premium.Net.WiFiNetworkInfo ni in scanResult)
                    {
                        try
                        {
                            wifi.Join(ni, cfg_wifi_psk);
                            break;
                        }
                        catch (GHI.Premium.Net.NetworkInterfaceExtensionException ex)
                        {
                            Debug.Print("PassPhrase probably faulty for the selected Access Point");
                            Debug.Print(ex.Message + ex.StackTrace);
                            continue;
                        }
                    }
                }
                try
                {
                    if (wifi != null && wifi.IsLinkConnected)
                    {
                        if (cfg_wifi_dhcp)
                        {
                            if (!wifi.NetworkInterface.IsDynamicDnsEnabled || (wifi.NetworkInterface.DnsAddresses.Length == 0))
                                wifi.NetworkInterface.EnableDynamicDns();
                        }

                    }
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    Debug.Print("DHCP Failed");
                    if (e.ErrorCode == 11003)
                        Debug.Print("Re-Enable the module.");

                    if (wifi != null && wifi.IsLinkConnected)
                        wifi.Disconnect();
                }

            }
            catch (System.Exception ex)
            {
                Debug.Print(ex.Message);
            }

            timer.Restart();
        }

        void wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Network address changed!");
            Debug.Print("DHCP is: " + (wifi.NetworkInterface.IsDhcpEnabled ? "Enabled" : "Not Enabled"));

            if (wifi.NetworkInterface.IPAddress != "0.0.0.0")
            {
                Debug.Print("Is DynamicDnsEnabled enabled: " + wifi.NetworkInterface.IsDynamicDnsEnabled);
                Debug.Print("NetworkInterfaceType " + wifi.NetworkInterface.NetworkInterfaceType);
                Debug.Print("Network settings:");
                Debug.Print("IP Address: " + wifi.NetworkInterface.IPAddress);
                Debug.Print("Subnet Mask: " + wifi.NetworkInterface.SubnetMask);
                Debug.Print("Default Gateway: " + wifi.NetworkInterface.GatewayAddress);
                Debug.Print("Number of DNS servers:" + wifi.NetworkInterface.DnsAddresses.Length);
                for (int i = 0; i < wifi.NetworkInterface.DnsAddresses.Length; i++)
                    Debug.Print("DNS Server " + i.ToString() + ":" + wifi.NetworkInterface.DnsAddresses[i]);
            }
            else
            {
                Debug.Print("No IP Address allocated!");
            }
            Debug.Print("------------------------------------------------------");
        }

    }
}

The result:

Now, the VS is in debug mode (statusbar displays “Ready”) and the output does not displays anything else. The board is periodically resetting, not pulsing the LED.
After this is very difficult to redeploy.

I admit I must be doing something wrong with the code. Maybe adding too much references, or too few. Perhaps I should create a new thread to do the wifi Join instead of doing it on the timer callback. I will try some more options now.

If you have some suggestions I will gladly try them also.
Thanks.


#4

Hello again. I just changed the previous code to add the threaded method wifiScan containing the code previously in tmWifi_Tick.

Here is the new code:


        Thread wifiScanThread;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");

                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
                timer.Start();

                wifi = new GHI.Premium.Net.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, GHI.Premium.Hardware.G120.Pin.P1_10, GHI.Premium.Hardware.G120.Pin.P2_11, GHI.Premium.Hardware.G120.Pin.P1_9, 4000);
                wifi.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);

                if (!wifi.IsOpen)
                    wifi.Open();


                Debug.Print("Assigning stack to wifi");
                GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);

                if (cfg_wifi_dhcp && !wifi.NetworkInterface.IsDhcpEnabled)
                {
                    Debug.Print("Enabling DHCP");
                    wifi.NetworkInterface.EnableDhcp();
                    Debug.Print("Enabling DNS");
                    wifi.NetworkInterface.EnableDynamicDns();
                }

                GT.Timer tmWifi = new GT.Timer(30000); // every 30 seconds
                tmWifi.Tick += new GT.Timer.TickEventHandler(tmWifi_Tick);
                tmWifi.Start();

                wifiScanThread = new Thread(wifiScan);

                Debug.Print("Startup complete");
        }

        void tmWifi_Tick(GT.Timer timer)
        {
            if (!wifiScanThread.IsAlive)
                wifiScanThread.Start();
        }

But it doesn’t matter because it does not even gets there.

The VS debug breaks in the line:


                tmWifi.Start();

with the exception CLR_E_WRONG_TYPE (1):


#5

I don’t mean to be talking to myself, and I’am sorry about the long messages, but I have made some other discovery:

Re-applied the firmware (4.2.9.1), erasing all.

Using the simplest code suggested by Brett (timer with a pulse led) with a brand new project, I deployed to the CobraII and it worked well.
Then I just added the GHI.Premium.Hardware and GHI.Premium.Hardware.G120 references. Rebuilt and deployed and… it happened:

breaking in the line:


            Mainboard = new GHIElectronics.Gadgeteer.FEZCobra_II();			

Removed the assemblies, redeployed and it started working again.

Added the assembly GHI.Premium.Net, redeployed and it worked well again.

Then I added this code after the timer.Start():


                wifi = new GHI.Premium.Net.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2,
                    (Microsoft.SPOT.Hardware.Cpu.Pin)(32 + 10), // GHI.Premium.Hardware.G120.Pin.P1_10
                    (Microsoft.SPOT.Hardware.Cpu.Pin)((2 * 32) + 11), // GHI.Premium.Hardware.G120.Pin.P2_11,
                    (Microsoft.SPOT.Hardware.Cpu.Pin)(32 + 9) // GHI.Premium.Hardware.G120.Pin.P1_9,
                );

                if (!wifi.IsOpen)
                    wifi.Open();

and this happened:

The board auto-reset, works for while and auto-reset again in a loop.

Has anyone have and idea of what this might be due? This is a very erratic behavior.

And now this one just happened: A new blank Gadgeteer project, second deployment (no changes) after reapplying the 4.2.9.1 firmware:

I give up (for now). I spent all the weekend around this just to found out that it happens even without any custom code.


#6

While USB specifications say ports should provide 500mA, we rarely see a port that can actually provide 500mA. We always recommend the use of powered hubs or a power pack instead of relying on the USB power. Use a powered hub from a known brand, not a cheap hub. If your device has the option of using a power pack (for example the USB Client DP Module) then you can use a power pack instead. 9V 1A is typically recommended but check the bottom of the circuit board for printed voltage range. All power connectors on GHI products are 2.1mm with positive on the inside and negative on the outer ring.

Not using a powered hub or power pack can cause:
[ul]Unexplained behavior
Device does not function
Device functions intermittently
Device functions but network fails
Device functions but SD card fails
Device functions but firmware update fails[/ul]
(Generated by QuickReply)

I’m now suspecting power. Just doesn’t sound sensible otherwise. The 4.2.9.1 firmware was meant to provide some additional WiFi / DHCP resilience and was meant to work with 4.2.9.0 bits in the SDK, so I don’t expect anything there.


#7

@ Brett - Yes, yes… I also sought that quick answer. In any case my USB ports can provide 2A (according to the PC manual) since it is an USB+ Charger port. 1A can provide for sure.

Anyway, just to be sure and exclude that hypothesis, I’m using a 1Ampere, 12V DC external power supply plugged in the barrel connector. I have tested with and without external power. Also tried other USB ports with the same outcome.

PS: You can also read my first post: “I have read that it could be a power issue, so I have the board self powered by an external 1A 12VDC.”.

If you have any more ideas, keep them coming. I already have run out. :slight_smile:

Thanks.


#8

@ Brett - Brett. Do you have this board? If so, can you replicate the test?

Now, I just would like to know if anybody else have the same syndrome with this configuration:

WinUSB + Windows 7 64bit, FEZ Cobra II (rev B), Visual Studio 2010, MicrosoftMicroFramework_42RTM_QFE2_SDK, GHI NETMF v4.2 and .NET Gadgeteer Package 2013-02-14, G120 Firmware Hotfix v4.2.9.1

I’m going to buy a USB Hub to try with that.


#9

I have it, yes. But I can’t test it for you because it looks like I’m the last remaining person whose WiFi / DHCP problem is not resolved by 4.2.9.1.


#10

Hi,
I think, you should declare your timers and wifi before the “Program started” part.
Regards
Roland


#11

Hi,
try the code, I just used successfully on my Cobra 2. It`s according to the DHCP problem troubleshooter thread: http://www.tinyclr.com/forum/topic?id=10855


// References:
// GHI.Premium.Hardware.G120
// GHI.Premium.Net


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using GHI.Premium.Net;
using GHI.Premium.Hardware;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GHINET = GHI.Premium.Net;

namespace GadgeteerApp2
{
    public partial class Program
    {
        static GHINET.WiFiRS9110 wifi;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
            wifi = new GHINET.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, G120.Pin.P1_10, G120.Pin.P2_11, G120.Pin.P1_9);
            wifi.Open();

            if (!wifi.IsOpen)
                wifi.Open();

            if (!wifi.NetworkInterface.IsDhcpEnabled)
                wifi.NetworkInterface.EnableDhcp();

            wifi.NetworkAddressChanged += new GHINET.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(wifi_NetworkAddressChanged);
            wifi.WirelessConnectivityChanged += new GHINET.WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);

            GHINET.NetworkInterfaceExtension.AssignNetworkingStackTo(wifi);

            GHINET.WiFiNetworkInfo[] scanResult = wifi.Scan();

            for (int i = 0; i < scanResult.Length; i++)
            {
                if (scanResult[i].SSID == "FriBo24")
                {
                    wifi.Join(scanResult[i], "X0X0X0X0X0");
                    break;
                }
            }
        }

        static void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            Debug.Print("Wireless is: " + (e.IsConnected ? "Connected" : "Disconnected"));

            Debug.Print("SSID: " + e.NetworkInformation.SSID);

            Debug.Print("WiFi Channel number: " + e.NetworkInformation.ChannelNumber);

            DisplayNetworkType(e.NetworkInformation);

            DisplayMACAddress(e.NetworkInformation);

            Debug.Print("RSSI: -" + e.NetworkInformation.RSSI + "dB");

            DisplaySecurityMode(e.NetworkInformation);
        }

        static void wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("New address for the Wireless Network Interface ");

            Debug.Print("Is DhCp enabled: " + wifi.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Is DynamicDnsEnabled enabled: " + wifi.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType " + wifi.NetworkInterface.NetworkInterfaceType);
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + wifi.NetworkInterface.IPAddress);
            Debug.Print("Subnet Mask: " + wifi.NetworkInterface.SubnetMask);
            Debug.Print("Default Gateway: " + wifi.NetworkInterface.GatewayAddress);
            Debug.Print("Number of DNS servers:" + wifi.NetworkInterface.DnsAddresses.Length);
            for (int i = 0; i < wifi.NetworkInterface.DnsAddresses.Length; i++)
                Debug.Print("DNS Server " + i.ToString() + ":" + wifi.NetworkInterface.DnsAddresses[i]);
            Debug.Print("------------------------------------------------------");
        }

        #region Helpers
        static void DisplayNetworkType(GHINET.WiFiNetworkInfo info)
        {
            switch (info.networkType)
            {
                case GHINET.NetworkType.AccessPoint:
                    {
                        Debug.Print("Wireless Network Type: Access Point");
                        break;
                    }
                case GHINET.NetworkType.AdHoc:
                    {
                        Debug.Print("Wireless Network Type: Ad Hoc");
                        break;
                    }
            }
        }

        static void DisplayMACAddress(GHINET.WiFiNetworkInfo info)
        {
            string MAC = new string(null);
            int splitCount = 0;

            for (int i = 0; i < info.PhysicalAddress.Length; i++)
            {
                MAC += (info.PhysicalAddress[i].ToString("X"));

                if (splitCount < 5)
                {
                    MAC += ":";
                    splitCount++;
                }
            }

            Debug.Print("MAC Address: " + MAC);
        }

        static void DisplaySecurityMode(GHINET.WiFiNetworkInfo info)
        {
            switch (info.SecMode)
            {
                case GHINET.SecurityMode.Open:
                    {
                        Debug.Print("Wireless Security Mode: Open");
                        break;
                    }
                case GHINET.SecurityMode.WEP:
                    {
                        Debug.Print("Wireless Security Mode: WEP");
                        break;
                    }
                case GHINET.SecurityMode.WPA:
                    {
                        Debug.Print("Wireless Security Mode: WPA");
                        break;
                    }
                case GHINET.SecurityMode.WPA2:
                    {
                        Debug.Print("Wireless Security Mode: WPA2");
                        break;
                    }
            }
        }
        #endregion

    }
}

Regards
Roland


#12

Hello again.

I have been experimenting with this board (CobraII-wifi Rev B) for the last week and I have found some unexpected behaviors that I would like to share with you.

I have finally managed to write my intended code (SD+wifi with timers), but it would not run for long. It could run for an hour or 20 seconds, with no changes in between and no apparent reason why. So I restarted from scratch:

  • Erased everything and re-flashed the firmware with v4.2.9.1.
  • Restarted VS2010 with a new Gadgeteer project with only this code:

        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

			GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
			timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
			timer.Start();

			GT.Timer timer2 = new GT.Timer(1100); // every 1100ms
			timer2.Tick += new GT.Timer.TickEventHandler(timer2_Tick);
			timer2.Start();
        }

        void timer_Tick(GT.Timer timer)
        {
            PulseDebugLED(); // to tick the debug LED
        }

        void timer2_Tick(GT.Timer timer)
        {
            Debug.Print("timer2");
        }

Then I deployed with the Debug configuration. Let it run for a while and it seamed ok.
Disconnected everything and reconnected only the 12VDC connector (not USB). The LED started to pulse every second (more ou less) as expected.

I leaved it standing there and after some hours (about 7) the debug LED was off and only the power LED was on. I tried again with the Release configuration and it is running for 1 day now. Conclusion? Could it be a problem with the Debug assemblies?

During my tentative of making my code work, most of the problems were related with timer events: Most of the times it would crash with CLR_E_OUT_OF_RANGE (you can read my previous posts).

I noticed that the longer the code inside the timer callback, the more chances of a crash, so I tried to leave from them as fast as possible. The workaround was to use AutoResetEvents, create a thread waiting for the event to signal and put only the .Set() inside the timer callback. That eliminated the crashes inside the timers callbacks.

As I was using the integrated RS9110 wifi module, I also was having problems with the callbacks of wifi events, but of a different kind: It looked like the .NetworkInterface references were changing while I was reading them. For instance it throwed and exception while I was Debug.Print’ing the string .NetworkInterface.IPAddress. A variable inspection showed that some of the strings were ok with values (like SubnetMask) but other like IPAddress or DnsAddresses were null. Also the argument GHI.Premium.Net.WiFiRS9110.WirelessConnectivityEventArgs from the callback _WirelessConnectivityChanged suffers from the same problem, throwing exceptions of null while reading strings.

Also noticed that the GHI.Premium.Net.WiFiRS9110.Join() method is freezing the system until it returns, even if it is on a different thread. Is this the behavior to be expected?

Last, but not the least, the System.Net.HttpWebRequest.Timeout property doesn’t work when the System.Net.HttpWebRequest.GetResponse() blocks. I was expecting an exception, but it never happened and the thread was just kept alive. Maybe related to the previous problems, don’t know.
(btw, I was periodically retrieving a file with the HttpWebRequest. Sometimes it would work, sometimes the call would block, to never return).

Maybe most of these behaviors are to be expected, or maybe not. I just wanted to report here so that you know that this is happening and perhaps someone could explain why this is happening, or look into it to see if something needs improving (or fixing).

Many thanks to all for reading my long posts :slight_smile:


#13

I would try making Timer variables be the class variables, so they will not get out of scope after ProgramStarted is done.


#14

@ Architect - Excellent. Thank you. That completely solved the timer issues. I assume the randomness of delay was due to the GC running.

So when it crashed with those “Exception System.Exception - CLR_E_WRONG_TYPE (1)” exceptions it was because it was running invalid code from an no longer valid part of memory, right?? Could it be possible to corrupt the firmware when an exception of this type happens? That could explain why I had sometimes to re-flash the firmware to make it work again.

Well, I still have this crazy errors:

I will keep trying to figure out why this is still happening… In the meantime I already ordered an G120HDRv2 to compare the results…


#15

That exception puzzles me. I’ve never seen it before and I see that you get it in random places. I would try reinstall everything from scratch including Net MF, you do have QFE2, right?


#16

@ Architect - It puzzles me too. Yes I had 4.2 QFE2.

I did what you suggested, uninstalled everything, including NetMF and reinstalled everything again “by the book”, first the NetMF, then the setup from GHI (4.2.9.0).
It all went well, so just to be sure, I flashed the firmware with the 4.2.9.1 hotfix.
Launched VS2010 and started a new Gadgeteer project.
Right after the project creation there was an error in the line “using Gadgeteer.Networking;” the reference was just not there. The Gadgeteer reference added by the project creation was version 2.41 which was odd. Also the “Add references” window didn’t had the Gadgeteer.* namespaces.

So I removed everything again and then installed everything, but manually this time: package by package.
After doing so, I opened VS2010 and started again a new Gadgeteer project. This time the Gadgeteer assembly was version 2.42.0.0 and it looked nice.

By copying and pasting the previous code into this new project, I deployed and I ran it successfully. It ran successfully for a lot of times as I was stopping, correcting, deploying and debugging the code.

Until it started to happen again when I started to use Wifi code. Also, deployment sometimes seams to gets interrupted by a wifi firmware update and then it resets (disconnecting and reconnecting the USB) by itself. The deployment also became much harder without a firmware re-flash.

I am using WinUSB drivers again now. After the re-installation I was using the GHI interface debug drivers, but these automatic resets also reset the PC (I can control when I do it, but not these automatic ones). It would be nicer that this wifi firmware update were only triggered by our code and not automatically (by adding a method like IsFirmwareCurrupt or something).

I also tried with my other 4.2 board (cerberus) and it is working, but this is the only one that has wifi… so not really of a test.

So far, without initializing the wifi board the code runs and deploys everytime without a glitch. There maybe something there. At least with my configuration…


#17

Hi,
I think, you should post your actual code.
Regards
Roland


#18

@ CarlosC - If you get an error on this “using Gadgeteer.Networking;” it’s because the Gadgeteer.dll is 4.1 and not 4.2

A few of us have issue that when you create a 4.2 gadgteer project VS actually references 4.1

I cant find the reason why and i have to remove the gadgeteer.dll and re add 4.2 every time a create a new project


#19

@ Justin - Thanks, also came to that conclusion. But since I’m having CLR errors I did not wanted to take the change that some installation might have gone wrong. So I reinstalled GHI, Gadgeteer core and NetMF again (using the msi and not the setup.exe) and it was installed right this time.

@ RoSchmi - Sure, here is my code. I just want to periodically request a http page using the wifi link:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace GadgeteerApp7
{
    public partial class Program
    {

        GT.Timer g_tmBlink;
        GT.Timer g_tmWifiScan;

        Thread g_tWifiScan;
        Thread g_tKeepAlive;

        AutoResetEvent g_areWifiScan;
        AutoResetEvent g_areKeepAlive;

        GHI.Premium.Net.WiFiRS9110 g_wifi;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            /*******************************************************************************************
            Modules added in the Program.gadgeteer designer view are used by typing 
            their name followed by a period, e.g.  button.  or  camera.
            
            Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
                button.ButtonPressed +=<tab><tab>
            
            If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            g_tmBlink = new GT.Timer(1000);
            g_tmBlink.Tick += new GT.Timer.TickEventHandler(tmBlink_Tick);

            g_areWifiScan = new AutoResetEvent(false);
            g_tWifiScan = new Thread(new ThreadStart(WifiScan));
            g_tWifiScan.Start();

            g_tmWifiScan = new GT.Timer(20000);
            g_tmWifiScan.Tick += new GT.Timer.TickEventHandler(tmWifiScan_Tick);

            g_areKeepAlive = new AutoResetEvent(false);
            g_tKeepAlive = new Thread(new ThreadStart(KeepAlive));
            g_tKeepAlive.Start();

            g_tmBlink.Start();
            g_tmWifiScan.Start();
        }

        void tmWifiScan_Tick(GT.Timer timer)
        {
            g_areWifiScan.Set();
        }

        void tmBlink_Tick(GT.Timer timer)
        {
            Gadgeteer.Program.Mainboard.SetDebugLED(true);
            Thread.Sleep(100);
            Gadgeteer.Program.Mainboard.SetDebugLED(false);
        }

        void WifiScan()
        {
            string cfg_wifi_ssid = "YOUR_SSID";
            string cfg_wifi_psk = "YOUR_WPA2_PSK";

            // Init wifi - You can try with G120.Pin or not. It's the same
            g_wifi = new GHI.Premium.Net.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2,
                (Microsoft.SPOT.Hardware.Cpu.Pin)(32 + 10), // GHI.Premium.Hardware.G120.Pin.P1_10
                (Microsoft.SPOT.Hardware.Cpu.Pin)((2 * 32) + 11), // GHI.Premium.Hardware.G120.Pin.P2_11,
                (Microsoft.SPOT.Hardware.Cpu.Pin)(32 + 9) // GHI.Premium.Hardware.G120.Pin.P1_9,
            );

            if (!g_wifi.IsOpen)
                g_wifi.Open();

            GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(g_wifi);

            if (!g_wifi.NetworkInterface.IsDhcpEnabled)
                g_wifi.NetworkInterface.EnableDhcp();

            if (!g_wifi.NetworkInterface.IsDynamicDnsEnabled)
                g_wifi.NetworkInterface.EnableDynamicDns();

            g_wifi.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(g_wifi_NetworkAddressChanged);

            while (true)
            {
                g_areWifiScan.WaitOne();
                Debug.Print("Wifi");

                if (g_wifi.IsLinkConnected)
                {
                    try
                    {
                            // if don't have an address, renew DHCP lease
                            if ("0.0.0.0".CompareTo(g_wifi.NetworkInterface.IPAddress) == 0)
                            {
                                Debug.Print("Renew DHCP lease");
                                g_wifi.NetworkInterface.RenewDhcpLease();
                            }
                    }
                    catch (System.Exception e)
                    {
                        Debug.Print(e.ToString());
                    }

                    g_areKeepAlive.Set(); // signal the keepalive
                    Debug.Print("KA signalled");
                }
                else
                {
                    // try to connect

                    GHI.Premium.Net.WiFiNetworkInfo linkTo = null;
                    try
                    {
                        GHI.Premium.Net.WiFiNetworkInfo[] scanResults = g_wifi.Scan();

                        foreach (GHI.Premium.Net.WiFiNetworkInfo ni in scanResults)
                        {
                            if (cfg_wifi_ssid.CompareTo(ni.SSID) == 0)
                            {
                                linkTo = ni;
                                Debug.Print(ni.SSID + " found with " + ni.RSSI + "dB");
                                break;
                            }
                        }
                    }
                    catch (System.Exception e)
                    {
                        Debug.Print("wifi.scan exception: " + e.ToString());
                    }

                    try
                    {
                        if (linkTo != null)
                            g_wifi.Join(linkTo, cfg_wifi_psk);
                    }
                    catch (GHI.Premium.Net.NetworkInterfaceExtensionException e)
                    {
                        Debug.Print("wifi join exception: " + e.ToString());
                    }
                }
            }
        }

        void g_wifi_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("network address changed");
        }

        void KeepAlive()
        {
            while (true)
            {
                g_areKeepAlive.WaitOne();

                Debug.Print("KeepAlive started");

                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.ietf.org/rfc/rfc2616.txt"); // as System.Net.HttpWebRequest;

                //req.Timeout = 5000;
                req.KeepAlive = false;

                try
                {
                    Debug.Print("getting response...");
                    System.Net.WebResponse resp = req.GetResponse();
                    Debug.Print("GetResponse returned.");
                    if (resp != null)
                    {
                        try
                        {
                            string[] heads = resp.Headers.AllKeys;
                            foreach (string head in heads)
                                Debug.Print(head + ": " + resp.Headers[head]);
                        }
                        catch (System.Exception e) { Debug.Print("headers exception: " + e.ToString()); }

                        resp.Close();
                        resp.Dispose();
                    }
                }
                catch (System.Exception e)
                {
                    Debug.Print("Exception: " + e.ToString());
                }

                if (req != null)
                    req.Dispose();

                Debug.Print("KeepAlive ended");

            } // while
        }

    }
}


Can you make this code run for a while without getting stuck or crash the CLR? I also have tried different combinations to accomplish my goal (by reusing the httpwebrequest, using the webrequest instead, etc), but the result is the same.

Thanks for your time.