CDC code on SC20260D

Is the code bellow correct? If i leave out the flush, it will write until the exception is raised, add the flush no exceptions but in both cases i only receive the first write in the connected terminal.

This is how CDC is setup and activated:

image

try send one or few bytes to see.

If i write just the first byte each message then BytesToWrite is increasing 1 each time (what makes sence) but only the first byte of first message is received, exception is raised after 64 bytes if flush is not send.

We need to see a simple project that we can reproduce issue.

We just tested, when write single bytes, ByteToWrite is always zero, no need to flush, because not heavy enough to bother system.
Somehow connection between sender and receiver corrupted or incorrect then those bytes couldn’t be transferred. So timeout exp raised.

We tested here by TeraTerm on Window 10. No need Flushed() and everything looks fine to us.
Tested on 6300.

Send us your simple project so we can help to find issue.
And make sure use latest firmware 2.1.0.6300

I’m using 2.1.0.6200 so let me update first, maybe it is that simple :slight_smile:

There is no changed about cdc from 6200 and 6300 so you should be fine.

Are you using TereTerm to check data or what app? Weird to us that you see only first transaction.

I believe that the maximum CDC packet size is 64 bytes?

Sounds like some kind of packet overflow is happening.

Try writing 64 bytes and then wait for the data to clear output buffer before writing the next glob of data.

Running 6300 now on a SC20260D Dev board, mode pin in, debugging over serial and running this basic test app.

using GHIElectronics.TinyCLR.Devices.UsbClient;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace MFC_SC20100
{
    public class MainClass
    {
        public static Cdc MyDebugInterface;
        public static UsbClientController MyUsbClientController;

        static void Main()
        {
            var usbclientController = UsbClientController.GetDefault();

            var usbClientSetting = new UsbClientSetting()
            {
                Mode = UsbClientMode.Cdc,
                ManufactureName = "GHI",
                ProductName = "CDC Test MFC Debug interface",
                SerialNumber = "1.0",
            };

            // Create the interface
            MyDebugInterface = new Cdc(usbclientController, usbClientSetting);

            //Attach the events
            MyDebugInterface.DeviceStateChanged += MyDebugInterface_DeviceStateChanged;
            MyDebugInterface.DataReceived += MyDebugInterface_DataReceived;

            // Enable the interface
            MyDebugInterface.Enable();

            while (true)
            {
                try
                {
                    // Convert string to byte array
                    byte[] bytes = Encoding.UTF8.GetBytes("Hello World...");

                    // Write to the stream and force the data to the interface
                    Debug.WriteLine("Stream CanWrite : " + MyDebugInterface.Stream.CanWrite.ToString());
                    MyDebugInterface.Stream.Write(bytes, 0, bytes.Length);
                    //MyDebugInterface.Stream.Flush();
                    Debug.WriteLine("Stream BytesToWrite : " + MyDebugInterface.Stream.BytesToWrite.ToString());
                }
                catch
                {
                    // Disable the interface if there is an issue
                    int MyException = 1;
                }

                Thread.Sleep(500);
            }
        }

        private static void MyDebugInterface_DataReceived(RawDevice sender, uint count)
        {
            // Create byte array with same size as received bytes
            byte[] MyData = new byte[count];
            int MyDataCount = MyDebugInterface.Stream.Read(MyData);
            string MyDebugCommand = "";

            // Build the string
            for (int _c = 0; _c < MyDataCount; _c++)
            {
                MyDebugCommand += (char)(MyData[_c]);
            }

            // Raise event in startup class to process the message and diplay in output window for debugging
            Debug.WriteLine("Debug received : " + MyDebugCommand);
            //DebugReceived(MyDebugCommand);
        }

        private static void MyDebugInterface_DeviceStateChanged(RawDevice sender, DeviceState state)
        {
            Debug.WriteLine("Connection changed.");
        }
    }
}

Output window capture from visual studio:

Found debugger!

Create TS.

Loading Deployment Assemblies.

Attaching deployed file.

Assembly: mscorlib (2.1.0.0) Attaching deployed file.

Assembly: GHIElectronics.TinyCLR.Devices.UsbClient (2.1.0.0) Attaching deployed file.

Assembly: GHIElectronics.TinyCLR.Native (2.1.0.0) Attaching deployed file.

Assembly: Usb_cdc_test (1.0.0.0) Attaching deployed file.

Assembly: GHIElectronics.TinyCLR.Devices.Usb (2.1.0.0) Resolving.

The debugging target runtime is loading the application assemblies and starting execution.
Ready.

