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
}
}
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.
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.
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
}
}
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.
@ 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;
}
}