QT的串口类QSerialPort

一、串口类简介

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

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

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

使用方法

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

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

    QT += serialport


第一步:获取串口号

[cpp]  view plain  copy
  1. void SOCom::getserialportnames()  
  2. {  
  3.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
  4.     {  
  5.         //qDebug() << "Name : " << info.portName();  
  6.         //qDebug() << "Description : " << info.description();  
  7.         //qDebug() << "Manufacturer: " << info.manufacturer();  
  8.         //qDebug() << "Serial Number: " << info.serialNumber();  
  9.         //qDebug() << "System Location: " << info.systemLocation();  
  10.   
  11.         ui->saerialport->addItem(info.portName());  
  12.     }  
  13. }  

第二步:配置串口

[cpp]  view plain  copy
  1. <span style="font-size:12px;">void MainWindow::set_serial()  
  2. {  
  3.     //设置串口号  
  4.     QString comname=ui->comboBox_host->currentText();  
  5.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())  
  6.     {  
  7.         if(info.portName()==comname)  
  8.         {  
  9.             my_serialport->setPortName(info.systemLocation());  
  10.         }  
  11.     }  
  12.   
  13.     //设置波特率  
  14.     qint32 baudrate_index = ui->comboBox_fre->currentIndex();  
  15.     switch (baudrate_index) {  
  16.     case 0:  
  17.         my_serialport->setBaudRate(QSerialPort::Baud1200,QSerialPort::AllDirections);  
  18.         break;  
  19.     case 1:  
  20.         my_serialport->setBaudRate(QSerialPort::Baud2400,QSerialPort::AllDirections);  
  21.         break;  
  22.     case 2:  
  23.         my_serialport->setBaudRate(QSerialPort::Baud4800,QSerialPort::AllDirections);  
  24.         break;  
  25.     case 3:  
  26.         my_serialport->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);  
  27.         break;  
  28.     case 4:  
  29.         my_serialport->setBaudRate(QSerialPort::Baud19200,QSerialPort::AllDirections);  
  30.         break;  
  31.     case 5:  
  32.         my_serialport->setBaudRate(QSerialPort::Baud38400,QSerialPort::AllDirections);  
  33.         break;  
  34.     case 6:  
  35.         my_serialport->setBaudRate(QSerialPort::Baud57600,QSerialPort::AllDirections);  
  36.         break;  
  37.     case 7:  
  38.         my_serialport->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);  
  39.         break;  
  40.     default:  
  41.         my_serialport->setBaudRate(QSerialPort::UnknownBaud,QSerialPort::AllDirections);  
  42.         break;  
  43.     }  
  44.   
  45.         //设置数据位  
  46.         qint32 databits_index=ui->comboBox_data->currentIndex();  
  47.         switch (databits_index) {  
  48.         case 0:  
  49.             my_serialport->setDataBits(QSerialPort::Data5);  
  50.             break;  
  51.         case 1:  
  52.             my_serialport->setDataBits(QSerialPort::Data6);  
  53.             break;  
  54.         case 2:  
  55.             my_serialport->setDataBits(QSerialPort::Data7);  
  56.             break;  
  57.         case 3:  
  58.             my_serialport->setDataBits(QSerialPort::Data8);  
  59.             break;  
  60.         default:  
  61.             my_serialport->setDataBits(QSerialPort::UnknownDataBits);  
  62.             break;  
  63.         }  
  64.   
  65.         //设置校验位  
  66.         qint32 parity_index=ui->comboBox_check->currentIndex();  
  67.         switch (parity_index) {  
  68.         case 0:  
  69.             my_serialport->setParity(QSerialPort::NoParity);  
  70.             break;  
  71.         case 1:  
  72.             my_serialport->setParity(QSerialPort::OddParity);  
  73.             break;  
  74.         case 2:  
  75.             my_serialport->setParity(QSerialPort::EvenParity);  
  76.             break;  
  77.         default:  
  78.             my_serialport->setParity(QSerialPort::UnknownParity);  
  79.             break;  
  80.         }  
  81.   
  82.         //设置停止位  
  83.         qint32 stopbit_index=ui->comboBox_stop->currentIndex();  
  84.         switch (stopbit_index) {  
  85.         case 0:  
  86.             my_serialport->setStopBits(QSerialPort::OneStop);  
  87.             break;  
  88.         case 1:  
  89.             my_serialport->setStopBits(QSerialPort::TwoStop);  
  90.             break;  
  91.         default:  
  92.             my_serialport->setStopBits(QSerialPort::UnknownStopBits);  
  93.             break;  
  94.         }  
  95.   
  96.   
  97.         my_serialport->setFlowControl(QSerialPort::NoFlowControl);  
  98.   
  99.         if(ui->pushButton_open_serial->text()=="打开串口")  
  100.         {  
  101.                 bool com=my_serialport->open(QIODevice::ReadWrite);//打开串口并选择读写模式  
  102.                 if(com)  
  103.                 {  
  104.                     timer->start(100);  
  105.                     ui->statusBar->showMessage("串口打开成功",3000);  
  106.                     isopen=true;  
  107.                     ui->pushButton_open_serial->setText("关闭串口");  
  108.                 }  
  109.   
  110.                 else  
  111.                 {  
  112.                     //qDebug()<<"串口打开失败";  
  113.                     //this->setStatusTip("串口打开失败");  
  114.                     ui->statusBar->showMessage("串口打开失败",3000);  
  115.                     isopen=false;  
  116.                 }  
  117.   
  118.   
  119.         }  
  120.         else  
  121.         {  
  122.            if(my_serialport->isOpen())  
  123.             {  
  124.                timer->stop();  
  125.                 my_serialport->close();  
  126.             }  
  127.             ui->pushButton_open_serial->setText("打开串口");  
  128.             ui->statusBar->showMessage("serial port is closed",3000);  
  129.         }  
  130. }</span>  

