UsbClient. Alignment 64 bytes throw Exception

When I try to write to the USB buffer in multiples of 64 bytes, I get an exception:

    #### Exception System.Exception - CLR_E_PIN_UNAVAILABLE (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.UsbClient.Provider.UsbClientControllerApiWrapper::Write [IP: 0000] ####
    #### GHIElectronics.TinyCLR.Devices.UsbClient.RawDevice+RawStream::Write [IP: 006f] ####
    #### GHIElectronics.TinyCLR.Devices.UsbClient.Cdc+CdcStream::Write [IP: 0014] ####
    #### GHIElectronics.TinyCLR.Devices.UsbClient.RawDevice+RawStream::Write [IP: 0021] ####
    #### UsbSendBlank.Program::Main [IP: 00bd] ####

"System.Exception" in GHIElectronics.TinyCLR.Devices.UsbClient.dll

An exception is thrown when trying to send a terminating ZERO-LENGTH DATA PACKET.
Packets not multiples of 64 bytes are transmitted without exceptions.

Code:

static void Main()
        {
            var usbclientController = UsbClientController.GetDefault();
            var usbClientSetting = new UsbClientSetting()
            {
                Mode = UsbClientMode.Cdc,
                ManufactureName = "Manufacture_Name",
                ProductName = "Product_Name",
                SerialNumber = "12345678",
            };

            var cdc = new Cdc(usbclientController, usbClientSetting);

            cdc.DeviceStateChanged += (a, b) => Debug.WriteLine("Connection changed.");
            cdc.DataReceived += (a, count) => Debug.WriteLine("Data received:" + count);

            cdc.Enable();
            
            while (cdc.DeviceState != DeviceState.Configured) { Thread.Sleep(100); }

            Debug.WriteLine("UsbClient Connected");

            // The example will read data from port to dataR array
            // Copy dataR to dataW array, plus 1 for each element
            // Write dataW array back to port
            
            var buffer = new byte[256];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte) i;
            }
            while (true)
            {
                try
                {
                    cdc.Stream.Write(buffer);
                    Thread.Sleep(1000);
                }
                catch { }
            }
        }

try to read cdc.DeviceState, it has to be “DeviceState.Configured”.

If it is suspended, deattach… then that will throw exception.

Yes, the state cdc.DeviceState is “Configured”.

I connect the terminal and see all transmitted data regardless of the length of the array. However, when a packet is a multiple of 64 bytes (64, 128, 192, 256, etc.), exceptions are thrown, but the data is transmitted.

Try to reproduce with the following code while connected to a terminal:

protected static string[] DeviceStateDescriptions =
{
    "Detached",
    "Attached",
    "Powered",
    "Default",
    "Address",
    "Configured",
    "Suspended",
};

static void Main()
{
    var usbClientController = UsbClientController.GetDefault();
    var usbClientSetting = new UsbClientSetting()
    {
        Mode = UsbClientMode.Cdc,
        ManufactureName = "Manufacture_Name",
        ProductName = "Product_Name",
        SerialNumber = "12345678",
    };

    var cdc = new Cdc(usbClientController, usbClientSetting);
    cdc.DeviceStateChanged += (a, b) => Debug.WriteLine($"Connection changed to {DeviceStateDescriptions[(int)b]}");
    cdc.DataReceived += (a, count) => Debug.WriteLine("Data received:" + count);
    cdc.Enable();

    ushort length = 0;
    while (true)
    {
        Thread.Sleep(100);
        if (cdc.DeviceState != DeviceState.Configured) continue;

        try
        {
            length = unchecked((ushort)(length + 1));
            cdc.Stream.Write(new byte[length]);
            Debug.WriteLine($"length {length} is OK");
        }
        catch (Exception ex)
        {
            if (ex.Message == "Timeout.")
            {
                Debug.WriteLine($"length {length} is Timeout Exception. Check the data consumer connection");
                continue;
            }
            Debug.WriteLine($"length {length} is Write Exception");
        }
    }
}