cancel
Showing results for 
Search instead for 
Did you mean: 

How to display data in the console from the array? Console returns strange signs

AWe.1
Associate II

Hello 🙂

I was trying to send some data to the console using USART1 (STM32F7508-DK).

With the following simple data there was no problem:

uint8_t data[] = "HELLO! \r\n";

HAL_UART_Transmit(&huart1, data, sizeof(data), 1000);

But when I try to display values using that code:

uint8_t pData = 0x80;
uint16_t tempData[64];
uint8_t convtempData[128];
 
while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  HAL_StatusTypeDef ret = HAL_ERROR;
	  uint8_t ACCEL_ADDR;
	  ACCEL_ADDR = 0xD0;
	
          ret = HAL_I2C_IsDeviceReady(&hi2c1, ACCEL_ADDR, 1, 100);
 
	  ret = HAL_I2C_Master_Transmit(&hi2c1, ACCEL_ADDR, &pData, 2, 50);
 
	  ret = HAL_I2C_Master_Receive(&hi2c1, ACCEL_ADDR | 0x01, convtempData, 128, 150);
 
	  print_to_console(convtempData);
  }

And print _to_console() function look in that way:

void print_to_console(uint8_t* convtempData)
{
	uint8_t space[] = " ";
	for(int i = 0; i < sizeof(convtempData); i++)
	{
		HAL_UART_Transmit(&huart1, convtempData[i], sizeof(convtempData[i]), 1000);
		HAL_UART_Transmit(&huart1, space, sizeof(space), 1000);
	}
}

From console:

0693W00000HqiTsQAJ.png 

Also, it looks like program exits print _to_console() after 4th iteration (i=3) and goes to the int main () to while(1).

Does anyone knows how to solve it?

USART1 configuration (same configuration in the console):

0693W00000HqiUbQAJ.png

5 REPLIES 5

Data Representation 101

The data in the array is in binary form used by the computer, if you want human readable ASCII decimal characters use sprintf(str,"%d". ..) or itoa()

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

In addition, use strlen instead of sizeof to avoid sending a null character (or more), at the end of each transmission.

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

@Community member​ @TDK​ Thank you for your answers 🙂 Indeed, I want to have human readable elements in the console.

Could you tell me how the correct syntax looks like?

I tried those 2 options:

void print_to_console(uint8_t* convtempData)
{
	uint8_t space[] = " ";
	for(int i = 0; i < 128; i++)
	{
		HAL_UART_Transmit(&huart1, itoa(convtempData[i]), strlen(convtempData[i]), 1000);
		HAL_UART_Transmit(&huart1, space, sizeof(space), 1000);
	}
}

And:

void print_to_console(uint8_t* convtempData)
{
	char buffer[3] ;
	for(int i = 0; i < 128; i++)
	{
		sprintf(buffer , "%u ",convtempData[i]);
		printf("%s", buffer);
	}
}

And unfortunately it didn't work :(

It looks like you need to spend some time studying the standard C string handling functions - this is standard C stuff, nothing specifically to do with STM32.

It is far easier to learn basic C skills on a PC or similar "conventional" computer - away from all the added complexities of embedded microcontrollers.

Here are some C learning & reference materials - including a free online textbook:

https://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

HAL_UART_Transmit( &huart1, itoa(convtempData[i]), strlen(convtempData[i]), 1000 );

Check the definition of itoa: eg, https://www.cplusplus.com/reference/cstdlib/itoa/ - you have to tell it where to store the string.

It's the length of that string that you need to tell HAL_UART_Transmit

strlen(convtempData[i]) won't work because convtempData[i] is not a string.

void print_to_console(uint8_t* convtempData)
{
	char buffer[16] ;
	for(int i = 0; i < 128; i++)
	{
              HAL_UART_Transmit(&huart1, buffer, sprintf(buffer, "%u ",convtempData[i]), 1000);
	}
}

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