Main Site Documentation

FEZ Cobra II (WiFi) - Odd behavior in FileSteam


#1

I’m having all kinds of troubletrying to read a file from the SD card… the weird thing is, I can write files to the SD card…
I’m using 4.2.9 with the newest hotfix firmware (about 2 or 3 weeks ago). I’ve tried a lot of things, the stack trace is just so terribly unhelpful.
I’m fairly sure the file exists, and is named correctly.


using System.Threading;
using System.Text;
using Microsoft.SPOT;
using System.IO;
using Microsoft.SPOT.IO;
using GHI.Premium.IO;
using Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace TestSDCard {
    public 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
        }

        private void InitializeModules() {
            // Initialize GTM.Modules and event handlers here.
        }

        public void ProgramStarted() {
            // ... check if SD is inserted

            // SD Card is inserted
            // Create a new storage device
            PersistentStorage sdPS = new PersistentStorage("SD");

            // Mount the file system
            sdPS.MountFileSystem();

            // Assume one storage device is available,
            // access it through NETMF
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @ "\PortalMonitor.ini", FileMode.Open, FileAccess.Read); // <<<< this is where the exception is thrown
            byte[] data = new byte[100];
            // write the data and close the file
            int read_count = FileHandle.Read(data, 0, data.Length);
            FileHandle.Close();
            Debug.Print("The size of data we read is: " + read_count.ToString());
            Debug.Print("Data from file:");
            Debug.Print(new string(Encoding.UTF8.GetChars(data), 0, read_count));

            // if we need to unmount
            sdPS.UnmountFileSystem();

            // ...
            Thread.Sleep(Timeout.Infinite);

        }
    }
}


The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCobra II version 1.0
    #### Exception System.IO.IOException - CLR_E_FILE_NOT_FOUND (1) ####
    #### Message: 
    #### System.IO.FileStream::.ctor [IP: 00f2] ####
    #### System.IO.FileStream::.ctor [IP: 0009] ####
    #### TestSDCard.Program::ProgramStarted [IP: 0022] ####
    #### TestSDCard.Program::Main [IP: 0015] ####
A first chance exception of type 'System.IO.IOException' occurred in System.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

The program '[4] Micro Framework application: Managed' has exited with code 0 (0x0).

Perhaps I’m missing something obvious?
Note: I do see the FILE_NOT_FOUND (in the stack trace…) Perhaps I’m referencing the SD card’s root directory funky?


#2

Sometimes a delay after mounting the SD card helps.


#3

Hi.
Try to format your card with Cobra. I had similar problem with my Panda II and it was fixed by: VolumeInfo.GetVolumes()[0].Format(“FAT”, 0); command. Maybe it helps to you as well.
Mike


#4

I have the same problem, an IOException in the constructor of VolumInfo right after mounting. A Thread.Sleep(500) does help but it is still a bug of course. When will it be fixed?


			if (PersistentStorage.DetectSDCard())
			{
				using (PersistentStorage ps = new PersistentStorage("SD"))
				{
					ps.MountFileSystem();

					// Thread.Sleep(500); This helps

					if (VolumeInfo.GetVolumes()[0].IsFormatted)
					{
						// etc.
					}
				}
			}

Debug output:


Using mainboard GHI Electronics FEZCobra II version 1.0
    #### Exception System.IO.IOException - CLR_E_FILE_IO (7) ####
    #### Message: 
    #### Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000] ####
    #### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####
A first chance exception of type 'System.IO.IOException' occurred in Microsoft.SPOT.IO.dll