Recently, we encountered an issue in production where multiple devices failed to mount the file system. The issue occurs during FileSystem.Mount()
The problem then magically seems to fix itself by simply inserting the USB stick into a PC and then inserting it into the device again.
Before inserting it into a pc:
Disk management details of the usb stick:
After inserting it into a pc:
using GHIElectronics.TinyCLR.Devices.UsbHost;
using System;
using Hardware.Platform.Mappings;
using GHIElectronics.TinyCLR.Devices.Storage;
using GHIElectronics.TinyCLR.IO;
using System.IO;
namespace Hardware.USB
{
public class USBController : IUSBController
{
IDriveProvider _driver;
StorageController _storageController;
public DriveInfo[] _driveInfo;
public USBController(StorageMapping controller)
{
_storageController = controller.Controller;
var usbHostController = UsbHostController.GetDefault();
usbHostController.OnConnectionChangedEvent +=
UsbHostController_OnConnectionChangedEvent;
usbHostController.Enable();
}
public IDriveProvider Driver
{
get { return _driver; }
}
public StorageController Controller
{
get { return _storageController; }
}
private void UsbHostController_OnConnectionChangedEvent
(UsbHostController sender, DeviceConnectionEventArgs e)
{
switch (e.DeviceStatus)
{
case DeviceConnectionStatus.Connected:
switch (e.Type)
{
case BaseDevice.DeviceType.MassStorage:
try
{
_driver = FileSystem.Mount(_storageController.Hdc);
_driveInfo = DriveInfo.GetDrives();
}
catch (Exception ex)
{
Logger.Logger.Fatal("USBController | " + ex.ToString());
}
break;
default:
Logger.Logger.Debug("USBController | USB device is not storage!");
break;
}
break;
case DeviceConnectionStatus.Disconnected:
Logger.Logger.Debug("USBController | Device Disconnected");
FileSystem.Unmount(_storageController.Hdc);
_driveInfo = DriveInfo.GetDrives();
//Unmount filesystem if it was mounted.
break;
case DeviceConnectionStatus.Bad:
Logger.Logger.Debug("USBController | Bad Device");
break;
}
}
}
}