CDC Example for read

I try the CDC write example and it gives an exception, what am I doing wrong.

Exception System.InvalidOperationException - 0x00000000 (12)

#### Message: Operation timed out.
#### GHI.Usb.Client.RawDevice+RawStream::Read [IP: 007c] ####

Operation timed out.


                    while (true)
                    {
                        if (Controller.State == UsbController.PortState.Running)
                        {
                            if (false)
                            {
                                byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello world!\r\n");
                                cdc.Stream.Write(bytes, 0, bytes.Length);
                            }
                            else
                            {
                                // Read and write using the stream

                                byte[] readData = new byte[256];

                                Int32 bytesRead = 0;

                                bytesRead = cdc.Stream.Read(readData, 0, readData.Length);

                                if (bytesRead > 0)
                                {
                                    Debug.Print("**************USB: " + readData.ToString());
                                    //CommParser.ProRequest(readData, bytesRead);
                                }
                            }
                        }
                        else
                        {
                            Thread.Sleep(50);       //Do not remove this; if USB cable to PC removed, removing this will make the display get stuck
                        }
                    }

Which device and which sdk?

@ microt -

Following your code, it always call Write, so ReadTimeOut exception can not be happened.
Anyway, ReadTimeOut exception happened because there is nothing to read after period time.

It is SDK 4.0.30319
and G120

if (true), I just put for testing to see if the write works.
I can turn it to false and this issue will happen

@ microt -

Anyway, ReadTimeoutException is not a bug, just because there is no data coming after period time.

@ Dat -
Can I get a usb read sample code.
Only your write example works.

HI GHI

Any help/pointers is appreciated for a CDC Read Example code
My customer is killing me

My issue is my example is not working.
I am not sure what I am doing wrong. Lot of read time out exceptions are coming
To test it I have tera term connected on COM and the “Hello…” comes ok.
But what I type on the terminal is not received.

So I want an example that works, thanks for your help.

What we are trying to say is that this is normal the way you have your code, you read and there is no data coming so an exception is raised.

You can cash those exception and handle as you need.

I think this is what others are trying to say as I haven’t used this in a while.

cash == catch

2 Likes

@ microt -

[quote]To test it I have tera term connected on COM and the “Hello…” comes ok.
But what I type on the terminal is not received.
[/quote]

Where does the “Hello…” come from?
Where does the “Hello…” go ?

Once the CDC device is reset, the VCOM port (on PC) needs to be reset too, otherwise it won’t work, even Read or Write

I tried all that I know, Please help. I know you folks are busy.
There is some bug in CDC. This is the code.
The hello world keep printing. But it does not read and gives an exception.
even if I ignore the exception. It does not read

                    while (true)
                    {
                        if (Controller.State == UsbController.PortState.Running)
                        {
                            {
                                // Read and write using the stream

                                byte[] readData = new byte[256];

                                Int32 bytesRead = 0;

                                cdc.Stream.Write(bytes, 0, bytes.Length);

                                //if (cdc.Stream.CanTimeout)
                                //if (cdc.Stream. > 0)                              
                                {
                                    try
                                    {
                                        bytesRead = cdc.Stream.Read(readData, 0, readData.Length);
                                    }
                                    catch (Exception ex)
                                    {
                                        Program.DB_Print("USB TimeOut");
                                    }

                                    if (bytesRead > 0)
                                    {
                                        Program.DB_Print("**************USB: " + readData.ToString());
                                        //CommParser.ProRequest(readData, bytesRead);
                                    }
                                }
                            }
                        }
                        else
                        {
                            Thread.Sleep(50);       //Do not remove this; if USB cable to PC removed, removing this will make the display get stuck
                        }
                    }

Try changing


to

```cs]byte[] readData = new byte[1];[/code

No luck , same exception

@ microt - Set read timeout to -1.

@ microt -

here is code I just test 1 minute ago. In my code, the application read 1 byte then write back that byte +1. You can see it in picture below.

static  void TestCDC()
        {
            Cdc cdc = new Cdc(0x1B9F, 0xF001, 0x100, 250, "GHI Electronics", "CDC VCOM", null, "CDC VCOM");
            byte[] read = new byte[1];
            cdc.Stream.ReadTimeout = 100000;
            cdc.Stream.WriteTimeout = 100000;                       
            Controller.ActiveDevice = cdc;            
            while (Controller.State != UsbController.PortState.Running)
            {
                Debug.Print("Waiting to connect to PC...");
                Thread.Sleep(1000);
            }
            while (true)
            {
                int bytesRead  = cdc.Stream.Read(read, 0, 1);
                cdc.Stream.Write(new byte[] { (byte)(read[0] + 1) });
                if (bytesRead > 0)
                {
                    Debug.Print("You pressed: " + (char)read[0]);
                }
                Thread.Sleep(100);

            }
        }

I don’t see any wrong in your code unless


You should keep it 1 as iamin suggested, just easy for test, because I don't think you typed 256 characters.
I tested on latest version, although there is no issue with VCOM in few recent release version but highly recommend to update latest 4.3.7.9.
Tested on G120, as it is your device.

cdc.Stream.ReadTimeout = -1; – cannot be a negative value

It is G120

and version 4.3.6

@ microt - Yes, it can. Have you tried it?

Yes

@ microt - Use the latest SDK then… I have used -1 value and it worked for me jut fine. Here is a source code for [em]ReadTimeout[/em] method:

  public override int ReadTimeout
  {
	get
	{
	  return this.readTimeout;
	}
	set
	{
	  if (value < 0 && value != -1)
		throw new ArgumentOutOfRangeException("value", "value must not be negative.");
	  this.readTimeout = value;
	}
  }