Skip to main content
PJone.2
Associate
November 4, 2020
Solved

How to send int values efficiently through CDC USB

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.
TDK
TDKBest answer
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

"If you feel a post has answered your question, please click ""Accept as Solution""."

8 replies

TDK
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.

"If you feel a post has answered your question, please click ""Accept as Solution""."
PJone.2
PJone.2Author
Associate
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;

TDK
TDKBest answer
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

"If you feel a post has answered your question, please click ""Accept as Solution""."
Patukes
Associate III
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.