I have the USB mass storage code working! The debugging issue is not solved. The code
is downloaded and the USB mass storage device is NOT mounted. However when the
device is reset it seems to always mount. To debug use the Debug | Attach to Process
method discussed above. The issue was the files were not always being saved adding this
statement solved the issues.
VolumeInfo.GetVolumes()[0].FlushAll()
For more information see the replay by StephW in this post.
https://www.ghielectronics.com/community/forum/topic?id=13546&page=1#msg138957
I have several applications written in VB that I would like to use a USB mass storage
device. Unfortunately VB is missing Microsoft.Spot.Io.RemovableMedia.Insert
needed to use USB mass storage. The code below uses a class library named USBExt.
The class library can be compiled to USBExt.dll that can be used in VB. The class library
and example files are below.
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using GHI.Pins;
using GHI.Usb.Host;
/// <summary>
/// Start a class libray and insert this code then build the dll to use in vb project
/// VB is missing Microsoft.Spot.Io.RemovableMedia.Insert needed to use USB mass storage
/// </summary>
namespace USBExt
{
public class MS
{
// Boolean varable to test for mounted mass storage device.
public static Boolean Mounted = false;
private static AutoResetEvent evt = new AutoResetEvent(false);
// Rem remove if not using PandaIII
public static OutputPort LEDRemoveable = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led1, false);
public static OutputPort LEDBeforeMount = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led2, false);
public static OutputPort LEDAfterMount = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led3, false);
public static OutputPort LEDInsert = new OutputPort(GHI.Pins.FEZPandaIII.Gpio.Led4, false);
/// <summary>
/// Mount USB Mass Storage
/// </summary>
public static void Mount()
{
Controller.MassStorageConnected += Controller_MassStorageConnected;
Controller.Start();
Debug.Print("Controller started");
}
/// <summary>
/// Connected to Mass Storage
/// </summary>
private static void Controller_MassStorageConnected(object sender, MassStorage massStorage)
{
LEDRemoveable.Write(true);
RemovableMedia.Insert += RemovableMedia_Insert;
LEDBeforeMount.Write(true);
massStorage.Mount();
LEDAfterMount.Write(true);
Mounted = true;
evt.Set();
}
/// <summary>
/// Media Inserted
/// </summary>
private static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
LEDInsert.Write(true);
evt.Set();
}
}
}
using GHI.Usb.Host;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using System;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHI.Pins;
public static class program
{
public static void Main()
{
Debug.Print("Connecting");
// The USB mass storage device will not be mounted in the debug mode
// When the application is reset the USB storeage device will be mounted and the files created
// To see debug statements use the Debug|Attach to Process see this post for more information
// https://www.ghielectronics.com/community/forum/topic?id=23879
USBExt.MS.Mount();
//Should mount much faster this could be changed
Thread.Sleep(3000);
Debug.Print("Mounted= " + USBExt.MS.Mounted.ToString());
if (USBExt.MS.Mounted)
{
try
{
int i;
for (i = 1; i <= 5; i++)
{
string min = DateTime.Now.Minute.ToString();
string sec = DateTime.Now.Second.ToString();
string st = min + sec;
int ln = st.Length;
using (StreamWriter sw = new StreamWriter("\\USB\\" + st + ".txt"))
{
sw.WriteLine(st);
sw.Close();
sw.Dispose();
}
Thread.Sleep(2000);
}
//*************The VolumeInfo... is what makes it work!!!!!
//See the reply by StephW
//https://www.ghielectronics.com/community/forum/topic?id=13546&page=1#msg138957
VolumeInfo.GetVolumes()[0].FlushAll();
Thread.Sleep(1000);
Debug.Print("Getting files");
string[] dir = Directory.GetFiles("\\USB");
for (i = 0; i <= dir.Length - 1; i++)
{
Debug.Print(dir[i].ToString());
}
//Turn off the leds
USBExt.MS.LEDAfterMount.Write(false);
USBExt.MS.LEDBeforeMount.Write(false);
USBExt.MS.LEDRemoveable.Write(false);
USBExt.MS.LEDInsert.Write(false);
}
catch (Exception ex)
{
Debug.Print("File error: " + ex.Message);
}
}
}
}
Option Explicit On
Option Strict On
Imports System
Imports System.Threading
Imports System.IO
Imports Microsoft.SPOT
Imports System.Text
Imports Microsoft.SPOT.IO
Imports GHI.Usb
Imports GHI.Usb.Host
Imports GHI.IO.Storage
Imports Microsoft.VisualBasic
'*****This is the class library created in C#
Imports USBExt
Namespace MassStorageTest
Public Module Module1
Sub Main()
Debug.Print("Connecting")
MS.Mount()
Thread.Sleep(3000)
Debug.Print("Mounted= " & USBExt.MS.Mounted.ToString)
If MS.Mounted Then
Dim dir() As String = Directory.GetFiles("\USB")
Debug.Print(dir.Length.ToString)
For i = 0 To dir.Length - 1
Debug.Print(dir(i).ToString)
Next
Dim min As String = String.Empty
Dim sec As String = String.Empty
Dim st As String = String.Empty
Dim ln As Integer = 0
Try
For i = 1 To 5
min = DateTime.Now.Minute.ToString
Thread.Sleep(100)
sec = DateTime.Now.Second.ToString
Thread.Sleep(100)
st = min & sec
ln = st.Length
Using fs = New FileStream("\USB\" & st & ".txt", FileMode.OpenOrCreate)
fs.Write(Encoding.UTF8.GetBytes(st), 0, ln)
fs.Close()
End Using
Debug.Print("Wrote: " & st & ".txt")
Thread.Sleep(2000)
Next
VolumeInfo.GetVolumes()(0).FlushAll()
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
End If
End Sub
End Module
End Namespace