I am detecting the memory stick & mounting the file system…no problem
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
but when I try to add code to set up a file, it raises an exception:
FileStream FileHandle = new FileStream( "hello.txt", FileMode.Create); // exception happens here!!!
byte[] data =
Encoding.UTF8.GetBytes("This string was brought to you by big bear");
// write the data and close the file
FileHandle.Write(data, 0, data.Length);
FileHandle.Close();
// if we need to unmount
ps.UnmountFileSystem();
GHIElectronics.NETMF.USBHost.USBHostController::nativeEventDispatcher_OnInterrupt [IP: 0037] ####
#### GHIElectronics.NETMF.System.InternalEvent::nativeEventDispatcher_OnInterrupt [IP: 0054] ####
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
Is this the proper setup for a usb stick (to create/open a file)?
Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
namespace Test
{
class Program
{
// Hold a static reference in case the GC kicks in and disposes it
// automatically, note that we only support one in this example!
static PersistentStorage ps;
public static void Main()
{
// Subscribe to RemovableMedia events
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
// Subscribe to USB events
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
// Sleep forever
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
}
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is inserted.");
Debug.Print("Getting files and folders:");
if (e.Volume.IsFormatted)
{
string[] files = Directory.GetFiles(e.Volume.RootDirectory);
string[] folders =
Directory.GetDirectories(e.Volume.RootDirectory);
Debug.Print("Files available on " +
e.Volume.RootDirectory + ":");
for (int i = 0; i < files.Length; i++)
Debug.Print(files[i]);
Debug.Print("Folders available on " +
e.Volume.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.");
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is ejected.");
}
}
}
@ Hoyt, maybe you should start small and simple. I have written \USB\ as a way to explicitely define the path to the root directory. Otherwise, as described by the Code written by Eric, you can use
e.Volume.RootDirectory
Actullay I believe that U should take the code written by Eric as it is. Run it and try to check if you succeed to use it. Then try to modify it and if U need explanations do not hesitate to ask us.
below is the ghi code–it works & give the directories…then I add some more GHI sample (see where says “ADDED”) to create & write a file…then it bombs out …why?
this is with pendrive usb memory stick
using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using System.Text;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
namespace Test
{
class Program
{
// Hold a static reference in case the GC kicks in and disposes it
// automatically, note that we only support one in this example!
static PersistentStorage ps;
public static void Main()
{
// Subscribe to RemovableMedia events
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
// Subscribe to USB events
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
// Sleep forever
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
if (device.TYPE == USBH_DeviceType.MassStorage)
{
Debug.Print("USB Mass Storage detected...");
ps = new PersistentStorage(device);
ps.MountFileSystem();
Thread.Sleep(1111);
//========below is added==========
// 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
ps.UnmountFileSystem();
//==================
}
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is inserted.");
Debug.Print("Getting files and folders:");
if (e.Volume.IsFormatted)
{
string[] files = Directory.GetFiles(e.Volume.RootDirectory);
string[] folders =
Directory.GetDirectories(e.Volume.RootDirectory);
Debug.Print("Files available on " +
e.Volume.RootDirectory + ":");
for (int i = 0; i < files.Length; i++)
Debug.Print(files[i]);
Debug.Print("Folders available on " +
e.Volume.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.");
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print("Storage \"" + e.Volume.RootDirectory +
"\" is ejected.");
}
}
}
USB Mass Storage detected…
#### Exception System.IO.IOException - CLR_E_FILE_IO (4) ####
#### 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
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
I can’t test right now, but I suspect that your code to create the file should be moved to the RemovableMedia.Insert event handler, my suspicion is that you can only access the storage device once the media is registered as inserted. That is pure conjecture of course, but maybe worth a shot.