Skip to main content
sangarius
Associate II
October 28, 2020
Solved

How to send and recevie data from serial print.

  • October 28, 2020
  • 21 replies
  • 3302 views

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.

This topic has been closed for replies.
Best answer by RomainR.

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 ?

21 replies

Tesla DeLorean
Guru
October 28, 2020

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
October 28, 2020

(uint8_t *)str

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sangarius
sangariusAuthor
Associate II
October 28, 2020

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

Tesla DeLorean
Guru
October 28, 2020

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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sangarius
sangariusAuthor
Associate II
October 28, 2020

.

RomainR.
ST Employee
October 28, 2020

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.
sangarius
sangariusAuthor
Associate II
October 28, 2020

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

RomainR.
ST Employee
October 28, 2020

oups, sorry.

Not %ld but %lu is the correct format.

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

If still not work, check baudrate, data bit lenght and uart parity settings.

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.
sangarius
sangariusAuthor
Associate II
October 28, 2020

It's still same. I just checked the settings.

RomainR.
ST Employee
October 28, 2020

Can you tell wich compiler / IDE tools 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.
sangarius
sangariusAuthor
Associate II
October 28, 2020

I'm using STM32CubeIDE.

RomainR.
RomainR.Best answer
ST Employee
October 28, 2020

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.
sangarius
sangariusAuthor
Associate II
October 28, 2020

I'm controlling the servo with potentiometer. "potadc" means the value came from ADC of the potentiometer. "servopot" means potadc/6, the rating of the servo 0-170. I want to see the value of the servopot live when i change the potentiometer.

Tesla DeLorean
Guru
October 28, 2020

Yeah, I think we all understood that, but there's a problem with the UART configuration and your hardware, and your posts lack any salient details to be able to diagnose.

Diagram what you have wired to where. And specify the STM32 part/board in question, several thousand different things out there, no idea what you're working with.

Stick a scope on the signal and figure out why you've got the levels or baud rate wrong,

Will save everyone wasting hours on this..

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sangarius
sangariusAuthor
Associate II
October 29, 2020

I write this code inside the loop. It worked. Thanks.

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