Skip to main content
sangarius
Associate II
Oct 28, 2020 10:21 AM UTC
Solved

How to send and recevie data from serial print.

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.
RomainR.
RomainR.Best answer
ST Employee
Oct 28, 2020 2:21 PM UTC

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.

21 replies

Tesla DeLorean
Guru
Oct 28, 2020 10:49 AM UTC

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
Oct 28, 2020 10:53 AM UTC

(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
Oct 28, 2020 11:01 AM UTC

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

Tesla DeLorean
Guru
Oct 28, 2020 11:06 AM UTC

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
Oct 28, 2020 11:23 AM UTC

.

RomainR.
ST Employee
Oct 28, 2020 11:48 AM UTC

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
Oct 28, 2020 12:20 PM UTC

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

RomainR.
ST Employee
Oct 28, 2020 12:33 PM UTC

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
Oct 28, 2020 1:40 PM UTC

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

RomainR.
ST Employee
Oct 28, 2020 12:33 PM UTC

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
Oct 28, 2020 1:38 PM UTC

I'm using STM32CubeIDE.

RomainR.
RomainR.Best answer
ST Employee
Oct 28, 2020 2:21 PM UTC

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
Oct 28, 2020 6:38 PM UTC

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
Oct 28, 2020 9:08 PM UTC

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
Oct 29, 2020 11:16 AM UTC

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

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