Qt串口持續輸入數據,上位機接收端未響應問題

同時在論壇版提問:http://topic.csdn.net/u/20120415/18/85ca116a-cb4e-4cc7-9e1f-9bf34531ff01.html

此Qt串口接收程序需要做的工作是:從開發板上不斷地傳輸過來帶有“Star”幀頭的數據幀,數據是浮點型數據。要求顯示的時候也是將浮點數顯示出來。我採用的是Polling查詢機制,不是EventDriven機制,在textBrowser裏面想先顯示出除了”Star”幀的數據,先是用十六進制數表示出來。

現在我遇到的問題是:

當我打開上位機串口,並且開啓開發板的電源,進行串口數據傳輸時候,Qt程序會出現未響應,只能把開發板電源關掉等待一會,textBrowser上面就會出現數據,此時的數據只是能看出第一次發送的時候會把“Star”去掉只顯示出後面的數據,如圖一所示。


需要幫忙解決的問題是:

1.用Qt寫串口程序時候,在利用Polling機制時,面對不斷讀取的串口數據,如何讓textBrowser一直輸出數據而不出現程序未響應的情況?

2.接圖一,除了第一個程序能夠去掉“Star”幀頭外,如何讓以後的幀傳過來時候都可以去掉“Star”幀頭,從而只是顯示後面的數據。

 

另附上我的主要實現代碼:

//**************mainwindow.h*********************//
#ifndefMAINWINDOW_H
#defineMAINWINDOW_H
#include<QMainWindow>
#include<QString>
#include<QStandardItemModel>
#include"win_qextserialport.h"
namespaceUi{
    classMainWindow;
}
classMainWindow:publicQMainWindow
{
    Q_OBJECT
public:
    explicitMainWindow(QWidget*parent=0);
    ~MainWindow();
private:
    Ui::MainWindow*ui;
    Win_QextSerialPort*myCom;
    QTimer*myReadTimer;            //採用polling查詢的方式進行
privateslots:
    voidreadMyCom();
    voidon_openMyComBtn_clicked();
    voidon_closeMyComBtn_clicked();
};
#endif//MAINWINDOW_H
 

 

//***********mainwindow.cpp***************//
#include"mainwindow.h"
#include"ui_mainwindow.h"
#include<QMessageBox>
MainWindow::MainWindow(QWidget*parent):
    QMainWindow(parent),
    ui(newUi::MainWindow)
{
    ui->setupUi(this);
    ui->closeMyComBtn->setEnabled(false);
    setWindowTitle(tr("串口調試主界面_P1"));
 
}
 
MainWindow::~MainWindow()
{
    deleteui;
}
 
 
voidMainWindow::readMyCom()
{
    //個人想法:先讓其在文本框中全部輸出字符串下,串口傳的是標準ASCII值,然後如果直接顯示的時候,又把它轉成字符串形式
    //由於有的ASCII值轉換成字符串後不能在屏幕上直接顯示,所以會出現卡死程序情況,停止傳輸後就只輸出Star字符
    //因此需要先把串口裏面的ASCII值轉換成十六進制數,然後再將其轉換成浮點數,分別顯示在表格裏面  --CommentByDream_Fly
    QStringtemp='\0';
    QStringstrHex;//16進制數據
    QByteArraydataAll=myCom->readAll();
    intrflag=0;
    if(!dataAll.isEmpty())
    {
        QDataStreamout(&dataAll,QIODevice::ReadWrite);
        //下面是判斷幀頭“Star”,不知道如何直接讀入4個字節的字符串Star,然後直接判斷??
        if(!out.atEnd())
        {
            qint8judge1=0;
            out>>judge1;
            if(judge1==83)
            {
                qint8judge2=0;
                out>>judge2;
                if(judge2==116)
                {
                    qint8judge3=0;
                    out>>judge3;
                    if(judge3==97)
                    {
                        qint8judge4=0;
                        out>>judge4;
                        if(judge4==114)
                        rflag=1;
                     }
                }
             }
             else
                rflag=0;
             if(rflag)
             {
                 while(!out.atEnd())
                 {
                     qint8outChar=0;
                     out>>outChar;
                     QStringstr1=QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0'));//轉換成十六進制數_Dream_Fly
                     if(str1.length()>1)
                     {
                        strHex+=str1+"";
                     }
                     else
                     {
                        strHex+="0"+str1+"";
                     }
                 }
             }
        }
        ui->textBrowser->append(strHex.toUpper());
    }
}
 
