2023-07-23 11:00 PM
I want to send an array of integers over UART in one go. I was able to do this using for loop as below.
But I want to do it without for loop as it is taking time to send over UART. How to send the integer data over UART in one go?
This is the data I want to send
Solved! Go to Solution.
2023-07-24 05:16 AM
Hello. This way you can append more values to same string in for loop:
uint16_t len=0,i;
for(i=0;i<8;i++)
{
len+=snprintf(&msg[len],sizeof(msg)-len,"%d\n",adc_buf[i]);
}
HAL_UART_Transmit_DMA(&huart2,msg,len);
(just idea, make sure msg buffer is enough big)
2023-07-23 11:05 PM
Check option with DMA
2023-07-23 11:51 PM
I tried DMA also without for loop but I keep on getting address I guess but I am not sure.
2023-07-24 03:07 AM - edited 2023-07-24 03:11 AM
Hello
You must first build complete string and then send it. Like this:
int len;
len=snprintf(msg,sizeof(msg),"%d\n%d\n%d\n%d\n", adc_buf[0],adc_buf[1],adc_buf[2],adc_buf[3]);
// ^ or use some nice for loop to build your complete string, this is just example !
HAL_UART_Transmit_DMA(&huart2,msg,len);
You cannot call HAL_UART_Transmit_DMA again if previous transmit is not ready, since there is no double buffering on the HAL level.
2023-07-24 04:30 AM
But again this will involve for loop as I have mentioned above and will take time to transmit.
Even if I tried to convert all the data into string; everytime only the first 8 indxes gets refresh; the other data doesn't append. So, I have to include the HAL Transmit function in for loop.
What is other way to transmit the integer without converting it to string and without the for loop?
2023-07-24 05:16 AM
Hello. This way you can append more values to same string in for loop:
uint16_t len=0,i;
for(i=0;i<8;i++)
{
len+=snprintf(&msg[len],sizeof(msg)-len,"%d\n",adc_buf[i]);
}
HAL_UART_Transmit_DMA(&huart2,msg,len);
(just idea, make sure msg buffer is enough big)
2023-07-24 11:13 AM
What is the reason for the "no for loops" restriction? Are you having a performance issue? Do you not have budget for another for loop? Did your instructor tell you that you can't use a for loop?
Just wondering.
2023-07-24 11:34 AM
>>What is other way to transmit the integer without converting it to string and without the for loop?
Send the memory content for the buffer directly? Not going to be in readable ASCII, but array of 16-bit values. See Data Representation 101
uint16_t adc_buf[32]; // or whatever
HAL_UART_Transmit_DMA(&huart2,(void *)adc_buf, sizeof(adc_buf)); // Send whole array as block of data
Probably want to move to another buffer, or ping-pong, so ADC inbound DMA doesn't overwrite data being sent to UART
2023-07-25 02:31 AM
There must be some mistake here, if the data is a lot, or is sent continuously, it would be more recommended to use DMA with buffer supply via interrupt handling. The main code is free for other tasks.
2023-07-25 02:52 AM
Thank you this worked for me