I’m having two problems that I need help with. I’ve searched the forums and have seen similar problems but nothing exactly like what I am seeing. First I would like to simply read and write text files to the SD card to save settings and other information inside my application. To flush out the interfaces I started by simply using the example code, which I cannot get to work.
I formatted my 2gb Kingston card as Fat32 with 4k blocks. I created 4 directories on it (while still in my PC). I then inserted it into my FEZ Domino and ran the following example:
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
namespace Test
{
class Program
{
public static void Main()
{
// ...
// 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
// Micro Framework and display available files and folders:
Debug.Print("Getting files and folders:");
if (VolumeInfo.GetVolumes()[0].IsFormatted)
{
string rootDirectory =
VolumeInfo.GetVolumes()[0].RootDirectory;
string[] files = Directory.GetFiles(rootDirectory);
string[] folders = Directory.GetDirectories(rootDirectory);
Debug.Print("Files available on " + rootDirectory + ":");
for (int i = 0; i < files.Length; i++)
Debug.Print(files[i]);
Debug.Print("Folders available on " + rootDirectory + ":");
for (int i = 0; i < folders.Length; i++)
Debug.Print(folders[i]);
}
else
{
Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
}
// Unmount
sdPS.UnmountFileSystem();
}
}
}
It seems to work fine and lists the directories I had previously created accurately. Then I tried to run the next example to write a simple text file:
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using System.IO;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
namespace MFConsoleApplication1
{
public class Program
{
static void Main()
{
// ... 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 + @ "\hello.txt", FileMode.Create);
byte[] data =
Encoding.UTF8.GetBytes("This string will go in the file!");
// write the data and close the file
FileHandle.Write(data, 0, data.Length);
FileHandle.Close();
// if we need to unmount
sdPS.UnmountFileSystem();
// ...
Thread.Sleep(Timeout.Infinite);
}
}
}
When I get to the line “FileHandle.Write(data, 0, data.Length);” I get the following exception:
#### Exception System.IO.IOException - CLR_E_FILE_IO (1) ####
#### Message:
#### Microsoft.SPOT.IO.NativeFileStream::Write [IP: 0000] ####
#### System.IO.FileStream::Write [IP: 002a] ####
#### MFConsoleApplication1.Program::Main [IP: 0035] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
I’ve tried everything I can think of. I am connected to both USB and an external power supply. Any ideas?
The second SD related problem I’m having is with the mass storage feature. I’d like to be able to connect my device to a PC and have it mount as a drive so I can copy script files to it. I wired a small switch to the mode pads that allows me to toggle between USB and COM debug mode. Using the following example, I have my device in USB mode. I run it in VS to deploy it and it throws an exception (since it is in USB mode). I then flick the switch to COM mode and reset the Domino. I’d then expect if everything worked for Windows to recognize the device but nothing happens. I’m not sure where to go from here.
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;
namespace USBClient_Example
{
public class Program
{
public static void Main()
{
// Check debug interface
if (Configuration.DebugInterface.GetCurrent() ==
Configuration.DebugInterface.Port.USB1)
throw new InvalidOperationException("Current debug interface is USB. ");
// Start MS
USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();
// Assume SD card is connected
PersistentStorage sd;
try
{
sd = new PersistentStorage("SD");
}
catch
{
throw new Exception("SD card not detected");
}
ms.AttachLun(0, sd, " ", " ");
// enable host access
ms.EnableLun(0);
Thread.Sleep(Timeout.Infinite);
}
}
}
I know that’s a lot but any help for either problem would be appreciated. Thanks,
Dean