Main Site Documentation

Can you use cdc.ReadTimeout to switch between VCOM and MS?


If you’ve set up a virtual COM port with cdc and specified a cdc.ReadTimeout = 20,000 what happens when the timeout is reached? For example in the following code snippet where will the program go if the user hasn’t entered any characters and the timeout is reached?

cdc.ReadTimeout = 20000;
                            // Simple loop that reads single chars until delim byte
                            ArrayList line = new ArrayList();
                            byte[] buf = new byte[1];
                            int count = 0;
                            while ((count = cdc.Read(buf, 0, 1)) > 0)
                                if (buf[0] == delim)
                                    cdc.Write(buf, 0, buf.Length); // echo to term
                                    buf[0] = lf;
                                    cdc.Write(buf, 0, buf.Length);
                                    goto convert;
                                cdc.Write(buf, 0, buf.Length);
                                // byte echo = buf[0];
                        // Convert ArrayList to char[] using UTF8 encoding.

Maybe the bigger question to ask is how would you get your system to automatically switch to a mass storage device if a user doesn’t connect to a virtual com port within a specified period of time. I’m trying to find a way to give the user a chance to input data without losing the automatic startup of the SD card as a mass storage device. I’d also like the program to switch to a data logging mode when there is no USB connection and the power is applied through Vin on the Panda II, but I know I can do that by checking the USBClientController.State.

I know I could force the user to open the COM port and then choose between data input or mass storage, but I’d like to know if there are any neat alternatives.


When it times out, you just get a read count of 0…
Usually cell phones start up as CDC and later, by the user choice, can switch to card reader mode.


That helps a lot, Mike. Thanks. Using a 20 second ReadTimeout drops me back into the code right after the while segment… At that point I jump to the place where I want to change to mass storage, stop the VCOM port with


and then use

USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();

to start up mass storage.

Another alternative is if the user does enter a character within 20 seconds I can switch the ReadTimeout back to -1 (infinite?) so they have a com link.

The bottom line is a user now sees 3 scenarios:

  1. Power up using Vin - system starts data logging

  2. Power up by plugging into USB
    [ulist] a. Do nothing and after a delay the Panda II turns into mass storage

    b. Connect to the VCOM port within a set time (20sec), enter a character, and you can communicate[/ulist]

Like the cell phone example you cite, the first character to show there is life at the other end of the VCOM could be sent by a macro that gives the user a couple of options for further interaction. I’ll work on that also