一、串口类简介

当前的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());}
}

第二步:配置串口

<span style="font-size:12px;">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);}
}</span>

第三步:发送函数

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 串口类QSerialPort 学习笔记相关推荐

  1. Qt 串口类QSerialPort 使用笔记

    Qt 串口类QSerialPort 使用笔记 虽然现在大多数的家用PC机上已经不提供RS232接口了.但是由于RS232串口操作简单.通讯可靠,在工业领域中仍然有大量的应用.Qt以前的版本中,没有提供 ...

  2. Qt 5.9.5学习笔记第三节课

    Qt 5.9.5学习笔记第三节课 学习目标 1.Qt资源图标添加和使用 1.1添加资源文件 1.2qmake让资源文件生效 1.3修改widget应用程序窗口ICO 1.3.1第一种方法 1.3.2 ...

  3. TS基础2(类)-学习笔记

    文章目录 TS基础2(类)-学习笔记 class类 类的继承 修饰符 类的类型.实现接口 TS基础2(类)-学习笔记 class类 //类 class//首字母大写//类(Class):定义了一件事物 ...

  4. 串口通信协议简介—学习笔记

    串口通信协议简介-学习笔记 文章目录 串口通信协议简介-学习笔记 一.串口.COM口.UART口, TTL.RS-232.RS-485区别详解 1.物理接口形式 2.电平标准 2.1 **TTL** ...

  5. 【Qt5.8】Qt5.8中串口类QSerialPort

    00. 目录 00. 目录 01. 串口通信基础 02. QtSerialPort模块简介 03. QSerialPort简介 04. QSerialPort类成员函数 05. 参考示例(简单的串口示 ...

  6. python 面向对象(类)--学习笔记

    面向对象是一种编程方式, 主要集中在类和对象的两个概念 python 中的类符合封装, 继承, 多态的特征 类 是一个模板, 是n多函数的集成 对象 是类的实例化 类的成员分为三大类:字段.方法.属性 ...

  7. qt5 -- qt中关于大小的类的学习笔记

    QSize 这个精度是整型的 QSize定义了一个二维大小,包含宽度(width)和高度(height): 显然,一个QSize有三个函数:宽width(),高height(),比例scale() 可 ...

  8. python 编程_类的学习笔记(from刘金玉b站教程)

    一.类的定义 在面向对象里面,对象和类是不同的,对象是特定类的一个实例,比如如果车是一个类的话,某个人的一辆奔驰车就是一个对象,它是车这个类的实例. 类是抽象的,而对象是具体的.方法是定义在对象上的操 ...

  9. String类的学习笔记(中):介绍字符串的不可变性和字符串常量池

    本文介绍了String类字符串的不可变性和字符串常量池,主要包括 如何保证字符串不可变, 如何对字符串的修改. 为什么字符串要设置不可变, 字符串常量池的创建和了解,简单的字符串常量池图, 以及如何将 ...

最新文章

  1. DevOps的工程化
  2. unity3d版本控制的设置方法(SVN)
  3. count_sort计数排序OpenMP的并行化
  4. elasticsearch 根据条件去除重复值_Excel工作表中的条件格式,不只是查找重复值,还有7种典型用法...
  5. c语言中listempty函数,list_empty()和list_empty_careful()
  6. Intro.js轻松搞定页面引导流程
  7. 人类一败涂地电脑版_iOS账号分享 |人类一败涂地 我们继续相爱相杀,PC大火游戏移植!...
  8. oracle两个date相减_从 Oracle 到 PostgreSQL:从 Uptime 到数据库实例运行时间
  9. java取消科学计数法_Jmeter、Java当double显示的数字过长时取消科学计数法显示
  10. 竖版1:2500万标准中国地图
  11. 量化交易入门--写第一个量化程序
  12. 你应该学点哲学的20个理由:不为拥有深奥的思想,只为更好地生活
  13. Spring cloud alibaba--Feign微服务调用组件
  14. FPGA数字信号处理(八)Quartus FFT IP核实现
  15. “华为区块链白皮书”重磅发布(附下载链接)
  16. 浙大版《C语言程序设计(第3版)》题目集
  17. 数据库密码过期和取消期限限制
  18. Hive入门 ------ Hive是什么,产生背景
  19. 对面向对象程序设计(OOP)的认识
  20. 罗默模型——对电子商务发展的理论分析

热门文章

  1. 【经验】lingo / lindo报错:invalid syntax
  2. 虚拟化平台cloudstack(7)——新版本的调试
  3. FireMonkey 源码学习(5)
  4. 接收list参数_Python 犄角旮旯--List
  5. kindle5 去广告
  6. mysql dml回滚_mysql binlog回滚/闪回,前滚, 分析各表DML情况, 找出长事务与大事务...
  7. cssd oracle,Oracle RAC /etc/init.d/init.cssd startcheck
  8. 传感器 倾斜角 android,android – 如何使用sensor / s获得手机的角度/度数?
  9. SQL2005 游标学习
  10. 论文笔记--跨媒体语义共享子空间学习理论与方法研究-2015