Spider USB Host - usb stick


I’ve become quite frustrated with the SD card module as it doesn’t work for me, so now
I was trying to see if I could read a usb stick.

I had found some code which I tried and I can see that the device is connected, however,
I do not see any way to read the directory / access the file system.

I don’t know why, but this assembly below does not exist for me and I can see no way to add it:
using GHIElectronics.NETMF.IO; // ?? It is not listed among the references for adding so

I am not able to do things like:

    FileStream file = new FileStream(storageDevice.Volume.RootDirectory + "\\small file.txt", FileMode.Open);
     byte[] small_file_buffer = new byte[file.Length];
    file.Read(small_file_buffer, 0, small_file_buffer.Length);

So, is there another way to see the files??




here https://www.ghielectronics.com/community/codeshare/entry/630
is a sample on how to scan all usb devices for mass storage and get the root directory path.
In FW 4.2.x you should add the following references (dll’s)

this should work then (SD as well).

VS Express or Professional does not make a difference in what libraries (dll’s) you can add as reference to your project.

Thanks Reinhard but I see something that is giving me all kinds of problems in your code (from your link):

ps = new PersistentStorage(“SD”); This is a problem for me on SD or usb device.

When I execute the above line I get a popup window that says:

An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHI.Premium.IO.dll

This is also (I believe) the root of my problem for SD. For some reason, I am not able to
create a PersistentStorage object.

Here is some simple code I tried for usb:

   static void DeviceConnectedEvent(USBH_Device device)
        PersistentStorage ps;
        ps = null;
        Debug.Print("Device connected...");
     Debug.Print("ID: " + device.ID + ", Interface: " +
              device.INTERFACE_INDEX + ", Type: " + device.TYPE);
        ps = new PersistentStorage(device); // error occurs when I step over this line

I’ve been wondering - Do you think maybe Visual studio 2012 express will not allow me to
do this and only if I upgrade I would be able???
Is anyone using express able to execute above code??


I can not imagine that the Express edition would do this.
Actually the exception is thrown on the device, not in VS.
The limitations of the Express editions are that you can not install all types of VS plugins and some tools are missing.
But any code you can compile must be executable as well.

But the exception at this line of code is strange.

Are you using Gageteer or plain NETMF?
Which HW do you use?
How does you code look like that fires the DeviceConnectedEvent?

I bought the spider starter kit so am using the spider with the usb host module.

I have already some success with the gadgeteer as I’ve been able to use the usb serial,
ethernet, button, etc - everything has worked ok except for storage devices (SD & usb).

So, I’m creating a gadgeteer project and using the diagram tool to setup a spider with
usb host module and it generates the appropriate code that calls programStarted().

In that routine, I copied this code:
void ProgramStarted()
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
USBHostController.DeviceDisconnectedEvent += DeviceDisconnectedEvent;

     // Sleep forever

        Debug.Print("Program Started");

I have a usbstick connected to the usbhost module before I start the program.
When I run the program , the DeviceConnectedEvent routine gets called and I see it executes ok until it bombs when trying to create the persistent storage obj.

fyi, in addition to the popup window as explained before, here is the debug output from executing the routine:
Device connected…
ID: 2694771880, Interface: 0, Type: 6
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (4) ####
#### Message:
#### GHI.Premium.IO.PersistentStorage::PersistentStorage_Helper [IP: 0000] ####
#### GHI.Premium.IO.PersistentStorage::.ctor [IP: 0011] ####
#### usbTest.Program::DeviceConnectedEvent [IP: 004b] ####
#### GHI.Premium.USBHost.USBH_DeviceConnectionEventHandler::Invoke [IP: 0000] ####
#### GHI.Premium.USBHost.USBHostController::nativeEventDispatcher_OnInterrupt [IP: 0037] ####
#### GHI.Premium.System.InternalEvent::nativeEventDispatcher_OnInterrupt [IP: 0054] ####
A first chance exception of type ‘System.InvalidOperationException’ occurred in GHI.Premium.IO.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHI.Premium.IO.dll


Hmm, not sure, but is it possible that the Gageteer stuff already mounts the usb stick?

@ dave001 - When using Gadgeteer, having the USB host module included does things behind the scene – this is why Cobra II is able to still use PS – you will connect to the device connected event of the USB Host Module:

void usbHost_USBDriveConnected(UsbHost sender, GT.StorageDevice storageDevice)         
             Debug.Print("Found mass storage on USB Host...");
             Debug.Print("Connecting to device...");
                 FileStream file = new FileStream(storageDevice.Volume.RootDirectory + "\\small file.txt", FileMode.Open);
                 byte[] small_file_buffer = new byte[file.Length];
                 file.Read(small_file_buffer, 0, small_file_buffer.Length);
                 Debug.Print("Read Okay");
             catch (Exception error)
                 Debug.Print("Error reading file");
1 Like

Ok, James,

I had that code you posted before and I had thought I wasn’t able to compile that ,
but I tried it again and now it worked.

So, it seems like now the usb host module is able to read the usb stick.

So, for Spider I have to use what you have there and it is not able to use the persistent storage.

anyway, thanks a lot - I somehow got confused.

Now I have to go back to the sd module and see if there is a way to make it work as well.


It would be on any system that includes the USB Host module in either code or the designer.


Perhaps you could help me understand the SD Module -

I have another topic on this subject Spider SD Module- In this case, the program bombs in
the generated code whenever it tries to create a sdCard object.

   private void InitializeModules() {
        this.sdCard = new GTM.GHIElectronics.SDCard(5);  //This line bombs!!
        this.usbClientDP = new GTM.GHIElectronics.UsbClientDP(1);

You mentioned code behind the scenes, in this case, it seems persistentStorage is again at the root of the problem. I get the following when I the line above executes.

Using mainboard GHI Electronics FEZSpider version 1.0
#### Exception System.Exception - 0xffffffff (1) ####
#### Message:
#### GHI.Premium.IO.PersistentStorage::.ctor [IP: 0000] ####
#### GHIElectronics.Gadgeteer.FEZSpider::MountStorageDevice [IP: 0005] ####
#### Gadgeteer.Modules.GHIElectronics.SDCard::MountSDCard [IP: 000f] ####
#### sdtest2app.Program::InitializeModules [IP: 0006] ####
A first chance exception of type ‘System.Exception’ occurred in GHI.Premium.IO.dll
SDCard ERROR : Error mounting SD card - no card detected.

I suppose it could be the module itself is unable to detect a card. I have already tried 2
sd cards, one which I just bought a couple of days ago.

So, in this case, it bombs before program started gets called…

@ dave001 - It is possible that the module has issues, but more likely it may be an issue of interaction with that card specifically, and the card you bought may share similar hardware. Do you have another system you can test the SD card and module on?


sorry - I’ve been away from working on this for a few days and just now read your comment.

In answer to your question, No, I don’t have another system, I only have the spider
starter kit. Pretty much everything I have tried with other modules (ethernet, spi, etc)
has worked ok - ONly this SDModule is currently not working.

As for the SD cards themselves, I don’t know how similiar they may or may not be as far
as H/W. I don’t have the 8GB one with me today but I can say this:

One is a Sandisk 8GB for HD video that I use in my camera and the other is a Samsung 4GB. Both are configured for FAT and both work in my computer. The 8GB card was bought about 1 year ago, the 4GB was bought last week.

thanks -


Hello again -

Perhaps someone out there could give me a couple examples of
SD cards (make, size, etc) that they know for sure works with the
SD module.

Then I could give that a try …