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




  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
实现串口接收数据上位机,可以借助Qt5自带的串口通信库进行操作。下面是一个简单的Qt5串口接收数据上位机的示例: 1. 创建一个新的Qt Widgets应用程序项目,添加一个QPushButton和一个QTextEdit控件。 2. 在按钮的槽函数中添加如下代码,用于打开串口并接收数据: ```c++ void MainWindow::on_btnOpen_clicked() { // 打开串口 QString portName = ui->cmbPortName->currentText(); int baudRate = ui->cmbBaudRate->currentText().toInt(); QSerialPort serialPort; serialPort.setPortName(portName); serialPort.setBaudRate(baudRate); if (serialPort.open(QIODevice::ReadWrite)) { ui->textEdit->append("串口打开成功!"); connect(&serialPort, &QSerialPort::readyRead, this, &MainWindow::on_serialPort_readyRead); } else { ui->textEdit->append("串口打开失败!"); } } void MainWindow::on_serialPort_readyRead() { // 接收串口数据 QSerialPort *serialPort = qobject_cast<QSerialPort *>(sender()); QByteArray data = serialPort->readAll(); ui->textEdit->append(QString(data)); } ``` 3. 在MainWindow类的构造函数中,初始化串口参数并添加可用的串口列表: ```c++ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 初始化串口参数 ui->cmbBaudRate->addItems(QStringList() << "9600" << "115200"); foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { ui->cmbPortName->addItem(info.portName()); } } ``` 4. 编译运行程序,选择正确的串口参数并点击按钮打开串口,即可开始接收数据。 以上是一个简单的Qt5串口接收数据上位机的示例,您可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JaydenZhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值