Qt 串口类QSerialPort 学习笔记

一、串口类简介

当前的QtSerialPort模块中提供了两个C++类,分别是QSerialPort QSerialPortInfo

  QSerialPort 类提供了操作串口的各种接口。

  QSerialPortInfo 是一个辅助类,可以提供计算机中可用串口的各种信息。

使用方法

    先介绍 QSerialPortInfo 的使用。下面是一个简单的例子,用来列举出电脑上全部的串口设备。

    首先,需要在pro文件中增加如下内容:

    QT += serialport


第一步:获取串口号

void SOCom::getserialportnames()
{
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        //qDebug() << "Name : " << info.portName();
        //qDebug() << "Description : " << info.description();
        //qDebug() << "Manufacturer: " << info.manufacturer();
        //qDebug() << "Serial Number: " << info.serialNumber();
        //qDebug() << "System Location: " << info.systemLocation();

        ui->saerialport->addItem(info.portName());
    }
}

第二步:配置串口

void MainWindow::set_serial()
{
    //设置串口号
    QString comname=ui->comboBox_host->currentText();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        if(info.portName()==comname)
        {
            my_serialport->setPortName(info.systemLocation());
        }
    }

    //设置波特率
    qint32 baudrate_index = ui->comboBox_fre->currentIndex();
    switch (baudrate_index) {
    case 0:
        my_serialport->setBaudRate(QSerialPort::Baud1200,QSerialPort::AllDirections);
        break;
    case 1:
        my_serialport->setBaudRate(QSerialPort::Baud2400,QSerialPort::AllDirections);
        break;
    case 2:
        my_serialport->setBaudRate(QSerialPort::Baud4800,QSerialPort::AllDirections);
        break;
    case 3:
        my_serialport->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
        break;
    case 4:
        my_serialport->setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections);
        break;
    case 5:
        my_serialport->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);
        break;
    case 6:
        my_serialport->setBaudRate(QSerialPort::Baud57600,QSerialPort::AllDirections);
        break;
    case 7:
        my_serialport->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
        break;
    default:
        my_serialport->setBaudRate(QSerialPort::UnknownBaud,QSerialPort::AllDirections);
        break;
    }

        //设置数据位
        qint32 databits_index=ui->comboBox_data->currentIndex();
        switch (databits_index) {
        case 0:
            my_serialport->setDataBits(QSerialPort::Data5);
            break;
        case 1:
            my_serialport->setDataBits(QSerialPort::Data6);
            break;
        case 2:
            my_serialport->setDataBits(QSerialPort::Data7);
            break;
        case 3:
            my_serialport->setDataBits(QSerialPort::Data8);
            break;
        default:
            my_serialport->setDataBits(QSerialPort::UnknownDataBits);
            break;
        }

        //设置校验位
        qint32 parity_index=ui->comboBox_check->currentIndex();
        switch (parity_index) {
        case 0:
            my_serialport->setParity(QSerialPort::NoParity);
            break;
        case 1:
            my_serialport->setParity(QSerialPort::OddParity);
            break;
        case 2:
            my_serialport->setParity(QSerialPort::EvenParity);
            break;
        default:
            my_serialport->setParity(QSerialPort::UnknownParity);
            break;
        }

        //设置停止位
        qint32 stopbit_index=ui->comboBox_stop->currentIndex();
        switch (stopbit_index) {
        case 0:
            my_serialport->setStopBits(QSerialPort::OneStop);
            break;
        case 1:
            my_serialport->setStopBits(QSerialPort::TwoStop);
            break;
        default:
            my_serialport->setStopBits(QSerialPort::UnknownStopBits);
            break;
        }


        my_serialport->setFlowControl(QSerialPort::NoFlowControl);

        if(ui->pushButton_open_serial->text()=="打开串口")
        {
                bool com=my_serialport->open(QIODevice::ReadWrite);//打开串口并选择读写模式
                if(com)
                {
                    timer->start(100);
                    ui->statusBar->showMessage("串口打开成功",3000);
                    isopen=true;
                    ui->pushButton_open_serial->setText("关闭串口");
                }

                else
                {
                    //qDebug()<<"串口打开失败";
                    //this->setStatusTip("串口打开失败");
                    ui->statusBar->showMessage("串口打开失败",3000);
                    isopen=false;
                }


        }
        else
        {
           if(my_serialport->isOpen())
            {
               timer->stop();
                my_serialport->close();
            }
            ui->pushButton_open_serial->setText("打开串口");
            ui->statusBar->showMessage("serial port is closed",3000);
        }
}

第三步:发送函数
void MainWindow::send_data()
{
    if(isopen)
    {
        if(!ui->text_seddata->toPlainText().isEmpty())
        {
            isHexS=ui->isHexS->isChecked();
            QString sendstr=ui->text_seddata->toPlainText();
            QByteArray sdata;
            if(isHexS)
            {
                sdata.append(sendstr).toHex();
              
            }
           else
            {
                sdata.append(sendstr);
              
            }
            my_serialport->write(sdata,sdata.length());
            ui->statusBar->showMessage("send success",3000);

        }
        else
        {
            ui->statusBar->showMessage("请输入要发送的数据",3000);
        }
    }
    else
    {
        ui->statusBar->showMessage("请先打开串口",3000);
    }
}

第四步:数据显示函数

void MainWindow::showData()
{
    QByteArray showdata=my_serialport->readAll();
    QString show="";
    isHexR=ui->isHexR->isChecked();
    if(isHexR)
    {
        for (int i = 0; i < showdata.length(); i++)
        {
        qint8 outChar = showdata[i];
        QString str = QString("%1").arg(outChar&0xFF, 2, 16, QLatin1Char('0'))+" ";
        show+= str.toUpper();
        }

    }else
    {
        show+=QString(showdata);
    }

    ui->textBrowser->setText(ui->textBrowser->toPlainText()+show);

}

第五步:结果测试




你可能感兴趣的:(Qt学习笔记)