Qt学习之路——— 串口通信总结

来源:互联网 发布:多米音乐mac版下载 编辑:程序博客网 时间:2024/05/16 07:52

注意: Qt5发布之前,Qt实现串口通信一般是采用第三方类库qextserialport。Qt5发布后自带了QtSerialPort 能够支持串口通信。

1、Qextserialport类介绍

     在Qt5之前的版本中并没有特定的串口控制类,现在大部分人使用的是第三方写的qextserialport类,本文章主要是讲解怎样利用此类实现串口通信。

2、文件下载地址:

     http://sourceforge.net/projects/qextserialport/files/

3、文件内容:

    3.1.下载到的文件为qextserialport-1.2win-alpha ,解压并打开后其内容如下。

   

(1)doc文件夹中的文件内容是QextSerialPort类和QextBaseType的简单的说明,我们可以使用记事本程序将它们打开。

(2)examples文件夹中是几个例子程序,可以看一下它的源码,不过想运行它们好像会出很多问题啊。

(3)html文件夹中是QextSerialPort类的使用文档。

(4)然后就是剩下的几个文件了。其中qextserialenumerator.cpp及qextserialenumerator.h文件中定义的QextSerialEnumerator类是用来获取平台上可用的串口信息的。不过,这个类好像并不怎么好用,而且它不是我们关注的重点,所以下面就不再介绍它了。

 (5)qextserialbase.cpp和qextserialbase.h文件定义了一个QextSerialBase类,win_qextserialport.cpp和win_qextserialport.h文件定义了一个Win_QextSerialPort类,posix_qextserialport.cpp和posix_qextserialport.h文件定义了一个Posix_QextSerialPort类,qextserialport.cpp和qextserialport.h文件定义了一个QextSerialPort类。这个QextSerialPort类就是我们上面所说的那个,它是所有这些类的子类,是最高的抽象,它屏蔽了平台特征,使得在任何平台上都可以使用它。

                                                              

在Windows下是:

qextserialbase.cpp和qextserialbase.h 以及win_qextserialport.cpp和win_qextserialport.h

在Linux下是:

qextserialbase.cpp和qextserialbase.h 以及posix_qextserialport.cpp和posix_qextserialport.h

而在Windows下我们可以使用事件驱动EventDriven方式,也可以使用查询Polling方式,但是在Linux下我们只能使用查询Polling方式。

4、串口通信的实现

   4.1  声明串口对象 :

Win_QextSerialPort  *myCom;      //Windows系统内Posix_QextSerialPort *myCom;      //Linux系统内

  4.2 串口定义:

//Windows中有两种查询模式,一种polling模式,一种EventDriven模式myCom = new Win_QextSerialPort("COM1",QextSerialBase::Polling);                 //myCom = new Win_QextSerialPort("COM1",QextSerialBase::EventDriven);//Linux中只有Polling模式myCom = new Posix_QextSerialPort("/dev/ttyS0",QextSerialBase::Polling);

    事件驱动方式EventDriven就是使用事件处理串口的读取,一旦有数据到来,就会发出readyRead()信号,我们可以关联该信号来读取串口的数据。在事件驱动的方式下,串口的读写是异步的,调用读写函数会立即返回,它们不会冻结调用线程。

    查询方式Polling则不同,读写函数是同步执行的,信号不能工作在这种模式下,而且有些功能也无法实现。但是这种模式下的开销较小。我们需要自己建立定时器来读取串口的数据。

    在Windows下支持以上两种模式,而在Linux下只支持Polling模式。

  4.3 串口打开模式

myCom ->open(QIODevice::ReadWrite);    //打开模式
QIODevice::Unbuffered0x0020描述QIODevice::NotOpen0x0000 QIODevice::ReadOnly0x0001 QIODevice::WriteOnly0x0002 QIODevice::ReadWriteReadOnly | WriteOnly QIODevice::Append0x0004 QIODevice::Truncate0x0008 QIODevice::Text0x0010 

 

4.4 串口的配置函数

复制代码
myCom->setBaudRate(BAUD9600);          //波特率设置,我们设置为9600myCom->setDataBits(DATA_8);            //数据位设置,我们设置为8位数据位myCom->setParity(PAR_NONE);           //奇偶校验设置,我们设置为无校验myCom->setStopBits(STOP_1);            //停止位设置,我们设置为1位停止位myCom->setFlowControl(FLOW_OFF);      //控制流myCom->setTimeout(long);              //设置时间间隔
复制代码

    setTimeout(long)参数决定了Polling查询模式的读取串口的速度。

4.5 串口工作

connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));    //EventDriven模式下才能触发readyRead()信号connect(readTimer,SIGNAL(timeout()),this,SLOT(readMyCom()));  //Polling模式定时器触发timeout()信

4.6 串口读取数据

QByteArray temp = myCom->readAll();    //返回读取的字节int byteLen = myCom->bytesAvailable(); //返回串口缓冲区字节数

4.7 串口写数据

myCom -> Write(const char * data, qint64 maxSize );          //myCom -> Write(const char * data );                          //myCom -> Write(const QByteArray & byteArray);                //int byteLen = myCom->bytesToWrite();                        //输出写数据的字节数//bytesWritten()信号函数来获取已经发送的数据的大小。

5、Qt5以后版本QSerialPort介绍

5.1  .pro文件内需要加上如下代码

QT += serialport               //加在第一行或者第二行

5.2 网上一段利用QSerialPort写的串口通信代码(转自:http://www.doc88.com/p-645858545768.html)

复制代码
#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);    read_port = new mythread(this);    connect(this->read_port, SIGNAL(read_port_data()), this,  SLOT(display_data()));}Dialog::~Dialog(){    //delete this->my_serialport;    delete ui;}void Dialog::on_pushButton_clicked()                   //open serial,打开串口{    this->read_port->start();}void Dialog::display_data()                            //显示数据{    ui->textBrowser->setText(this->read_port->requestData);}
0 0
原创粉丝点击