In the original NETMF code we polled the Microsoft.SPOT.Hardware.UsbClient.UsbController.PortState for state Running to detect when a computer was connected to the USB Client interface.
In TinyCLR we dont have that state? (we dont have port state, only device state)
It looks like this is changing state only if you AttachLogicalUnit and enable, so this works:
var sd = StorageController.FromName(SC20260.StorageController.SdCard);
var usbclientController = UsbClientController.GetDefault();
var usbclient_masstorage = new MassStorage(usbclientController);
usbclient_masstorage.AttachLogicalUnit(sd.Hdc);
usbclient_masstorage.Enable();
But i want the sd card as logging storage as long that there is no cable attached.
In NETMF this worked like:
You need Enable() to start USBClient. To enable you need add AttachLogicalUnit.
Whenever you want to work with PC or listen event from PC, need to be in enabled state.
Whenever you want to work with SITCore File System, need to be in disabled state.
As I understand, your code should become like this:
private static StorageController Mysd;
private static UsbClientController MyusbclientController;
private static MassStorage usbclient_masstorage;
static void DoTestSD2()
{
Mysd = StorageController.FromName(SC20260.StorageController.SdCard);
MyusbclientController = UsbClientController.GetDefault();
usbclient_masstorage = new MassStorage(MyusbclientController);
usbclient_masstorage.DeviceStateChanged += Usbclient_masstorage_DeviceStateChanged;
usbclient_masstorage.AttachLogicalUnit(Mysd.Hdc);
usbclient_masstorage.Enable();
while (true)
{
if (usbclient_masstorage.DeviceState == DeviceState.Configured)
{
// Masstorage connect and ready
connected = true;
Debug.WriteLine("Connected");
}
else
{
// If cable was disconnected
if (connected) // was connected
{
connected = false;
Debug.WriteLine("Disconnecting....");
usbclient_masstorage.Disable(); // No more event
// Mount file system
//....
// Umount system
usbclient_masstorage.Enable(); // Enable again to listen event for state changed.
}
else
{
// Do something else
}
}
Thread.Sleep(1000);
}
}
static bool connected = false;
private static void Usbclient_masstorage_DeviceStateChanged(GHIElectronics.TinyCLR.Devices.UsbClient.RawDevice sender, DeviceState state)
{
}
So is Sitcore is using the sd card as logging device, it’s not possible to detect the connection plugged in between device and computer? Is this correct?
If this is the case, can we like AttachLogicalUnit with a “dummy” or so just to get the detection and replace this with sd.hdc after?
Basicly the sitcore appliaction is active on multiple interfaces, it’s logging data to the sd card during that process. At some point you have a service engineer on site that needs to extract this data, and if needed deploy updates over the USB Client interface. The only interface availible to the service engineer is the USB Client connection, so detecting service engineer that is connecting his laptop is the trigger we have to be able to implement. But before this event the sd card must be availible for logging.
Can you have USB running as something else (eg CDC) and then switch it to Mass Storage programmatically once you see a connection?
We do this with NetMF, I’m not familiar enough with TinyCLR to know if it is possible.