我无法在基于自定义 USB CDC 类的 STM32 设备上接收超过 64 个字节

1 c usb serial-port cdc stm32

currently I try to sent 720 bytes from Windows application to custom STM32 device (now for testing purposes I use Blue Pill - STM32F103xxx). Ah, I forgot to point that I am totally newbie into programming :). So on device side I have 1000 bytes buffers for receiving and sending (Thanks to STMCube for this). Testing device with terminal program ( packets < than 64 bytes) works. Then I rework one of Microsoft examples to be able to sent more data to device. Used device driver on Windows is "usbser.sys". In short my console program do following:

  1. Calculate SINE weave (360) samples - 16 bytes size
  2. Sent them to USB Device as 720 bytes (byte size protocol for COM port) My problem is that no more than 64 bytes comes into device. Somewhere I read that reason for this can be into built in Rx,Tx Windows buffers (64 bytes long by mention somewhere on internet) and for this into code below I insert:
    • SetupComm(hCom,1000,1000) in hope that this will solve my troubles but nope. Below is "my" code, any ideas how I can fix this?
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <math.h>  
    
    #define PI 3.14159265
  
    void PrintCommState(DCB dcb)
    {
        //  Print some of the DCB structure values
        _tprintf(TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"),
            dcb.BaudRate,
            dcb.ByteSize,
            dcb.Parity,
            dcb.StopBits);
    }
    
    
    int _tmain(int argc, TCHAR* argv[])
    {
        DCB dcb;
        HANDLE hCom;
        BOOL fSuccess;
        const TCHAR* pcCommPort = TEXT("COM3"); //  Most systems have a COM1 port
        unsigned __int8 aOutputBuffer[720];// Data that will sent to device
        unsigned __int16 aCalculatedWave[360];// Data that will sent to device
        int iCnt; // temp counter to use everywhere 
    
        for (iCnt = 0; iCnt < 360; iCnt = iCnt + 1)
        {
            aCalculatedWave[iCnt] = (unsigned short)(0xFFFF * sin(iCnt * PI / 180));
            if (iCnt > 180) aCalculatedWave[iCnt] = 0 - aCalculatedWave[iCnt];
        }
    
        // 16 bit aCalculatedWaveto to 8 bit aOutputBuffer
        for (int i = 0, j = 0; i < 720; i += 2, ++j)
        {
            aOutputBuffer[i] = aCalculatedWave[j] >> 8; // Hi byte
            aOutputBuffer[i + 1] = aCalculatedWave[j] & 0xFF; // Lo byte
        }
    
        //  Open a handle to the specified com port.
        hCom = CreateFile(pcCommPort,
            GENERIC_READ | GENERIC_WRITE,
            0,      //  must be opened with exclusive-access
            NULL,   //  default security attributes
            OPEN_EXISTING, //  must use OPEN_EXISTING
            0,      //  not overlapped I/O
            NULL); //  hTemplate must be NULL for comm devices
    
        if (hCom == INVALID_HANDLE_VALUE)
        {
            //  Handle the error.
            printf("CreateFile failed with error %d.\n", GetLastError());
            return (1);
        }
        if (SetupComm(hCom,1000,1000) !=0)
            printf("Windows In/Out serial buffers changed to 1000 bytes\n");
        else
            printf("Buffers not changed with error %d.\n", GetLastError());
    
        //  Initialize the DCB structure.
        SecureZeroMemory(&dcb, sizeof(DCB));
        dcb.DCBlength = sizeof(DCB);
    
        //  Build on the current configuration by first retrieving all current
        //  settings.
        fSuccess = GetCommState(hCom, &dcb);
    
        if (!fSuccess)
        {
            //  Handle the error.
            printf("GetCommState failed with error %d.\n", GetLastError());
            return (2);
        }
    
        PrintCommState(dcb);       //  Output to console
    
        //  Fill in some DCB values and set the com state: 
        //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
        dcb.BaudRate = CBR_9600;     //  baud rate
        dcb.ByteSize = 8;             //  data size, xmit and rcv
        dcb.Parity = NOPARITY;      //  parity bit
        dcb.StopBits = ONESTOPBIT;    //  stop bit
    
        fSuccess = SetCommState(hCom, &dcb);
    
        if (!fSuccess)
        {
            //  Handle the error.
            printf("SetCommState failed with error %d.\n", GetLastError());
            return (3);
        }
    
        //  Get the comm config again.
        fSuccess = GetCommState(hCom, &dcb);
    
        if (!fSuccess)
        {
            //  Handle the error.
            printf("GetCommState failed with error %d.\n", GetLastError());
            return (2);
        }
    
        PrintCommState(dcb);       //  Output to console
    
        _tprintf(TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
        if (WriteFile(hCom, aOutputBuffer, 720, NULL, 0) != 0)
            _tprintf(TEXT("720 bytes successfully writed to Serial port %s \n"), pcCommPort);
        else
            _tprintf(TEXT("Fail on write 720 bytes to Serial port %s \n"), pcCommPort);
        return (0);
    }

Run Code Online (Sandbox Code Playgroud)

Cod*_*odo 5

USB批量端点实现基于流的协议,即无限的字节流。这与基于消息的协议形成对比。因此USB批量端点没有消息、消息开始或结束的概念。这也适用于 USB CDC,因为它基于批量端点。

\n

在较低的 USB 级别,字节流被分割成最多 64 字节的数据包。根据 USB 全速标准,数据包不能大于 64 字节。

\n

如果主机发送间隔超过 1 毫秒的小数据块,它们将在单独的数据包中发送和接收,看起来 USB 是一种基于消息的协议。但是,对于超过 64 字节的块,它们会被分割成更小的数据包。如果小块的发送间隔小于 1 毫秒,主机会将它们合并成更大的数据包。

\n

您的设计似乎要求对数据进行分组,例如问题中提到的 720 字节组。如果这是要求,则必须实现分组,例如通过首先发送组的大小然后发送数据。

\n

由于较大的组被分成 64 字节的块,并且为每个数据包调用接收回调,因此必须连接数据包,直到完整的组可用。

\n

另请注意当前代码中的一些问题(请参阅usbd_cdc_if.c,第 264 行):

\n
  USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);\n  USBD_CDC_ReceivePacket(&hUsbDeviceFS);\n  NewDataFromUsb = *Len;\n
Run Code Online (Sandbox Code Playgroud)\n

USBD_CDC_SetRxBuffer设置要接收的下一个数据包的缓冲区。如果您始终使用与本例中的 \xe2\x80\x93 相同的缓冲区 \xe2\x80\x93 ,则不需要。初始设置就足够了。但是,如果当前数据包不包含完整的组,则可以使用它来设置新的缓冲区。

\n

尽管有它的名字,但USBD_CDC_ReceivePacket没有收到数据包。相反,它会同意接收下一个包。仅当缓冲区中的数据已被处理并且缓冲区已准备好接收下一个数据包时才应调用它。您当前的实现存在缓冲区在处理之前被覆盖的风险,特别是如果您发送一组超过 64 个字节,这可能会导致数据包快速连续。

\n

请注意,此处未提及 Windows。Windows 代码似乎没问题。更改为 Winusb.sys 只会让您的生活变得更加困难,但不会让您的数据包大于 64 字节。

\n