Main Site Documentation

Example code for CDC in 4.3


#1

I am looking for an example how to do reading and writing in CDC. It seems a lot of changes were introduced with 4.3.

By the way, GHI documentation is talking about Write method only, Read method is missing.


#2

@ iamin - For the read methods go up the inheritance chain.


#3

@ iamin - The Stream property on the Cdc class can be used to read and write just like in 4.2. As Mike said, the Read method is just further up in the inheritance chain.


#4

I see. An example code would still be handy though.


#5

Why have you deleted your post?

I have tried your code, but [em]Cdc.DefaultDevice[/em] was never returning. Thus I was not getting virtual COM port.


#6

@ iamin -

Hi iamin,

For some reasons, CDC VCOM does not work on 4.3.2.0 as you saw. It was fixed yesterday, and we are also on the way to finish USB Client, can you please wait for next release, pls?
The example code will be same so keep that in your project.


#7

I see. I can wait.


#8

I try Cdc vsp = new Cdc(); but see Not supported exception.
I use Cerb platform and 2014 R2 SDK

Can you help to make cdc ?


#9

@ x893.00 - CDC is not supported on Cerb. If you need it, you will need another mainboard.


#10

Can GHI recommend board with STM32 MCU and USB CDC support ?


#11

None, you need one of our soms. You can also simply add ausb to serial chip on any of the serial ports.


#12

I know this way, but interest build-in usb support.
Thanks


#13

it works in 4.3.3. Did you try it yet? :slight_smile:
Here is fast food

public class Program
    {
       
        // socket 7, pin 3 => PA25 LMODE
        // Socket 10 for USB serial
        public static void Main()
        {
            // Check debug interface
            // If 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.

            InputPort button = new InputPort((Cpu.Pin)(0 * 32 + 4), false, Port.ResistorMode.PullUp); // PA4 - LDR1
            OutputPort led = new OutputPort((Cpu.Pin)(3 * 32 + 3), true);// PD3 led
            while (button.Read())
            {
                Thread.Sleep(100);
                led.Write(!led.Read());
            }
           
           
            Cdc cdc = new Cdc(0x1B9F, 0xF001, 0x100, 250, "GHI Electronics", "CDC VCOM", null, "CDC VCOM");
            cdc.Stream.ReadTimeout = 100000;
            cdc.Stream.WriteTimeout = 100000;            
            Controller.ActiveDevice = cdc;            
            Controller.ActiveDevice = cdc;
            while (Controller.State != UsbController.PortState.Running)
            {
                Debug.Print("Waiting to connect to PC...");
                Thread.Sleep(1000);
            }

            byte[] read = new byte[1];
            while (true)
            {
                //if (button.Read() == false)
                {

                    cdc.Stream.Read(read, 0, 1);
                    cdc.Stream.Write(new byte[] { (byte)(read[0]+1) });
                    if (read[0] != 0)
                    {
                        Debug.Print("You pressed: " + (char)read[0] + "\r\n");
                    }
                }
                Thread.Sleep(100);
            }
        }

    }