cancel
Showing results for 
Search instead for 
Did you mean: 

Send 16bits buffer via USB Virtual COM Port.

fposser
Associate III

Hi, I have a buffer with 65000 values in 16 bits from the ADC read that I need to provide to my computer running a python code.

Any suggestion how can I handle this in both sides?

I'm trying to use this function but I'm getting weird data when I'm listening the COM port with Putty.

 

// Generalized function to transmit any 16bits buffer over USB
void send_16b_buffer_over_usb(uint16_t *buffer, uint32_t buffer_size)
{
    uint32_t total_bytes = buffer_size * BYTES_PER_SAMPLE;  // Total size of the buffer in bytes
    uint32_t bytes_sent = 0;
    uint8_t result = USBD_OK;

    // Send the buffer in chunks
    while (bytes_sent < total_bytes)
    {
        // Calculate the remaining bytes to be sent
        uint32_t bytes_to_send = (total_bytes - bytes_sent > CHUNK_SIZE) ? CHUNK_SIZE : (total_bytes - bytes_sent);

        // Send the current chunk of data
        result = CDC_Transmit_HS((uint8_t*)&buffer[bytes_sent / BYTES_PER_SAMPLE], bytes_to_send);

        // Wait for the USB transmission to complete
        while (result == USBD_BUSY)
        {
            result = CDC_Transmit_HS((uint8_t*)&buffer[bytes_sent / BYTES_PER_SAMPLE], bytes_to_send);
        }

        // If the transmission was successful, update the sent bytes counter
        if (result == USBD_OK)
        {
            bytes_sent += bytes_to_send;
        }
        else
        {
            // Handle transmission failure (optional)
            break;
        }
    }
}

 

If I do something simple like this it work fine:

 

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t *data = "Hello World from USB CDC\n";
/* USER CODE END 0 */

int main(void)
{
  HAL_Init();
  
  SystemClock_Config();

  MX_GPIO_Init();
  MX_USB_DEVICE_Init();

  while (1)
  {
	  CDC_Transmit_FS(data, strlen(data));
	  HAL_Delay (1000);
  }
}

 

Thank you.

10 REPLIES 10

Yes, this is the PC code.

STM32 was fine.

Im having other troubles now with the ADC but it's not related with this.

https://community.st.com/t5/stm32-mcus-boards-and-hardware/ch2-on-adc1-does-not-work-while-ch15-or-ch2-ch15-works-fine/td-p/733584