Skip to main content
Associate II
June 18, 2025
Solved

UART Commands to control B-G431B-ESC1

  • June 18, 2025
  • 3 replies
  • 1590 views

I am currently working or a project with microcontrollers STM32 and this is my current situation:

- I want to send from my NUCLEO f4476ZE the desired speed via UART to my other controller which is B-G431B-ESC1 discovery kit with STM32G431CB MCU.

- I have this discovery kit connected to the motor (I can make it run with the CubeIDE project generated from the Motor Control Workbench that i have created with this Discovery kit and a Maxon 251601 motor).

 

The problem that I have is that i don't know which structure of message should I send from the Nucleo to the ESC1 for the ESC1 to read and interpret it correctly to move the motor at the desired speed of the message.

I was trying sending this as desired speed:

char tx_buff[32];
int velocidad = 1500;

while (1)
{
 sprintf(tx_buff, "SPD%d\n", velocidad);
 HAL_UART_Transmit(&huart2, (uint8_t*)tx_buff, strlen(tx_buff), 1000);
 HAL_Delay(1000);
}

And for the receiver I wanted to add this: 

HAL_UART_Receive_IT(&huart2, &rx_buf[rx_index], 1); //after MX_USART2_UART_Init();
#include <string.h>
#include <stdlib.h>

void process_command(char *cmd)
{
 if (strncmp(cmd, "SPD", 3) == 0)
 {
 int rpm = atoi(&cmd[3]);
 MC_ProgramSpeedRampMotor1(rpm, 1000); // Cambia velocidad con rampa de 1s
 }
 else if (strcmp(cmd, "START") == 0)
 {
 MC_StartMotor1();
 }
 else if (strcmp(cmd, "STOP") == 0)
 {
 MC_StopMotor1();
 }
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 if (huart->Instance == USART2)
 {
 if (rx_buf[rx_index] == '\n')
 {
 rx_buf[rx_index] = '\0';
 process_command((char*)rx_buf);
 rx_index = 0;
 }
 else
 {
 if (rx_index < RX_BUF_LEN - 1)
 rx_index++;
 else
 rx_index = 0;
 }

 HAL_UART_Receive_IT(&huart2, &rx_buf[rx_index], 1);
 }
}




The problem is that the motor is not moving at the desired speed that i tell him, but with the osciloscope i can see that there is a signal in the tx,rx,ground that i have connected correctly between the two boards, as i did the getting started tutorial from uart and the receiver received the messages from the trasmiter.
I think that the problem that i have is that it can not read correctly the velocity. Can somebody help me to send the desired speed to the motor for it to run at that speed?
Even if the code does not have anything to do with mine, I may have written it incorrectly.

 

Best answer by Karl Yamashita

Look at my signature. It has a link to Git project for setting up UART in DMA mode with idle detection to accept any length string. 

This will queue your START, STOP and SPD as individual messages correctly.

 

 

3 replies

Karl Yamashita
Principal
June 18, 2025

Use the debugger to view the rpm value before you pass the argument to MC_ProgramSpeedRampMotor1.

 

 

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
garivinAuthor
Associate II
June 19, 2025

This way should I view the rpm value?

void process_command(char *cmd)
{
if (strncmp(cmd, "SPD", 3) == 0)
{
int rpm = atoi(&cmd[3]); // Set breakpoint here

// Debug line (optional if you use printf)
printf("Parsed RPM: %d\n", rpm); // Requires a working printf() setup

// Or use debugger: watch the value of rpm here

if (MC_GetMotorState(0) == RUN)
{
MC_ProgramSpeedRampMotor1(rpm, 1000);
}
}
else if (strcmp(cmd, "START") == 0)
{
MC_StartMotor1();
}
else if (strcmp(cmd, "STOP") == 0)
{
MC_StopMotor1();
}
}

 

And the code of the transmiter looks good to you? The way of sendingthe speed I mean

cedric H
Technical Moderator
June 19, 2025

Hello @garivin,

What is your version of the MCSDK used to generate ESC code?

If I understood correctly, you want to implement your own communication mechanism through the UART right?

 

Karl Yamashita
Principal
June 19, 2025

Instead of worrying about the communication part, first test that by calling MC_ProgramSpeedRampMotor1 works.

Before your main while loop, just call MC_ProgramSpeedRampMotor1 with some test arguments. 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
garivinAuthor
Associate II
June 19, 2025

Ok, I will try that as fast as I can take the university computer and test it.

I will keep you updated, thank you.

Karl Yamashita
Karl YamashitaBest answer
Principal
June 23, 2025

Look at my signature. It has a link to Git project for setting up UART in DMA mode with idle detection to accept any length string. 

This will queue your START, STOP and SPD as individual messages correctly.

 

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source