Qt中的串口编程之一

来源:互联网 发布:狐谓狼曰羊肉其鲜乎 编辑:程序博客网 时间:2024/05/21 07:07

                                                          QtSerialPort

简介

         QtSerialPort模块是Qt5库的附加部分,为硬件和虚拟的串口提供了统一的接口。注意:该模块也增加了对Qt4的支持。
        串口由于其简单和可靠,目前在像嵌入式系统,机器人等工业中依旧用得很多。使用QtSerialPort模块,开发者可以大大缩短开发串口相关的应用程序的周期。使用QtSerialPort模块最初是来源于第三方库QSerialDevice(2.0分支),现在QtSerialPort已经移到了https://codereview.qt-project.org/代码库上。

功能介绍

目前,该模块API只包括两个类:Serial和SerialPortInfo。

【SerialPort】

SerialPort是该模块的基础类,提供了一系列基础的方法和属性来访问串口资源。

SerialPort类对操作系统的支持如下:


对Symbian平台的支持只是部分的,是因为缺乏开发者,因为诺基亚已经决定放弃该平台,Qt5也不会包括对该平台的支持。

【SerialPortInfo】

SerialPortInfo是一个帮助类。它提供了系统上可用的串口的信息。

SerialPortInfo类对操作系统的支持如下:


对Symbian平台的支持只是部分的,是因为缺乏开发者,因为诺基亚已经决定放弃该平台,Qt5也不会包括对该平台的支持。

源代码

目前,QtSerialPort相关的源代码都可以通过浏览器浏览:https://qt.gitorious.org/qt/qtserialport
也可以通过git下载:git clone git://gitorious.org/qt/qtserialport.git

编译和安装

注意:最好选择影子构建,因为这样的话构建目录和源代码目录是分开的,这样就允许你保持源代码目录是干净的。
编译步骤:

1、配置编译环境

*安装Perl
*确保环境变量设置正确:Qt4/Qt5;编译器;Perl
*创建编译目录:跟源代码目录同级
/
|- /serialport-src
|- /serialport-build

2、Perl只是在Qt5的时候才需要,Qt4的情况下可以不配置

3、使用如下推荐步骤在Qt4/Qt5下编译和安装QtSerialPort库

cd serialport-build
qmake ../serialport-src/qtserialport.pro
make [or 'nmake' for MSVC compiler, or 'mingw32-make' for MinGW compiler]
make install [or 'nmake install' for MSVC compiler, or 'mingw32-make install' for MinGW compiler]
注意:在*nix系统上,你可能需要超级用户的权限才能执行安装这一步骤:
sudo make install

使用方法

QtSerialPort提供相应的库文件,我们在编写自己的应用程序的时候可以使用这些库,使用方法如下:
Qt4
CONFIG += serialport
Qt5
QT += serialport
然后在工程中包含相应的头文件就行:
...
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
...
示例代码
[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <QtCore/QCoreApplication> 
  2. #include <QtCore/QDebug> 
  3.   
  4. #include <QtSerialPort/QSerialPort> 
  5. #include <QtSerialPort/QSerialPortInfo> 
  6.   
  7. QT_USE_NAMESPACE 
  8.   
  9. int main(int argc,char *argv[]) 
  10.     QCoreApplication a(argc, argv); 
  11.   
  12.     // Example use QSerialPortInfo 
  13.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { 
  14.         qDebug() << "Name        : " << info.portName(); 
  15.         qDebug() << "Description : " << info.description(); 
  16.         qDebug() << "Manufacturer: " << info.manufacturer(); 
  17.   
  18.         // Example use QSerialPort 
  19.         QSerialPort serial; 
  20.         serial.setPort(info); 
  21.         if (serial.open(QIODevice::ReadWrite)) 
  22.             serial.close(); 
  23.     } 
  24.     
  25.     return a.exec(); 
  26. }</span> 

注意:CONFIG += serialport必须是.pro文件中的第一行或者第二行。

实际操作

1、代码下载:


2、编译:


3、安装:

sudo make install后,我的系统上因为设置了Qt5的环境变量:

头文件安装到了:/opt/Qt5.0.1/5.0.1/gcc/include/QtSerialPort

库文件安装到了:/opt/Qt5.0.1/5.0.1/gcc/lib

4、测试,运行自带的examples:


4、使用QSerialPort库

(1)创建一个Qt Console应用程序

修改.pro文件,添加serialport库,如下:


(2)修改main函数

如下:添加QtSerialPort库的相应头文件,并实现简单的代码:

[cpp] view plaincopyprint?
  1. <span style="font-size: 14px;">#include <QCoreApplication> 
  2. #include <QtCore/QDebug> 
  3. #include "QSerialPort" 
  4. #include "QSerialPortInfo" 
  5.  
  6. int main(int argc,char *argv[]) 
  7.     QCoreApplication a(argc, argv); 
  8.      
  9.     // Example use QSerialPortInfo 
  10.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { 
  11.         qDebug() << "Name        : " << info.portName(); 
  12.         qDebug() << "Description : " << info.description(); 
  13.         qDebug() << "Manufacturer: " << info.manufacturer(); 
  14.  
  15.         // Example use QSerialPort 
  16.         QSerialPort serial; 
  17.         serial.setPort(info); 
  18.         if (serial.open(QIODevice::ReadWrite)) 
  19.             serial.close(); 
  20.     } 
  21.  
  22.     return a.exec(); 
  23. }</span> 

(3)编译后运行如下:




原创粉丝点击