cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble sending/receiving UART message using HAL libraries. I can receive the correct number of data sent, but it only prints as one character ("-")

MOstl.1
Associate II

I have been trying to figure out this problem all day. I am calling the following function with on a character array. I am trying to use the serial port innate in the nucleo board (usart 2, pins 2/3) to open up a serial monitor. The first times I would run it, I would get a string of characters printed out, all dashes "------------------" the length of my message. I tried messing with my gpio config, and all it managed to do was change the character that was being printed multiple times to an exclamation mark, and then a left bracket.

I am using the arduino serial monitor, but have gotten the same results using minicom.

I am using the same baud rate in both the serial monitor and my code, 115200.

I am developing on a mac pro (Catalina),

void printMsg(char *msg)
{
	for(uint32_t i = 0; i<strlen(msg); i++)
	{
		while((__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) != true)) {}
 
		HAL_UART_Transmit(&huart2, (uint8_t)msg[i], sizeof(msg), HAL_MAX_DELAY);
	}
 
}
 
	sprintf(usr_msg, "This is hello world /n");
	printMsg(usr_msg);

Here are the settings that I am using

void prvSetupHardware(void)
{
 
 
	GPIO_InitTypeDef GPIO_InitStructure;
 
	memset(&GPIO_InitStructure,0,sizeof(GPIO_InitStructure));
	memset(&huart2,0,sizeof(huart2));
 
	GPIO_InitStructure.Pin = GPIO_PIN_2;
	GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStructure.Alternate = GPIO_AF1_USART2;
	GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStructure.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 
	GPIO_InitStructure.Pin = GPIO_PIN_3;
	GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 
	huart2.Instance= USART2;
	huart2.Init.BaudRate  = 115200;
	huart2.Init.WordLength = UART_WORDLENGTH_8B;
	huart2.Init.StopBits = UART_STOPBITS_1;
	huart2.Init.Parity = UART_PARITY_NONE;
	huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart2.Init.Mode = UART_MODE_TX_RX;
	HAL_UART_Init(&huart2);
 
}

1 ACCEPTED SOLUTION

Accepted Solutions

Either transmit the whole message using

HAL_UART_Transmit(&huart2, msg, sizeof(msg), HAL_MAX_DELAY);

or do it as you want to do now, byte by byte, using

HAL_UART_Transmit(&huart2, &msg[i], 1, HAL_MAX_DELAY);

and you don't need to wait for TXE, HAL_UART_Transmit does that for you.

If you want to use Cube, read Cube's manual, or maybe better, read Cube's sources.

JW

View solution in original post

2 REPLIES 2

Either transmit the whole message using

HAL_UART_Transmit(&huart2, msg, sizeof(msg), HAL_MAX_DELAY);

or do it as you want to do now, byte by byte, using

HAL_UART_Transmit(&huart2, &msg[i], 1, HAL_MAX_DELAY);

and you don't need to wait for TXE, HAL_UART_Transmit does that for you.

If you want to use Cube, read Cube's manual, or maybe better, read Cube's sources.

JW

MOstl.1
Associate II

Thank you! this solved it.