无法通过UART传输所有字符 [英] Cannot transmit every characters through UART

查看:237
本文介绍了无法通过UART传输所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用stm32f0 MCU.

I am using stm32f0 MCU.

我想从uart中传输从uart接收到的每个字节.我在从uart收到的每个字节上启用了一个中断.

I would like to transmit every single byte received from the uart out of the uart. I am enabling an interrupt on every byte received from uart.

我的代码很简单.

uint8_t Rx_data[5]; 

//Interrupt callback routine
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart->Instance == USART1)  //current UART
    {
        HAL_UART_Transmit(&huart1, &Rx_data[0], 1, 100);        
        HAL_UART_Receive_IT(&huart1, Rx_data, 1);   //activate UART receive interrupt every time on receiving 1 byte
    }
}

我的PC将ASCII 12345678传输到stm32.如果一切正常,则PC应该收回12345678.但是,PC会收到1357.代码有什么问题?

My PC transmits ASCII 12345678 to stm32. If things work as expected, the PC should be receiving 12345678 back. However, the PC receives 1357 instead. What is wrong with the code?

推荐答案

重新启用中断可能效率不高.通过进行一些修改,可以使中断保持活动状态,而无需再次编写处理程序.请参见下面的示例,它是从stm32cubemx生成器更改而来的.

Reenabling interrupts may be inefficient. With a couple of modifications it is possible to keep the interrupt active without needing to write the handler all over again. See the example below altered from the stm32cubemx generator.

/**
* @brief This function handles USART3 to USART6 global interrupts.
*/
void USART3_6_IRQHandler(void)
{
  InterruptGPS(&huart5);
}

void InterruptGPS(UART_HandleTypeDef *huart) {
    uint8_t rbyte;
    if (huart->Instance != USART5) {
        return;
    }
    /* UART in mode Receiver ---------------------------------------------------*/
    if((__HAL_UART_GET_IT(huart, UART_IT_RXNE) == RESET) || (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) == RESET)) {
        return;
    }
    rbyte = (uint8_t)(huart->Instance->RDR & (uint8_t)0xff);
    __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);

    // do your stuff

}

static void init_gps() {
    __HAL_UART_ENABLE_IT(&huart5, UART_IT_RXNE);
}

这篇关于无法通过UART传输所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