Main Site Documentation

Failed to do anything with SD after using HttpWebRequest


#1

I am experiencing strange problem. I can create, delete files on SD. After using HttpWebRequest every attempt to create or delete files is failing.

void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            if (e.IsConnected)
            {
               
                File.Delete(@ "\SD\password");  //OK
                FileStream file = new FileStream(@ "\SD\password", FileMode.Create); //OK
                file.Close();
                file.Dispose();
                File.Delete(@ "\SD\password"); //OK

                HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(ShareConst.HttpMicrocontrolerUrl + "GetDateTime.php");
                _request.Method = "GET";
                var response = _request.GetResponse();
                file = new FileStream(@ "\SD\password", FileMode.Create); //FAILED
                File.Delete(@ "\SD\password"); //FAILED
            }
           
        }

Maybe it has something to do with the latest (NETMF and Gadgeteer Package 2013 R2) release.

[quote]The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCobra II version 1.0
Program Started
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
The thread ‘’ (0x4) has exited with code 0 (0x0).
The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (5) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 005c] ####
#### System.IO.FileStream::.ctor [IP: 0010] ####
#### LivingRoom.Program::wifi_WirelessConnectivityChanged [IP: 0094] ####
#### GHI.Premium.Net.WiFiRS9110::NetworkChangeExtension_NetworkAvailabilityChanged [IP: 0025] ####
#### GHI.Premium.Net.NetworkChangeExtension::OnNetworkChangeCallback [IP: 0099] ####
#### GHI.Premium.Net.NetworkChangeExtension+NetworkChangeExtensionListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll[/quote]


#2

Still nothing new.
Can someone please confirm this. I have a complete source code, small as possible as can be. Drivers are still NETMF and Gadgeteer Package 2013 R2.
The error is still: [quote]The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCobra II version 1.0
Program Started
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (5) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 005c] ####
#### System.IO.FileStream::.ctor [IP: 0010] ####
#### BugSDWriteAfterRequest.Program::wifi_WirelessConnectivityChanged [IP: 004b] ####
#### GHI.Premium.Net.WiFiRS9110::NetworkChangeExtension_NetworkAvailabilityChanged [IP: 0025] ####
#### GHI.Premium.Net.NetworkChangeExtension::OnNetworkChangeCallback [IP: 0099] ####
#### GHI.Premium.Net.NetworkChangeExtension+NetworkChangeExtensionListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll[/quote]

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.Presentation.Shapes;
using Microsoft.SPOT.Touch;

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

namespace BugSDWriteAfterRequest
{

    public partial class Program
    {
        string RouterIP = "192.168.1.1";
        string SubnetMask = "255.255.255.0";
        string[] DNS = new string[]{ "84.255.209.79", "84.255.210.79" };
        string IP = "192.168.1.201";
        string WirelessSSID = "WifiID";
        string WirelessPassword = "wifipassword";

        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (PersistentStorage.DetectSDCard())
            {
                var ps = new PersistentStorage("SD");
                ps.MountFileSystem();
                InitWireless();
            }
        }

        GHINET.WiFiRS9110 _wifi;
        void InitWireless()
        {
            _wifi = new GHINET.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_10, GHI.Hardware.G120.Pin.P2_11, GHI.Hardware.G120.Pin.P1_9, 4000);

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

            _wifi.WirelessConnectivityChanged += new GHINET.WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);

            _wifi.NetworkInterface.EnableStaticIP(IP, SubnetMask, RouterIP);
            _wifi.NetworkInterface.EnableStaticDns(DNS);

            GHINET.NetworkInterfaceExtension.AssignNetworkingStackTo(_wifi);

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

            if (scanResult != null)
            {
                for (int i = 0; i < scanResult.Length; i++)
                {
                    if (scanResult[i].SSID == WirelessSSID)
                    {
                        _wifi.Join(scanResult[i], WirelessPassword);
                    }
                }
            }
        }

        void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            if (e.IsConnected)
            {
                File.Delete(@ "\SD\password");   //ok
                FileStream file = new FileStream(@ "\SD\password", FileMode.Create);     //ok
                file.Close();
                file.Dispose();
                File.Delete(@ "\SD\password");   //ok

                HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(@ "http://google.si");
                _request.Method = "GET";
                var response = _request.GetResponse();
                file = new FileStream(@ "\SD\password", FileMode.Create);    //failed
            }
            else
            {
               
            }
            
        }
    }
}


#3

@ Makla - I haven’t tested it, but it seems one for GHI


#4

