cancel
Showing results for 
Search instead for 
Did you mean: 

Passing multidimensional arrays over HAL UART

kcire L.
Associate III

Hello,

I have a 246 x 3 matrix that I have filled with counter values in the below for loops. I for some reason cannot get the HAL_UART to output individual columns of this matrix.

 for(i=0; i<246; i++)

 {

   for(j=0;j<3;j++)

   {

   Audio_Data[i][j] = counter++;

   }

 }

 HAL_UART_Transmit(&huart1, (uint8_t *)&Audio_Data[0][0], 246,1);

Here I would like to output the entire first column, which should be values 0 through 245.

If I want to trasmit Audio_Data[0][1] (entire column) wouldn't that be 246 through 491 etc...

What am I doing wrong here?

Thanks for any help

4 REPLIES 4
char_array
Associate III

This has nothing to do with the HAL UART driver but how you want to serialize your data. The uart driver just sends a piece of memory you pass it byte by byte.

C is row major order. So an entire row is in continuous memory

https://en.wikipedia.org/wiki/Row-_and_column-major_order

If you want to send a column you either need to send it element by element in a loop or you need to copy it element by element to a buffer and then send the entire buffer.

If it is possible to swap the rows and columns in your data you can just send an entire row.

If the datatype is larger than 1 byte you need to consider the endianness on both sides.

kcire L.
Associate III

what about if I wanted to copy a single column into a separate single dimension array using memcpy?

TDK
Guru

> Here I would like to output the entire first column, which should be values 0 through 245.

> If I want to trasmit Audio_Data[0][1] (entire column) wouldn't that be 246 through 491 etc...

No, based on the way you populate data, the data is not stored in the array in this manner. This is easy to verify in a debugger.

0 1 2
3 4 5
6 7 8
...
 

So first column is 0, 3, 6, ....

But as noted above, columns are not in contiguous memory so you will need to rearrange them before sending.

If you feel a post has answered your question, please click "Accept as Solution".

Your transmit has a timeout of 1 millisecond. Can you get your 246 bytes out in 1 millisecond? That would be upwards of 2.5 megabaud.