通过UART从PC发送和接收数据到STM32问题

时间:2019-07-01 12:07:13

标签: c qt uart

我尝试使用UART连接从PC向STM32发送一个(多个数据行)十六进制数据。

单词看起来像:

FF18E2054605A11661EE
FF4353454605A11661EE
FF18E2564645A11661EE
FF18E2054667876861EE
...

(0xFF和0xEE是开始和结束字节)

如果我通过CuteCom控制台发送数据,它的效果很好(我收到了数据并将其保存到二维数组中)

如果我使用的QT程序(应该达到相同的目的)不起作用,请帮助查找错误!

(PC端)QT:

static uint16_t m1 = 0;
static uint16_t m2 = 0;
static uint16_t m3 = 0;
static uint16_t m4 = 0;

static QByteArray hextest;

if(lines.at(i).contains(",")){
    while(lines.at(i+j) != "\n"){
        QStringList speed_chunks = lines.at(i+j).split(",");
        uart = speed_chunks.at(2)+speed_chunks.at(3)+speed_chunks.at(4)+speed_chunks.at(5) +"\0";

        m1 = speed_chunks.at(2).toInt();
        m2 = speed_chunks.at(3).toInt();
        m3 = speed_chunks.at(4).toInt();
        m4 = speed_chunks.at(5).toInt();

        hextest.resize(8);

        hextest[0]=255;
        hextest[1]=m1>>8;
        hextest[2]=m1;
        hextest[3]=m2>>8;
        hextest[4]=m2;
        hextest[5]=m3>>8;
        hextest[6]=m3;
        hextest[6]=m4>>8;
        hextest[8]=m4;
        hextest[9]=238;

        qDebug()<< uart;
        qDebug()<< hextest.toHex();

        m_serial.write(hextest.toHex());
        m_serial.waitForBytesWritten(1000);
        QThread::usleep(2000000);

        j++;
    }
    i += j;
}

(STM32侧)C代码:

void USART2_IRQHandler() {

    if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET) {          //Checks whether the specified USART interrupt has occurred
        USART_ClearITPendingBit(USART2, USART_IT_RXNE);             //Clears the USART2 interrupt pending bits
        static int string_cnt = 0;                                  //Initialisation of string counter
        char uart_input = USART2->DR;                               //Initialisation a char variable with USART Data register

        if (uart_input == 0xff) {                                   //If a string = 0xff > first Letter
            string_cnt = 0;                                         //String_cnt reset
        }

        if (string_cnt < 10) {                              //If string_cnt < 10 letters ->
            received_str[string_cnt++] = uart_input;                //receive next letter
        }
        if (string_cnt >= 10 && received_str[0] == 0xff && received_str[9] == 0xee) {   //If string_cnt >= 10 units and first unit = 0xff & last = 0xee ->
            receive_done_flag = TRUE;                               //receive_done_flag = true
        }
    }
}

while(h<5000){

if (receive_done_flag == TRUE){
    testarrays[0][h]=((uint16_t)received_str[1] << 8) | received_str[2];
    testarrays[1][h]=((uint16_t)received_str[3] << 8) | received_str[4];
    testarrays[2][h]=((uint16_t)received_str[5] << 8) | received_str[6];
    testarrays[3][h]=((uint16_t)received_str[7] << 8) | received_str[8];

    h++;

    receive_done_flag=FALSE;
}

因此,testarrays[0][x]对于不同的x十六进制数据将变为相同的值(received_str不获取新数据)

谢谢!

P.S。使用chrome时如何格式化? Chrome +快捷键=(

0 个答案:

没有答案