Here is the complete section of code where mass storage and VCOM are used.
// all the mass storage/VCOM/dateset/mass storage stuff
try
{
led.Write(false);
USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();
// inserted a number of Thread.sleeps as a precaution.Since this only happens once on powerup we can suffer the delay
Thread.Sleep(200);
// Assume SD card is connected
PersistentStorage sd = new PersistentStorage("SD"); // instantiate SD card
ms.AttachLun(0, sd, " ", " ");
Thread.Sleep(200);
// enable host access
ms.EnableLun(0);
Thread.Sleep(200);
// Debug.Print("Attached and enabled SD Card & will see if it's running");
USBClientController.State state = USBClientController.GetState();
// Debug.Print("State " + state.ToString());
Thread.Sleep(200);
if (state == USBClientController.State.Running)
{
// Debug.Print("USBClientController.State is Running. will disable ms & attempt VCOM");
ms.DisableLun(0);
sd.UnmountFileSystem();
Thread.Sleep(2000);
sd.Dispose();
USBClientController.Stop();
Thread.Sleep(1000);
// this code segment is for an xxx second window to try and connect to VCOM
// Start CDC - requires that the FEZ is set for serial debug
USBC_CDC cdc = USBClientController.StandardDevices.StartCDC();
try
{
int l = 0;
while (true)
{
// Check if connected to PC
if (USBClientController.GetState() != USBClientController.State.Running)
{
// Debug.Print("Waiting to connect to PC... " + l.ToString());
l++;
}
else
{
led.Write(true);
byte delim = 13, lf = 12;
while (true)
{
// set this time infinite or at least very long if TeraTerm macro is the interface - settled on 60 seconds as a reasonable time
cdc.ReadTimeout = 60000;
Start:
byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Current ADS time is " + DateTime.Now.ToString() + "\r\n");
cdc.Write(bytes, 0, bytes.Length);
bytes = System.Text.Encoding.UTF8.GetBytes("To change the time use this format yyyy, mm, dd, hh, mm, ss\r\n");
cdc.Write(bytes, 0, bytes.Length);
/// Simple loop that reads single chars until delim byte
/// and returns the string. We could improve perf by reading more chars per read.
// private string ReadLine()
// Read and buffer chars until a newline char.
ArrayList line = new ArrayList();
byte[] buf = new byte[1];
int count = 0;
while ((count = cdc.Read(buf, 0, 1)) > 0)
{
// Debug.Print("A key was hit & there's a live connection count = " + count.ToString());
cdc.ReadTimeout = -1; // someone hit a key, wait forever for more characters
if (buf[0] == delim)
{
line.Add(buf[0]);
cdc.Write(buf, 0, buf.Length);
buf[0] = lf;
cdc.Write(buf, 0, buf.Length);
line.Add(buf[0]);
goto convert;
}
line.Add(buf[0]);
cdc.Write(buf, 0, buf.Length);
// byte echo = buf[0];
}
// Debug.Print("count after while segment = " + count.ToString());
if (count == 0) goto sdcard; // no key was hit before timeout
// Convert ArrayList to char[] using UTF8 encoding.
convert:
if (line.Count < 24)
{
// Debug.Print("here is line[0] " + line[0].ToString());
if ((byte)line[0] == 47) goto sdcard; //test for "/" key to goto mass storage
byte[] err = System.Text.Encoding.UTF8.GetBytes("Please try again. \r\n");
cdc.Write(err, 0, err.Length);
goto Start;
}
// Debug.Print(DateTime.Now.ToString());
byte[] inbytes = new byte[line.Count];
int[] cb = new int[line.Count];
for (int x = 0; x < inbytes.Length; x++)
{
inbytes[x] = (byte)line[x];
cb[x] = inbytes[x] - 48;
// Debug.Print(cb[x].ToString());
}
char[] chars = Encoding.UTF8.GetChars(inbytes);
// try - catch in case the typed characters are wrong
try
{
int y = cb[0] * 1000 + cb[1] * 100 + cb[2] * 10 + cb[3];
int m = cb[6] * 10 + cb[7];
int d = cb[10] * 10 + cb[11];
int h = cb[14] * 10 + cb[15];
int mn = cb[18] * 10 + cb[19];
int s = cb[22] * 10 + cb[23];
// Debug.Print(" here's y .....s print");
// Debug.Print(y.ToString() + ", " + m.ToString() + ", " + d.ToString() + ", " + h.ToString() + ", " + mn.ToString() + ", " + s.ToString());
// update the RTC with new time as received from keyborad input or from the ext pc clock if usingthe TeraTerm macro
RTCDS.SetDateTime(new DateTime(y, m, d, h, mn, s, 0));
//do I need both of these Utility statements ?
Utility.SetLocalTime(RTCDS.GetDateTime());
}
catch
{
byte[] err = System.Text.Encoding.UTF8.GetBytes("Please try again. \r\n");
cdc.Write(err, 0, err.Length);
}
}
}
}
}
catch
{
byte[] err = System.Text.Encoding.UTF8.GetBytes("Please try again. \r\n");
cdc.Write(err, 0, err.Length);
// throw new Exception("buggered up the input");
}
sdcard:
// Now reinstate mass storage if VCOM was never used or if user input a "/"
led.Write(false);
USBClientController.Stop();
Thread.Sleep(200);
// Debug.Print("OK, now starting ms2 for sdcard. will see if it is connected ");
try
{
USBC_MassStorage ms2 = USBClientController.StandardDevices.StartMassStorage();
// Assume SD card is connected
PersistentStorage sd2 = new PersistentStorage("SD");
ms.AttachLun(0, sd2, " ", " ");
// enable host access
ms.EnableLun(0);
Thread.Sleep(200);
// Check if connected to PC & MASS STORAGE UP AND RUNNING
i = 0;
while (USBClientController.GetState() != USBClientController.State.Running)
{
// Debug.Print("Waiting to connect to PC... " + l.ToString());
i++;
}
i = 0;
// this blinking LED loop put in to trace where the program is - here mass storage is set up 7 blinks
while (i < 8)
{
led.Write(false);
Thread.Sleep(700);
led.Write(true);
Thread.Sleep(300);
i++;
}
i = 0;
// Debug.Print("USBClientController.State is Running. You now have a mass storage device");
Thread.Sleep(Timeout.Infinite);
}
catch
{
// Debug.Print(" throw new Exception(SD card not detected)");
// throw new Exception("SD card not detected");
i = CatchBlink(led, 50);
}
}
else
{
ms.DisableLun(0);
sd.UnmountFileSystem();
Thread.Sleep(200);
sd.Dispose();
// Debug.Print("SD Card wasn't running);
}
}
catch
{
Debug.Print(" throw new Exception(SD card not detected)");
//changed blink to 15 to distinguish from inner catch
i = CatchBlink(led, 15);
}