Skip to main content
Associate II
June 23, 2025
Question

Receiving speed with UART

  • June 23, 2025
  • 1 reply
  • 385 views

I have to receive a desired speed that is sent from the TX code, and receive it into the RX code of another different STM32 board that is connected to a Maxon 251601 motor, for it to work.

If i write for example MC_ProgramSpeedRampMotor1(1500,2000) directly in the board that it is connected to the motor it works good.

The problem that I have is that I try to send this from the transmiter board:

char tx_buff[32];

int velocidad = 1500;

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

And the receiver has this in the main:

char rx_buff[32];

int duration = 5000;

while (1)

{

HAL_UART_Receive(&huart2, (uint8_t*)rx_buff, 8, HAL_MAX_DELAY);

char *ptr = strstr(rx_buff, "SPD");
if (ptr != NULL)
{
 int velocidad = atoi(ptr + 3);
 MC_StartMotor1();
 MC_ProgramSpeedRampMotor1(velocidad, duration);
}

I was trying for it to receive "SPD1500\n" inside the rx_buff, to make the motor move. The strange thing is that while I was trying to receive it, it actually worked once, but the motor didn't move, so when I tried it again it didn't receive anything, even with the same code. I have heard that it could have been only luck that the transmiter and receiver were sincronized at the same moment, but I could not do it again.

Do you think that I should change something in the code to receive the speed and make the motor move using the MC_ProgramSpeedRampMotor(speed,time), or does this issue come from somewhere else?

1 reply

Pavel A.
Super User
June 23, 2025

Your code is a bit naive. Please search for examples of buffered, asynchronous and receive and transmit. Unlike Arduino or other libraries, the STM32 HAL library does not provide buffering.

 

garivinAuthor
Associate II
June 23, 2025

I have looked for simple examples, but non of them send this type of message. I can not find  a tx, rx about a speed that will be transmited into a motor. I have been able to send buffers but nothing like this.

Andrew Neil
Super User
June 23, 2025

@garivin wrote:

but non of them send this type of message. 


Really?

It's just a simple text string "SPD1500\n"

As far as the UART goes, that's no different to sending "Hello, World\n"

 


@garivin wrote:

I can not find  a tx, rx about a speed that will be transmited into a motor. .


That's entirely irrelevant to the UART.

As far as the UART goes, it's all just a string of characters.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.