cancel
Showing results for 
Search instead for 
Did you mean: 

How to send and recevie data from serial print.

sangarius
Associate II

I'm using servo and driving it with servo motor. I want to print the servo degree via UART to serial. But im getting unknown characters. My code is;

/* USER CODE BEGIN 0 */
uint32_t potadc;
uint32_t servopot;
/* USER CODE END 0 */
 
/* USER CODE BEGIN 1 */
	char str[16];
  /* USER CODE END 1 */
 
 
 HAL_UART_Transmit(&huart2, (uint32_t)*str, sprintf(str, "Servo : %d\n", servopot), 0xFFFF);
 
 	  	  HAL_Delay(1);

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
RomainR.
ST Employee

here my test, it works on my side with trace using TeraTerm.

0693W0000059crVQAQ.png 

main.c

/* USER CODE BEGIN Includes */
#include <stdio.h>
 
/* USER CODE END Includes */

/* USER CODE BEGIN PV */
char str[16] = {0};
uint32_t servopot = 1234;
 
/* USER CODE END PV */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
 
  int size_len = sprintf (str, "Servo : %lu\n", servopot);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  	HAL_UART_Transmit (&huart2, (uint8_t *)str, size_len, HAL_MAX_DELAY);
  	HAL_Delay(1000);
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Can you shareyour code (UART2 setup) and hardware you use ?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

21 REPLIES 21

Levels output by the STM32 are not directly compatible with RS232, you need to use a different adapter.

C​heck that your baud rate and settings are consistent.

Check that HSE_VALUE is consistent with your actual clock source.​

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

(uint8_t *)str

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

I did it but still getting 999999 or ===== in different values. Baud rate and settings are OK.

Is 16 characters enough? Does setting servopot to 1234 give consistent reporting?​

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

.

How many characters should i set?

RomainR.
ST Employee

Hi sangarius,

Set str before calling HAL_UART_Transmit(), and I think sprintf cannot use %d with unsigned long integer.

HAL_UART_Transmit(&huart2, (uint32_t)*str, sprintf(str, "Servo : %d\n", servopot), 0xFFFF);

Try:

uint8_t srt[16] = {0};

uint32_t servopot = 1234;

int size_len = sprintf (str, "Servo : %ld\n", servopot);

HAL_UART_Transmit (&huart2, (uint8_t)*str, size_len, HAL_MAX_DELAY);

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

32 would give you some margin, the %d could output 11 digits and sign, you need space for the NUL and newline characters​.

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

I tried this but still getting words like A A A A, not my servopot value.