//打開串口的信號與槽自動關聯函數
voidMainWindow::on_openMyComBtn_clicked()
{
    QStringportName=ui->portNameComboBox->currentText();//獲取串口名
    myCom=newWin_QextSerialPort(portName,QextSerialBase::Polling);
    //定義串口對象,並傳遞參數,在構造函數裏對其進行初始化
    myCom->open(QIODevice::ReadWrite);  //注意:得要先打開串口,然後再設置串口的參數,不然設置無效!!!
    myCom->flush();//存入緩衝區內待讀取
    //設置波特率
    if(ui->baudRateComboBox->currentText()==tr("9600"))   //根據組合框內容對串口進行設置
       myCom->setBaudRate(BAUD9600);
    elseif(ui->baudRateComboBox->currentText()==tr("115200"))
       myCom->setBaudRate(BAUD115200);
    //設置數據位
    if(ui->dataBitsComboBox->currentText()==tr("8"))
        myCom->setDataBits(DATA_8);
    elseif(ui->dataBitsComboBox->currentText()==tr("7"))
       myCom->setDataBits(DATA_7);
    //設置奇偶校驗
    if(ui->parityComboBox->currentText()==tr("無"))
       myCom->setParity(PAR_NONE);
    elseif(ui->parityComboBox->currentText()==tr("奇校驗"))
       myCom->setParity(PAR_ODD);
    elseif(ui->parityComboBox->currentText()==tr("偶校驗"))
       myCom->setParity(PAR_EVEN);
    //設置停止位
    if(ui->stopBitsComboBox->currentText()==tr("1"))
       myCom->setStopBits(STOP_1);
    elseif(ui->stopBitsComboBox->currentText()==tr("2"))
       myCom->setStopBits(STOP_2);
    myCom->setFlowControl(FLOW_OFF);//設置數據流控制,我們使用無數據流的默認設置
    myCom->setTimeout(10);//設置延時      --Modify改小點
    myReadTimer=newQTimer(this);
    myReadTimer->setInterval(200);
    connect(myReadTimer,SIGNAL(timeout()),this,SLOT(readMyCom()));
    this->myReadTimer->start();         //開始poll查詢操作
   //connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));  //不採用事件Event的方式,採用Polling方式
    ui->openMyComBtn->setEnabled(false);
    ui->closeMyComBtn->setEnabled(true);
    ui->helpBtn->setEnabled(true);
    ui->portNameComboBox->setEnabled(false);
    ui->baudRateComboBox->setEnabled(false);
    ui->dataBitsComboBox->setEnabled(false);
    ui->stopBitsComboBox->setEnabled(false);
    ui->parityComboBox->setEnabled(false);
    ui->displayBtn->setEnabled(true);
    ui->debugBtn->setEnabled(true);
}
voidMainWindow::on_closeMyComBtn_clicked()
{
    myCom->close();
    this->myReadTimer->stop();          //關閉poll操作
    ui->openMyComBtn->setEnabled(true);
    ui->helpBtn->setEnabled(true);
    ui->portNameComboBox->setEnabled(true);
    ui->baudRateComboBox->setEnabled(true);
    ui->dataBitsComboBox->setEnabled(true);
    ui->stopBitsComboBox->setEnabled(true);
    ui->parityComboBox->setEnabled(true);
}
voidMainWindow::on_helpBtn_clicked()
{
    QMessageBox::about(this,tr("幫助信息"),tr("1.選定好具體串口設置,點擊打開串口即可收到信息"));
}
 
 

//***************main.cpp************//
#include<QtGui/QApplication>
#include<QTextCodec>       //加入頭文件
#include"mainwindow.h"
intmain(intargc,char*argv[])
{
    QApplicationa(argc,argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());    //使程序可處理中文
    MainWindoww;
    w.show();
    returna.exec();
}
 


Qt串口數據這裏的問題困擾我幾天了,希望能夠幫忙解決下,不勝感激。

原創文章,歡迎轉載,轉載請註明:blog.csdn.net/jjzhoujun2010

作者:Dream Fly




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章