Main Site Documentation

SD bug?


#1

I have an issue when I try to write to in SD.

This is my code:

public static void SDWrite(string text, string filename, FileMode modo)
{
	try
	{
		PersistentStorage sdPS = new PersistentStorage("SD");// Create a new storage device            
		
		sdPS.MountFileSystem();// Mount the file system

		string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;// Access storage device
		FileStream FileHandle = new FileStream(rootDirectory +
				 @ "\" + filename, modo);
		
		byte[] data = Encoding.UTF8.GetBytes(text+"\r\n");

		FileHandle.Write(data, 0, data.Length);
		FileHandle.Flush();
		FileHandle.Close();

		sdPS.UnmountFileSystem();// Unmount file system
		sdPS.Dispose();
	}
	catch (IOException ex) //???
	{
		dataLogger.addErrorLog(ex.Message, "errores.txt");
		throw new SDIOException();                
	}
	catch
	{
		throw new ApplicationException("Exception");
	}
}

This is the error:

[quote]#### Exception System.IO.IOException - CLR_E_FILE_IO (4) ####

Message:

Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000]

Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022]

An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll[/quote]

I’ve been looking for some info in Internet and I have found some interesting things:


Any idea??


#2

you need to allow netmf to close it before disposing; this has been discussed here before. I think the workaround is to just thread.sleep(1000) before disposing the SD object - search a bot more and you’ll find how others did it.


#3

Thanks for the answer, but this does not work…

I have been some test and, if I use this code and an old class for LCD (it uses Glide so different layout, different button events definitions, etc.) all works ok.

Can this be related? :o


#4

If you step in code one line at once, does it work?


#5

No it doesn’t. I’ll try to explain:

I have this:

{
	...
	...
	some code here...
	...
	SDAccess.SDWrite(str, "settings.xml", FileMode.Create);
	...
	some more code...
	...
}

If I use step by step execution, I get the exception when all code block is executed, after the “}”