‘GHIElectronics.TinyCLR.VisualStudio.ProjectSystem.dll’ (Managed): Loaded ‘C:\tinyclr\MFC_SC20260\Test_20100\Usb_cdc_test\bin\Debug\pe…\GHIElectronics.TinyCLR.Devices.Usb.dll’, Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘GHIElectronics.TinyCLR.VisualStudio.ProjectSystem.dll’ (Managed): Loaded ‘C:\tinyclr\MFC_SC20260\Test_20100\Usb_cdc_test\bin\Debug\pe…\GHIElectronics.TinyCLR.Native.dll’, Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘GHIElectronics.TinyCLR.VisualStudio.ProjectSystem.dll’ (Managed): Loaded ‘C:\tinyclr\MFC_SC20260\Test_20100\Usb_cdc_test\bin\Debug\pe…\GHIElectronics.TinyCLR.Devices.UsbClient.dll’, Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘GHIElectronics.TinyCLR.VisualStudio.ProjectSystem.dll’ (Managed): Loaded ‘C:\tinyclr\MFC_SC20260\Test_20100\Usb_cdc_test\bin\Debug\pe…\Usb_cdc_test.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
Connection changed.
Connection changed.
Connection changed.
Connection changed.
Connection changed.
Stream CanWrite : True
Stream BytesToWrite : 0
Stream CanWrite : True
Stream BytesToWrite : 0
Stream CanWrite : True
Stream BytesToWrite : 0
Stream CanWrite : True
Stream BytesToWrite : 14
Stream CanWrite : True
Stream BytesToWrite : 28
Stream CanWrite : True
Stream BytesToWrite : 42
Stream CanWrite : True
Stream BytesToWrite : 56
Stream CanWrite : True
Stream BytesToWrite : 70
Stream CanWrite : True
Stream BytesToWrite : 84
Stream CanWrite : True
Stream BytesToWrite : 98
Stream CanWrite : True
Stream BytesToWrite : 112
Stream CanWrite : True
Stream BytesToWrite : 126
Stream CanWrite : True
Stream BytesToWrite : 140
Stream CanWrite : True
Stream BytesToWrite : 154
Stream CanWrite : True
Stream BytesToWrite : 168
Stream CanWrite : True
Stream BytesToWrite : 182
Stream CanWrite : True
Stream BytesToWrite : 196
Stream CanWrite : True
Stream BytesToWrite : 210
Stream CanWrite : True
Stream BytesToWrite : 224
Stream CanWrite : True
Stream BytesToWrite : 238
Stream CanWrite : True
Stream BytesToWrite : 252
Stream CanWrite : True
Stream BytesToWrite : 266
Stream CanWrite : True
Stream BytesToWrite : 280
Stream CanWrite : True
Stream BytesToWrite : 294
Stream CanWrite : True
Stream BytesToWrite : 308
Stream CanWrite : True
Stream BytesToWrite : 322
Stream CanWrite : True
Stream BytesToWrite : 336
Stream CanWrite : True
Stream BytesToWrite : 350
Stream CanWrite : True
Stream BytesToWrite : 364
Stream CanWrite : True
Stream BytesToWrite : 378
Stream CanWrite : True
Stream BytesToWrite : 392
Stream CanWrite : True
Stream BytesToWrite : 406
Stream CanWrite : True
Stream BytesToWrite : 420
Stream CanWrite : True
Stream BytesToWrite : 434
Stream CanWrite : True
Stream BytesToWrite : 448
Stream CanWrite : True
Stream BytesToWrite : 462
Stream CanWrite : True
Stream BytesToWrite : 476
Stream CanWrite : True
Stream BytesToWrite : 490
Stream CanWrite : True
Stream BytesToWrite : 504
Stream CanWrite : True
Stream BytesToWrite : 518
Stream CanWrite : True
Stream BytesToWrite : 532
Stream CanWrite : True
Stream BytesToWrite : 546
Stream CanWrite : True
Stream BytesToWrite : 560
Stream CanWrite : True
Stream BytesToWrite : 574
Stream CanWrite : True
Stream BytesToWrite : 588
Stream CanWrite : True
Stream BytesToWrite : 602
Stream CanWrite : True
Stream BytesToWrite : 616
Stream CanWrite : True
Stream BytesToWrite : 630
Stream CanWrite : True
Stream BytesToWrite : 644
Stream CanWrite : True
Stream BytesToWrite : 658
Stream CanWrite : True
Stream BytesToWrite : 672
Stream CanWrite : True
Stream BytesToWrite : 686
Stream CanWrite : True
Stream BytesToWrite : 700
Stream CanWrite : True
Stream BytesToWrite : 714
Stream CanWrite : True
Stream BytesToWrite : 728
Stream CanWrite : True
Stream BytesToWrite : 742
Stream CanWrite : True
Stream BytesToWrite : 756
Stream CanWrite : True
Stream BytesToWrite : 770
Stream CanWrite : True
Stream BytesToWrite : 784
Stream CanWrite : True
Stream BytesToWrite : 798
Stream CanWrite : True
Stream BytesToWrite : 812
Stream CanWrite : True
Stream BytesToWrite : 826
Stream CanWrite : True
Stream BytesToWrite : 840
Stream CanWrite : True
Stream BytesToWrite : 854
Stream CanWrite : True
Stream BytesToWrite : 868
Stream CanWrite : True
Stream BytesToWrite : 882
Stream CanWrite : True
Stream BytesToWrite : 896
Stream CanWrite : True
#### Exception System.Exception - 0x00000000 (1) ####
#### Message: Timeout.
#### GHIElectronics.TinyCLR.Devices.UsbClient.RawDevice+RawStream::Write [IP: 0090] ####
#### GHIElectronics.TinyCLR.Devices.UsbClient.Cdc+CdcStream::Write [IP: 002e] ####
#### MFC_SC20100.MainClass::Main [IP: 008a] ####
Exception thrown: ‘System.Exception’ in GHIElectronics.TinyCLR.Devices.UsbClient.dll
Stream CanWrite : True
Stream BytesToWrite : 14
Stream CanWrite : True
Stream BytesToWrite : 28
Stream CanWrite : True
Stream BytesToWrite : 42
Stream CanWrite : True
Stream BytesToWrite : 56
Stream CanWrite : True
Stream BytesToWrite : 70
Stream CanWrite : True
Stream BytesToWrite : 84
Stream CanWrite : True
Stream BytesToWrite : 98
Stream CanWrite : True
Stream BytesToWrite : 112
Stream CanWrite : True
Stream BytesToWrite : 126
Stream CanWrite : True
Stream BytesToWrite : 140
Stream CanWrite : True