第三步:发送函数
[cpp]  view plain  copy
  1. void MainWindow::send_data()  
  2. {  
  3.     if(isopen)  
  4.     {  
  5.         if(!ui->text_seddata->toPlainText().isEmpty())  
  6.         {  
  7.             isHexS=ui->isHexS->isChecked();  
  8.             QString sendstr=ui->text_seddata->toPlainText();  
  9.             QByteArray sdata;  
  10.             if(isHexS)  
  11.             {  
  12.                 sdata.append(sendstr).toHex();  
  13.                 
  14.             }  
  15.            else  
  16.             {  
  17.                 sdata.append(sendstr);  
  18.                 
  19.             }  
  20.             my_serialport->write(sdata,sdata.length());  
  21.             ui->statusBar->showMessage("send success",3000);  
  22.   
  23.         }  
  24.         else  
  25.         {  
  26.             ui->statusBar->showMessage("请输入要发送的数据",3000);  
  27.         }  
  28.     }  
  29.     else  
  30.     {  
  31.         ui->statusBar->showMessage("请先打开串口",3000);  
  32.     }  
  33. }  

第四步:数据显示函数

[cpp]  view plain  copy
  1. void MainWindow::showData()  
  2. {  
  3.     QByteArray showdata=my_serialport->readAll();  
  4.     QString show="";  
  5.     isHexR=ui->isHexR->isChecked();  
  6.     if(isHexR)  
  7.     {  
  8.         for (int i = 0; i < showdata.length(); i++)  
  9.         {  
  10.         qint8 outChar = showdata[i];  
  11.         QString str = QString("%1").arg(outChar&0xFF, 2, 16, QLatin1Char('0'))+" ";  
  12.         show+= str.toUpper();  
  13.         }  
  14.   
  15.     }else  
  16.     {  
  17.         show+=QString(showdata);  
  18.     }  
  19.   
  20.     ui->textBrowser->setText(ui->textBrowser->toPlainText()+show);  
  21.   
  22. }  

第五步:结果测试


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 中的串口通信主要是通过 QSerialPort 实现的。该提供了一组方法和信号,用于打开、关闭、读取和写入串口数据。 以下是使用 QSerialPort 实现串口通信的基本步骤: 1. 创建 QSerialPort 对象,并设置串口参数(如波特率、数据位、停止位、奇偶校验位等)。 ``` QSerialPort serialPort; serialPort.setPortName("COM1"); // 设置串口号 serialPort.setBaudRate(QSerialPort::Baud9600); // 设置波特率 serialPort.setDataBits(QSerialPort::Data8); // 设置数据位 serialPort.setParity(QSerialPort::NoParity); // 设置校验位 serialPort.setStopBits(QSerialPort::OneStop); // 设置停止位 serialPort.setFlowControl(QSerialPort::NoFlowControl); // 设置流控制 ``` 2. 打开串口。 ``` if (serialPort.open(QIODevice::ReadWrite)) { // 串口打开成功,可以进行数据读写操作 } else { // 串口打开失败 } ``` 3. 读取串口数据。 ``` QByteArray data = serialPort.readAll(); // 读取所有数据 ``` 4. 写入串口数据。 ``` QByteArray data = "Hello, World!"; serialPort.write(data); // 写入数据 ``` 5. 关闭串口。 ``` serialPort.close(); ``` 在使用 QSerialPort 时需要注意以下几点: - 在 Windows 平台下,串口号通常是 "COM1"、"COM2" 等形式的字符串,而在 Linux 平台下,串口号通常是 "/dev/ttyS0"、"/dev/ttyS1" 等形式的字符串。 - 在读取串口数据时,可以使用 QSerialPort 的 readyRead() 信号和 waitForReadyRead() 方法。readyRead() 信号在有数据可读时发出,而 waitForReadyRead() 方法则会阻塞当前线程,直到有数据可读或者超时。 - 在写入串口数据时,可以使用 QSerialPort 的 bytesWritten() 信号和 waitForBytesWritten() 方法。bytesWritten() 信号在数据被发送出去时发出,而 waitForBytesWritten() 方法则会阻塞当前线程,直到所有数据都被发送出去或者超时。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值