How to send int values efficiently through CDC USB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2020-11-04 8: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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2020-11-05 6: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2020-11-04 5: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2020-11-05 6: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2020-11-06 2:58 AM
Thanks a lot TDK! It solved my problem. wish you all the best :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2023-02-09 2: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-14 3: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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-14 3: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.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-06-14 5: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.