Port is monitored by Device monitoring studio 8.10

With Tera term

That is why we need to see full your project :))).

When you call Enable(), you need to wait until the state is connected. You can check the state from event for simple just poll as our docs.

while (cdc.DeviceState != DeviceState.Configured);
 
// Now you can write/read
Debug.WriteLine("UsbClient Connected");

"

Added the state check, this is the full code:

using GHIElectronics.TinyCLR.Devices.UsbClient;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace MFC_SC20100
{
    public class MainClass
    {
        public static Cdc MyDebugInterface;
        public static UsbClientController MyUsbClientController;

        static void Main()
        {
            var usbclientController = UsbClientController.GetDefault();

            var usbClientSetting = new UsbClientSetting()
            {
                Mode = UsbClientMode.Cdc,
                ManufactureName = "GHI",
                ProductName = "CDC Test MFC Debug interface",
                SerialNumber = "1.0",
            };

            // Create the interface
            MyDebugInterface = new Cdc(usbclientController, usbClientSetting);

            //Attach the events
            MyDebugInterface.DeviceStateChanged += MyDebugInterface_DeviceStateChanged;
            MyDebugInterface.DataReceived += MyDebugInterface_DataReceived;

            // Enable the interface
            MyDebugInterface.Enable();
            while (MyDebugInterface.DeviceState != DeviceState.Configured) ;

            while (true)
            {
                try
                {
                    Debug.WriteLine("Device state : " + MyDebugInterface.DeviceState.ToString());
                    // Convert string to byte array
                    byte[] bytes = Encoding.UTF8.GetBytes("Hello World...");

                        // Write to the stream and force the data to the interface
                        Debug.WriteLine("Stream CanWrite : " + MyDebugInterface.Stream.CanWrite.ToString());
                        MyDebugInterface.Stream.Write(bytes, 0, bytes.Length);
                        //MyDebugInterface.Stream.Flush();
                        Debug.WriteLine("Stream BytesToWrite : " + MyDebugInterface.Stream.BytesToWrite.ToString());
                }
                catch
                {
                    // Disable the interface if there is an issue
                    int MyException = 1;
                }

                Thread.Sleep(500);
            }
        }

        private static void MyDebugInterface_DataReceived(RawDevice sender, uint count)
        {
            // Create byte array with same size as received bytes
            byte[] MyData = new byte[count];
            int MyDataCount = MyDebugInterface.Stream.Read(MyData);
            string MyDebugCommand = "";

            // Build the string
            for (int _c = 0; _c < MyDataCount; _c++)
            {
                MyDebugCommand += (char)(MyData[_c]);
            }

            // Raise event in startup class to process the message and diplay in output window for debugging
            Debug.WriteLine("Debug received : " + MyDebugCommand);
            //DebugReceived(MyDebugCommand);
        }

        private static void MyDebugInterface_DeviceStateChanged(RawDevice sender, DeviceState state)
        {
            Debug.WriteLine("Connection changed.");
        }
    }
}

Board has external power supply, i switched the cables and the ports, could there be a device issue? Result is still only 1 string visible, after this bytestowrite starts to increase.

We ran exactly your code, no changed anything:
It has to be something with your virtual port connection.

Also, as I remember, older TeraTerm requires to disconnect, reconnect again once the connection lost (if you re-deploy application, the connect of course lost).

Mine is 4.105 or newer no need to do that. Older, you may have to.

Yes, i noticed this on Tera Term, to overcome this i added a breakpoint just after enabling CDC, then start Tera Term and connect, then run the loop. Like you are saying, must be something with the port on PC side, most important for me at the moment is that this should work and i can continue the code port for this application :slight_smile: Thanks for the support !