Ive coded an application for a Panda II that automatically switches between opening a VCOM port, going to mass storage, or dropping into a data logging mode. My question is if there is a better or more efficient way than what Ive chosen, or is there a potential flaw in this approach. The sequence of how the code works is as follows:
[ol]1. On power up the program tests for a USB client connection by starting mass storage and then testing the state to see if its running.
-
If the state test doesnt return a running condition, power is coming from Vin (not USB) and the code stops mass storage and jumps to the data logger code.
-
If the state test shows mass storage is running, it will turn off mass storage and open a cdc VCOM port to see if a user opens a terminal session to interact with the system.
-
If the VCOM sees no input within a set period of time, VCOM is turned off and the system reopens mass storage so the user can access files on the SD card.[/ol]
I know there are other ways to accomplish this such as just opening a VCOM port and having a user choose what happens next, but for this application there is a need to have an auto start sequence.
Im also having a problem in that periodically on power up the Panda II doesnt even get to the first step in the code and just hangs. I think that is a separate topic though and Ill post it later ion another thread unless someone has some input now.
The code segment that accomplishes this is as follows. Ive deleted large sections of code that arent relevant to my question to make this more readable. Thanks in advance for any input.
using System;
using System.Text;
using System.IO;
using System.Threading;
using System.Collections;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware.LowLevel;
namespace PandaIIswitching
{
public class Program
{
public static void Main()
{
Debug.Print("Starting code execution");
Utility.SetLocalTime(RealTimeClock.GetTime());
// Set up leds for later use by data logger code
OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di52, true);
OutputPort bled = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
Debug.Print(DateTime.Now.ToString());
// Check debug interface
if (Configuration.DebugInterface.GetCurrent() == Configuration.DebugInterface.Port.USB1)
throw new InvalidOperationException("Current debug interface is USB. It must be changed to something else before proceeding. Refer to your platform user manual to change the debug interface.");
// START TESTING FOR USB CONNECTION TO SEE IF THE PANDA II IS BEING POWERED BY USB CLIENT CONNECTION
Debug.Print("OK, now starting ms for sdcard. will see if it is connected ");
USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();
PersistentStorage sd;
try
{
sd = new PersistentStorage("SD");
}
catch
{
throw new Exception("SD card not detected");
}
ms.AttachLun(0, sd, " ", " ");
ms.EnableLun(0);
Debug.Print("Attached and enabled SD Card & will see if it's running");
USBClientController.State state = USBClientController.GetState();
Debug.Print("State " + state.ToString());
if (state == USBClientController.State.Running)
{
bled.Write(true); // visual indication that execution got to this point
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);
// Start CDC
USBC_CDC cdc = USBClientController.StandardDevices.StartCDC();
try
{
// FOR CLARITY A LARGE SECTION OF CODE TO HANDLE TERMINAL INPUT WAS DELETED HERE
// THE IMPORTANT FEATURE IS THAT IF THERE'S NO INPUT WITHIN SO MANY SECONDS THE
// EXECUTION JUMPS BACK TO CREATE A MASS STORAGE DEVICE
goto sdcard;
}
catch
{
// ANY TERMINAL INPUT ERRORS ARE HANDLED HERE
}
sdcard:
// Now reinstate mass storage if VCOM was never used
USBClientController.Stop();
Thread.Sleep(200);
Debug.Print("OK, now starting ms2 for sdcard. will see if it is connected ");
USBC_MassStorage ms2 = USBClientController.StandardDevices.StartMassStorage();
// Assume SD card is connected
PersistentStorage sd2;
try
{
sd2 = new PersistentStorage("SD");
}
catch
{
throw new Exception("SD card not detected");
}
ms.AttachLun(0, sd2, " ", " ");
ms.EnableLun(0);
Debug.Print("Attached and enabled SD Card & will see if it's running");
USBClientController.State state2 = USBClientController.GetState();
Debug.Print("State2 " + state2.ToString());
if (state2 == USBClientController.State.Running)
{
Debug.Print("USBClientController.State is Running. You now have a mass storage device");
Thread.Sleep(Timeout.Infinite);
}
}
else
{
ms.DisableLun(0);
sd.UnmountFileSystem();
Thread.Sleep(200);
sd.Dispose();
Debug.Print("SD Card wasn't running - CONTINUE WITH DATA LOGGER");
}
// DATA LOGGER CODE IS HERE BUT HAS BEEN DELETED FOR CLARITY
}
}
}