Is there a possibility to create a two way communication with uarts using HAL_UART_RxCpltCallback?

262 views Asked by At

I am currently working on a project where i want to communicate with ESP8266 module using usart-1 and have another usart-2 which could be used in putty. My question is if there is any possibility to achieve this goal with HAL_UART_RxCpltCallback function? So far my all attempts to do so failed miserably and the best i could is creating communication where data can be send to esp but no answer can be captured. All my other attempts ended in creating a loop between usarts. Thank you in advance

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {

    uint8_t Data[50]; 
    uint16_t size = 0; 

    size = sprintf(Data, "%c", Received);

    if(Received != '\r' && Received != '\n')
    {
        RingBuffer_PutChar(&USART_RingBuffer_Tx, Received);

        HAL_UART_Transmit_IT(&huart1, Data, size); 
        HAL_UART_Receive_IT(&huart1, &Received, 1); 
    }
    else
    {
        if(Received == '\n')
        {
            //size = sprintf(Data, "\n\r", 2);
            HAL_UART_Transmit_IT(&huart1,(uint8_t*) "\n\r", 2); 
            HAL_UART_Receive_IT(&huart1, &Received, 1); 

        }

        if(Received == '\r')
        {
            if(!RingBuffer_IsEmpty(&USART_RingBuffer_Tx))
            {
                RingBuffer_PutChar(&USART_RingBuffer_Tx, '\r');
                RingBuffer_PutChar(&USART_RingBuffer_Tx, '\n');

                HAL_UART_Transmit_IT(&huart6, (uint8_t*) RingBufferData_Tx, sizeof(RingBufferData_Tx));
                HAL_UART_Transmit_IT(&huart1, "\nSent\n\r", 12);
                HAL_UART_Receive_IT(&huart6, &Received1, 1);
                HAL_UART_Receive_IT(&huart1, &Received, 1);
                RingBuffer_Clear(&USART_RingBuffer_Tx);
            }
            else
            {
                HAL_UART_Transmit_IT(&huart1,(uint8_t*) "Buffer is empty\n\r", 17); 
                HAL_UART_Receive_IT(&huart1, &Received, 1); 
            }
        }

    }
}
1

There are 1 answers

0
Andrej On

The HAL_UART_RxCpltCallback function has a parameter UART_HandleTypeDef *huart, so you can check which UART instance called this Callback. You might check it whether it is usart-1 or usart-2 and execute the code according to this.