cancel
Showing results for 
Search instead for 
Did you mean: 

How to send int values efficiently through CDC USB

PJone.2
Associate II

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,"");

}

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

8 REPLIES 8
TDK
Guru

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

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>> 😎 & 0xff;

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

Thanks a lot TDK! It solved my problem. wish you all the best 🙂

Patukes
Associate III

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

@TDK  The data will be transferred as actual Binary data in uint8_t data type with 8 different bytes? or the data will be transferred as a string of the binary data? 


@abhishek_chari wrote:

@TDK  The data will be transferred as actual Binary data in uint8_t data type  


Yes.

 


@abhishek_chari wrote:

with 8 different bytes?  


Not sure what you mean by that?

 


@abhishek_chari wrote:

or the data will be transferred as a string of the binary data? 


No.

@Andrew Neil thank you for the answer. Sorry I did not mention that I am trying to send the double data type value which is generally 64 bits long, that was the reason I said 8 bytes.