Hi everyone,
I’m new here and I’m not familiar with .NET MF (I’m learning it for less than a month), and this is my first serious attempt with handling USB storage.
I. Well, I wrote a big program for FEZ Cobra, where a small part is about sending a file to the flash drive connected via USB. I’m using one 4GB flash drive.
When I tried for the first time, it looked ok. At second attempt… I connected my pendrive to a PC to see if everything is ok. Well… It wasn’t. I saw such mess for the first time! There were few folders and files with names containing some letters and various nonalphanumeric characters. Some of them had size over 3GB. I wasn’t able to access or delete any of them. So I formatted the device.
Later I have extracted the part which is related to handling the flash drive and done several tests.
Here it is:
using System.IO;
using System.Text;
using System.Threading;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
using System;
using Microsoft.SPOT.IO;
using NetMf.CommonExtensions; //http://netmfcommonext.codeplex.com/
namespace MFConsoleApplication1
{
public class Program
{
private static PersistentStorage _ps;
public static void Main()
{
RemovableMedia.Insert += RemovableMedia_Insert;
RemovableMedia.Eject += RemovableMedia_Eject;
USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
Thread.Sleep(Timeout.Infinite);
}
static void DeviceConnectedEvent(USBH_Device device)
{
try
{
_ps = new PersistentStorage(device); //exception at first attempt after formatting the device
_ps.MountFileSystem();
}
catch (Exception)
{
}
}
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
if (!e.Volume.IsFormatted)
return;
var path = e.Volume.RootDirectory + @ "\" + Timestamp() + ".txt";
using (var logStream = new FileStream(path, FileMode.Create, FileAccess.Write)) //sometimes an exception
{
logStream.Write(Encoding.UTF8.GetBytes("hewhjw"), 0, 6);
File.SetAttributes(path, FileAttributes.ReadOnly);
logStream.Flush();
e.Volume.FlushAll();
logStream.Close();
}
}
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
_ps.UnmountFileSystem();
_ps.Dispose();
}
private static string Timestamp()
{
var datetime = RealTimeClock.GetTime().ToString();
datetime = datetime.Replace(" ", "_");
datetime = datetime.Replace(":", "");
datetime = datetime.Replace("/", "");
return datetime;
}
}
}
What have I seen? When I format the device (and maybe create some file there), always for the first time I get an exception in the line where the PersistentStorage constructor is called (unfortunately, I don’t remember what kind of exception, I will be able to check it tomorrow). When I connect the device again (without resetting Cobra), it is ok, but…
Sometimes PersistentStorage was created successfuly, but RemovableMedia.Eject event didn’t occur. It turned out that I have to format my flash drive then.
Sometimes, even if the device is freshly formatted and empty (I couldn’t discover what conditions are needed - it’s somehow random for me) I get the IO_Exception on the line where the FileStream constructor is called. It’s weird because as I read @ Microsoft, it occurs when using FileMode.CreateNew while there already exists a file with the same path, and my device is empty. I’ll try it again tomorrow but resetting the Cobra (60% probability) or formatting the device was fixing it.
Ok, when I got both devices set (with a bit of luck) into state when no Exception was thrown, what turned out… An example:
- Format the pen drive.
- Turn Cobra on.
- Insert the USB mass storage -> PersistentStorage() throws an exception
- Reconnect the USB mass storage -> Code execution analysis shows, that everything is fine
- Check on PC -> file created
- Once again -> file created
- Once again -> bush again!
This problem occurs with random attempt. I don’t know what is the problem. I only know, that the program stopped at writing to the FileStream for a little bit longer time.
Tomorrow I will try with another pen drive if I find, and I’ll write here what kind of exception is thrown when PersistentStorage constructor is invoked for the first time. Although, if someone knows already how to solve the problem, please help.
II. I saw, when I have a program in debug mode, and I simply subscribe DeviceConnectedEvent for USB device, when I run my program in debug mode, it detects USB device that has already been connected (before starting the program). When I power the device (Cobra, Domino, Mini) via power connector, it works in the same way. When I power the device via USB cable (without using debug mode), I have to reconnect the plug. Why? How to solve this? Or in the other way - do I have to solve this?
Regards,
Tomasz
P.S. One thing my mind recovered, not related… Did anyone use Glide? It’s easy to create GUI, but am I able somehow to handle OnTouchDown and OnTouchUp with this? I would like to change the button image when the image is pressed and released. OnTapEvent is not suitable for this.