Main Site Documentation

CDC VCOM Issues


#1

Using the CDC example in the manual works flawlessly, however when I attempt to implement my own copy, such as:


        public static void Execute()
        {
            // Create serial endpoint
            SerialPort serial = new SerialPort("COM4",57600);

            // Open serial port
            serial.Open();

            try {
                // Remap serial port
                RemapSerialPort(serial);

                // Create buffer to read into
                byte[] buffer = new byte[512];

                // Update text
                LCDDriver.Line1.Text = "GPS to USB";
                LCDDriver.Line1.ScrollInterval = 0;

                LCDDriver.Line2.Text = "Select to Cancel";
                LCDDriver.Line2.ScrollInterval = 0;

                // 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 CDC
                USBC_CDC usb = USBClientController.StandardDevices.StartCDC();

                try {
                    // Set key down
                    bool key_down = false;

                    while (true) {
                        // Get key
                        LCDDriver.Key key = LCDDriver.CurrentKey;

                        // Process key
                        switch (key) {
                            case LCDDriver.Key.None: {
                                // Reset key down
                                key_down = false;

                                // Break
                                break;
                            }
                            case LCDDriver.Key.Select: {
                                // If key is not down
                                if (!key_down) {
                                    // Return
                                    return;

                                    // Flag key down
                                    //key_down = true;
                                }

                                // Break
                                break;
                            }
                        }

                        if (serial.IsOpen && serial.BytesToRead > 0) {
                            // Read in buffer from serial
                            int num_read = serial.Read(buffer,0,buffer.Length);

                            // Write out buffer to USB
                            if (num_read > 0 && USBClientController.GetState() == USBClientController.State.Running) usb.Write(buffer,0,num_read);
                        }

                        // Sleep for a bit
                        Thread.Sleep(10);
                    }
                } finally {
                    // Stop CDC
                    USBClientController.Stop();
                }
            } finally {
                // Close serial port
                serial.Close();
            }
        }

I get an error thrown on the StartCDC() call:

And I’m unsure why, am I missing something?


#2

Debug your program step by step and you should find the problem.
For example, I think you are calling Execute() two times in a row. So you are getting an exception when you start USB again…


#3

Yeh trying but not getting much luck. I don’t think I am calling it twice, Execute() is designed to execute serially so when it returns it should have released all resources before entering another Execute().


#4

Argh even this fails:


        public static void Main()
        {
            // Start CDC
            USBC_CDC usb = USBClientController.StandardDevices.StartCDC();

            try {
                //
                Thread.Sleep(5000);
            } finally {
                // Stop CDC
                USBClientController.Stop();
            }
        }


#5

Seems I AM calling StartCDC() twice however not in the way you might think. I have something like this:


    public static class SerialInterface
    {

        private static USBC_CDC cdc;
        private static string serial_line;
        private static object locker;
        private static bool thread_terminate;
        private static Thread thread;

        static SerialInterface()
        {
            cdc = USBClientController.StandardDevices.StartCDC();
            serial_line = String.Empty;
            locker = new object();
            thread_terminate = false;
            thread = new Thread(new ThreadStart(ThreadProc));
        }

However, although I’m not actually using this class at the moment the static class gets initialized, in the normal .NET framework this is usually on first reference.


#6

I was unclear from your post if you found the issue or still have a question? tia


#7

I found the issue, I was StartCDC()'ing in a static class I wasn’t using and just including it was enough for the static class to fire so there was a collision when I called StartCDC() again. This has now been rectified thankfully.


#8

Hi LloydK,

i was looking at your code and i was wondering how

works. For a project i now need to use the usb client as a virtual com port for the first time (we always just used it for debugging). Do you remap the serial port object to the cdc?

Regards,

Per


#9

Per,

have a look in the Fez Mini brochure: http://www.ghielectronics.com/downloads/FEZ/Mini/Broch_FEZ_Mini.pdf

It’s explained there how to remap the serial port on the Mini.


#10

Hi EriSan500 (Eric),

i will do that straight away :stuck_out_tongue: I must admit i never look at those since i only have emx and chipworks over here…

Regards,

Per


#11

hmmm that is about remapping it to pins, i thought LloydK was remapping a serial port object to the cdc object.

Regards,

Per


#12

CDC is a “virtual” serial port, there is no pins! So how are you trying to remap pins that do not exist? :slight_smile:


#13

Hi Gus,

i don’t know, that was what i was wondering. I was reading the code above and just saw that he is using a serial port, then calls his remap function and later on is using cdc. So i thought is he somehow remapping his cdc to a serial port ???

Regards,

Per