2020-11-04 08:06 AM
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,"");
}
Solved! Go to Solution.
2020-11-05 06:15 AM
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
2020-11-04 05:58 PM
> 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.
2020-11-05 12:55 AM
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;
2020-11-05 06:15 AM
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
2020-11-06 02:58 AM
Thanks a lot TDK! It solved my problem. wish you all the best :)
2023-02-09 02:59 AM
@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.
2024-06-14 03:12 AM
@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?
2024-06-14 03:18 AM
@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.
2024-06-14 05:29 AM
@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.