Skip to main content
Visitor II
November 4, 2020
Solved

How to send int values efficiently through CDC USB

  • November 4, 2020
  • 2 replies
  • 4133 views

Hello ST community, I am trying to send values from ADC through USB using "CDC_Transmit_FS()"

The code works well but my goal is to transmit ADC values faster!

(I have no option to use High Speed USB transmission mode.)

The ADC values in the adcbuff are 4 digits integer and "\n" is appended to distinguish each value at the receiving side.

e.g) txbuff[] = {'1', '1', '1', '1','\n','2', '2', '2', '2','\n' ... '9', '9', '9', '9','\n'};

Is there any better or direct method to send ADC values through USB instead of converting int values to string?

Thanks in advance!

uint32_t adcbuff[sample];

char txbuff[sample*5];

char tempbuff[10];

while(1)

{

  HAL_ADC_Start_DMA(&hadc2,(uint32_t*)adcbuff, sample);

  for(i = 0; i < sample; i++)

  {

   sprintf (tempbuff, "%d\n", ((adcbuff[i] * 5000) / 0xFFFF)-2000); 

   strcat( txbuff,tempbuff);

  }

     

  CDC_Transmit_FS( (uint8_t*)txbuff, strlen(txbuff));  

  strcpy(txtbuff,"");

}

    This topic has been closed for replies.
    Best answer by TDK

    Create a uint8_t * pointer from wherever you're storing the values. No need to change them in memory, although you'll need to decipher it on the other side correctly.

    (uint8_t *) adcbuff

    2 replies

    Super User
    November 5, 2020

    > Is there any better or direct method to send ADC values through USB instead of converting int values to string?

    Of course. Send them as raw uint16_t ADC values, or if less than 16 bits per reading, pack them into the smallest possible space. No need to convert them to strings first.

    PJone.2Author
    Visitor II
    November 5, 2020

    Thanks for your answer! How do I send raw uint16_t values? CDC_Transmit_FS takes uint8_t* as argument. Do you mean by putting uint16_t value into two uint8_t values like code below?

    uint8_t txbuff[];

    uint16_t adcval;

    txbuff[0] = (adcval >> 0) & 0xff;

    txbuff[1] = (adcval>> 8) & 0xff;

    TDKAnswer
    Super User
    November 5, 2020

    Create a uint8_t * pointer from wherever you're storing the values. No need to change them in memory, although you'll need to decipher it on the other side correctly.

    (uint8_t *) adcbuff

    Explorer
    February 9, 2023

    @TDK​ 

    Referring to your answer, would't the data truncate to an unit8_t data at the other end. If not, is there any clue on how to recover it properly. Thank you.