cancel
Showing results for 
Search instead for 
Did you mean: 

Send an array of integers over UART

AKhot.1
Senior

I want to send an array of integers over UART in one go. I was able to do this using for loop as below.

AKhot1_0-1690178079538.png

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

AKhot1_1-1690178406237.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
JTP1
Lead

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)

View solution in original post

11 REPLIES 11
RhSilicon
Lead

Check option with DMA

AKhot.1
Senior

I tried DMA also without for loop but I keep on getting address I guess but I am not sure.

AKhot1_0-1690181435951.png

AKhot1_1-1690181504099.png

 

 

JTP1
Lead

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.

 

 

 

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?  

JTP1
Lead

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)

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.

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
RhSilicon
Lead

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.

Thank you this worked for me