HAL_UART_Transmit_IT发送数据两次

时间:2019-07-09 15:30:04

标签: c stm32 uart stm32f4discovery hal

我正在尝试使用arduino nano作为中间人在PC和STM32f407-DISC1板之间建立UART通信。 PC将“ r”发送到arduino以指示请求。 然后,该请求将通过GPIO中断与stm32进行通信,然后应使用HAL_UART_Transmit_IT传输480字节的数据。 但是,它只发送了一个请求就发送了两次数据。

STM32上的代码是由STM32CubeMX生成的

arduino提出的数据请求

void loop() {
    digitalWrite(4, 0); // Clear EXTI11 line.

    if (mySerial.available() && received < 480) {   // STM32 sending data and is not done.
        buff[received] = mySerial.read();           // Append received data to the buffer.
        received++;
    }
    if (received >= 480) {                          // If the buffer is full
        received = 0;                               // transmit it to PC.
        Serial.println(buff);
    }


    if (Serial.available()) {
        if (Serial.read() == 'r') {     // PC requests data from the STM32
            digitalWrite(4, 1);         // Triggers STM32 EXTI11 line.
            while (Serial.available())  // Empty the buffer.
                Serial.read();
        }
    }
}

STM32上的数据传输

void EXTI15_10_IRQHandler(void)
{
    // Make sure that the interrupt is the good one.
    if (HAL_GPIO_ReadPin(data_req_IRQ_GPIO_Port, data_req_IRQ_Pin)) {
        if (is_sending_data == FALSE)   // If no transmission is happening
            should_send_data = TRUE;    // raise transmission flag.
    }

    // IRQ handling stuff...
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef * huart) {
    is_sending_data = FALSE;    // Transmition is completed, unblock requests.
}

void main(void){
    // Init and other stuff...

    while (1) {
        if (should_send_data == TRUE) { // If data was requested
            HAL_GPIO_WritePin(LD5_GPIO_Port, LD5_Pin, GPIO_PIN_RESET);
            HAL_UART_Transmit_IT(&huart3, matrice, 480);    // Start transmission by interrupt.
            is_sending_data = TRUE;                         // Block requests.
            should_send_data = FALSE;                       // Clear flag.
        }
    // matrice acquisition stuff here
    }
}

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了一个解决方案,但这只是重新考虑我的方法,对于那些希望找到该问题的答案的人感到抱歉。

我用USB转RS232转换器替换了arduino中间人,并通过中断使UART接收工作。 STM检测到“ r”字符,该字符触发数据通信。 这是中断部分:

void USART3_IRQHandler(void)
{
    if (USART3->SR & UART_IT_RXNE) {    // If a byte is received
        rxBuff[0] = (uint8_t) (huart3.Instance->DR & (uint8_t) 0xFF);   // Read it.
        __HAL_UART_FLUSH_DRREGISTER(&huart3);   // Clear the buffer to avoid errors.
        rx_new_char_flag = TRUE;    // Raise the new_char flag.
        return; // Stops the IRQHandler from disabling interrupts.
    }
}

及其主要内容的构想

while (1) {
    if (rx_new_char_flag == TRUE) {
        rx_new_char_flag = FALSE;
        if (rxBuff[0] == 'r') {
            rxBuff[0] = 0;
            HAL_UART_Transmit_IT(&huart3, matrice, 480); // Start transmission by interrupt.
            }
        }

在PC端,为了优化性能,我只等待一个字符,而不是等待完整的480个字节,如果收到一个字符,我将继续读取串行端口,如下面的代码所示

int i = 0;
do {
    ReadFile(m_hSerial, &temp_rx[i], 1, &dwBytesRead, NULL);
    i++;
} while (dwBytesRead > 0 && i < 480);
for (int j = i; j < 480; j++)  // If the transmission is incomplete, fill the buffer with 0s to avoid garbage data.
    temp_rx[j] = 0;

if(i>=480)  // If all the bytes has been received, copy the data in the working buffer.
    std::copy(std::begin(temp_rx), std::end(temp_rx), std::begin(m_touch_state));

这在相当不错的性能下效果很好,因此这可能是解决我的问题的永久方法。