2025-06-23 6:52 AM - last edited on 2025-06-23 6:54 AM by mƎALLEm
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?
2025-06-23 7:10 AM
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.
2025-06-23 7:46 AM
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.
2025-06-23 8:04 AM
@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.