UART STM32 not working correctly when trying a mirror/echo test

130 views Asked by At

I am trying to create a mirror/echo test using powerline and stm32 through UART (nucleo F401RE). With this project I'm relearning C as I haven't coded in a while.

I have to boards, a Master board and a slave one. The master generates a string that is sent through a power line module: ie.. Sending string %d. The slave receives the string, and replies with a string saying... I have received %d value.This with the end of checking tha the value sent is the same on the slave.

UPDATE: Console

Let me explain better, I am sending the following string:

3C980100003DC100000000003200000000000032A3BA0049534901015C543035375C58315C46305C44315C43325C5039365C4D31315C4A3030436F67616E3230303030C4DC363E

In ascii as shown in the Console image it is delimited by the symbols <>. The number after Cogan is incremented and I want to extract that value. I have manage to do this, however, I have found some data loss. I used UART in polling mode, I have had to find that my Receive timeout has to be half the time I am sending the data, i.e. I send every 2 s and my receive timeout is 1 sec. In the example of the image, I send the string with the number 27, my slave receives and replies I received number 27. The next number starts in master, I send number 28, slave receives number 28 and replies I received number 28. However, master does not detect this message but it has been sent because I can see it with a sniffer on my powerline. Hope this diagram helps a bit.

Master (Transmitter receiver) || Transmit -> Receive | Transmit -> Receive | Transmit -> Receive

Slave (Receive transmitter) || Receive -> Transmit | Receive -> Transmit | Receive -> Transmit

I'm trying now with Interrupts, constantly sending message and trying to receive with HAL_UART_Receive_IT, trying using the callback however, first I had the 8 error code (overrun error). Now, I don't see the error while debugging but it seems my callbakc is just working once.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

if(huart->Instance == USART1){ /* Do everything in this block */
    
    HAL_UART_Receive_IT(&huart1, RxData, sizeof(RxData));
    sprintf(msgOut, "\r\n String \r\n  ");
    HAL_UART_Transmit(&huart2, (uint8_t *)msgOut, strlen(msgOut), HAL_MAX_DELAY);
    // Iterate through the array and print each element
    for (int i = 0; i < sizeof(RxData); i++) {
        sprintf(msgOut, "%02X ", RxData[i]);
        HAL_UART_Transmit(&huart2, (uint8_t *)msgOut, strlen(msgOut), HAL_MAX_DELAY);// %02X formats the number as a two-digit hexadecimal
        }
     sprintf(msgStatus, "\r\n  ");
     HAL_UART_Transmit(&huart2, (uint8_t *)msgStatus, strlen(msgStatus), HAL_MAX_DELAY);
     

} else {/* Do nothing */

}

I receive the following and then stops. I don't have the continuous messages stream that is coming:

3C 98 01 10 00 38 1F FF 00 FE 18 F2 00 30 32 A3 BA 00 49 53 49 01 01 5C 54 30 35 37 5C 58 31 5C 46 30 5C 44 31 5C 43 32 5C 50 39 36 5C 4D 31 31 5C 4A 30 30 43 6F 67 61 6E 32 30 30 91 E9 8E 3E 3C 98 01 10 00 38 1F FF 00 FE 18 F2 00 30 32 A3 BA 00 49 53 49 01 01 5C 54 30 35 37 5C 58 31 5C 46 30 5C 44 31 5C 43 32 5C 50 39 36 5C 4D 31 31 5C 4A 30 30 43 6F 67 61 6E 32 30 30 91 E9 8E 3E 3C 98 01 10 00 38 1F FF 00 FE 18 F2 00 30 32 A3 BA 00 49 53 49 01 01 5C 54 30 35 37 5C 58 31 5C 46 30 5C 44 31 5C 43 32 5C 50 39 36 5C 4D 31 31 5C 4A 30 30 43 6F 67 61 6E 32 30 30 91 E9 8E 3E 3C 98 01 10 00 38 1F FF 00 FE 18 F2 00 30 32 A3 BA 00 49 53 49 01 01 5C 54 30 35 37 5C 58 31 5C 46 30 5C 44 31 5C 43 32 5C 50 39 36 5C 4D 31 31 5C 4A 30 30 43 6F 67 61 6E 32 30 30 91 E9 8E 3E

0

There are 0 answers