QT5.9串口接受的内容不对



  • 使用STM32定时向Windows的PC机通过串口传输数据,while(1)的循环内容如下

    while(1)
    {
     printf("J%.8f",J);
     HAL_Delay(15);
     printf("W%.8f",W);
     HAL_Delay(15);
     printf("G%.3f",G);
     HAL_Delay(15);
     printf("V%.3f",V);
     HAL_Delay(15);
     printf("R%.3f\r\n",R);
    			
     HAL_Delay(40);
     printf("A101N303\r\n");
     HAL_Delay(100);
    			
     J += 0.00000001;
     W += 0.00000002;
     G += 0.001;
    }
    

    上位机Qt使用多线程接受数据 采用readall函数 偶尔有接受的内容不对的情况。代码如下

    void MSerialThread::run()
    {
        QByteArray receiveData = "";
        QString readData = "";
        //串口如果打开就永远进行这个线程
        while(serialPort.isOpen())
        {
            //读取串口缓冲区的数据
            receiveData = serialPort.readAll();
    
            //添加倒string类型的末尾
            readData.append(receiveData);
    
            //清除临时数据
            receiveData.clear();
    
            //如果收到 0x0a 0x0d 就说明到底了
            if(readData.endsWith("\r\n"))
            {
                //发送数据
                emit readEnd(readData);
    
                qDebug() << readData;
    
                //清除接收到的数据
                readData.clear();
            }
        }
    }
    
    

    偶尔会收到错误的数据 例如"J1.123W1.1233V1.123G1.123\r\n"(假设正确的是”J1.123W1.123G1.123V1.123R1.123\r\n“)


  • Banned

    This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.