We are able to reproduce it almost 100% of the time. We will let you know once we find out more.


#5

Great and thanks.


#6

Could you move the code out of the event handler and into a different function? Don’t call that function from the handler though, use a bool to signal the event like:

void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
{
	connected = true;
}

void aDifferentThread()
{
	while (!connected)
		Thread.Sleep(10);

	//Place Networking and Filesystem code here
}

#7

I didn’t use the bool, but I think that AutoResetEvent is also good.
The error is this time CLR_E_INVALID_DRIVER (4): [quote]The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCobra II version 1.0
Program Started
RS9110 firmware version Number is 4.4.5
RS9110 driver version Number is 4.4.5
starting SD operations
The thread ‘’ (0x3) has exited with code 0 (0x0).
#### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (4) ####
#### Message:
#### Microsoft.SPOT.IO.NativeIO::GetAttributes [IP: 0000] ####
#### System.IO.FileStream::.ctor [IP: 005c] ####
#### System.IO.FileStream::.ctor [IP: 0010] ####
#### BugSDWriteAfterRequest.Program::StartThread [IP: 004e] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll[/quote]

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.Presentation.Shapes;
using Microsoft.SPOT.Touch;

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

namespace BugSDWriteAfterRequest
{

    public partial class Program
    {
        string RouterIP = "192.168.1.1";
        string SubnetMask = "255.255.255.0";
        string[] DNS = new string[]{ "84.255.209.79", "84.255.210.79" };
        string IP = "192.168.1.201";
        string WirelessSSID = "";
        string WirelessPassword = "";

        void ProgramStarted()
        {
            Debug.Print("Program Started");

            if (PersistentStorage.DetectSDCard())
            {
                Thread t = new Thread(StartThread);
                t.Start();
                var ps = new PersistentStorage("SD");
                ps.MountFileSystem();
                InitWireless();
            }
        }

        GHINET.WiFiRS9110 _wifi;
        void InitWireless()
        {
            _wifi = new GHINET.WiFiRS9110(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, GHI.Hardware.G120.Pin.P1_10, GHI.Hardware.G120.Pin.P2_11, GHI.Hardware.G120.Pin.P1_9, 4000);

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

            _wifi.WirelessConnectivityChanged += new GHINET.WiFiRS9110.WirelessConnectivityChangedEventHandler(wifi_WirelessConnectivityChanged);

            _wifi.NetworkInterface.EnableStaticIP(IP, SubnetMask, RouterIP);
            _wifi.NetworkInterface.EnableStaticDns(DNS);

            GHINET.NetworkInterfaceExtension.AssignNetworkingStackTo(_wifi);

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

            if (scanResult != null)
            {
                for (int i = 0; i < scanResult.Length; i++)
                {
                    if (scanResult[i].SSID == WirelessSSID)
                    {
                        _wifi.Join(scanResult[i], WirelessPassword);
                    }
                }
            }
        }

        AutoResetEvent RE = new AutoResetEvent(false);
        void wifi_WirelessConnectivityChanged(object sender, GHINET.WiFiRS9110.WirelessConnectivityEventArgs e)
        {
            if (e.IsConnected)
            {
                RE.Set();
            }
            else
            {               
            }            
        }

        void StartThread()
        {
            RE.WaitOne();
            Debug.Print("starting SD operations");

            File.Delete(@ "\SD\password");   //ok
            FileStream file = new FileStream(@ "\SD\password", FileMode.Create);     //ok
            file.Close();
            file.Dispose();
            File.Delete(@ "\SD\password");   //ok

            HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(@ "http://google.si");
            _request.Method = "GET";
            var response = _request.GetResponse();
            file = new FileStream(@ "\SD\password", FileMode.Create);    //failed
        }
    }
}


#8

An AutoResetEvent is definitely the better way to do it. Even though the error remains, it is best to keep the reset event method instead of doing work in the event handler. If possible, can you call ps.UnmountFileSystem() right after the line that fails? Sounds odd, but it fixed the problem on our end. (You’ll have to make ps a class variable).

Have you also tried different SD cards, along with formatting them?


#9

Yes, I did try with different SD card and I also try to format it in windows.
I did find something interesting. It seems that the problem is here:

var ps = new PersistentStorage("SD");

If I set this variable global, the error is no more.

Maybe this is expected maybe not, but it seems logic. God know what garbage collector do with PersistentStorage since ps has no references.


#10

That actually makes sense. Eventually it got collected.


#11

Yeah, makes perfect sense in hindsight. It was just getting garbage collected at some point after the if block.