zoukankan      html  css  js  c++  java
  • Qt中的串口编程之一

    1.                                                                 QtSerialPort
      1. 简介
      2. 功能介绍
        1. SerialPort
        2. SerialPortInfo
      3. 源代码
      4. 编译和安装
        1. 配置编译环境
        2. Perl只是在Qt5的时候才需要Qt4的情况下可以不配置
        3. 使用如下推荐步骤在Qt4Qt5下编译和安装QtSerialPort库
      5. 使用方法
      6. 实际操作
        1. 代码下载
        2. 编译
        3. 安装
        4. 测试运行自带的examples
        5. 使用QSerialPort库
          1. 1创建一个Qt Console应用程序
          2. 2修改main函数
          3. 3编译后运行如下

      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>
    ...
    示例代码

    #include <QtCore/QCoreApplication>
    #include <QtCore/QDebug>
     
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
     
    QT_USE_NAMESPACE
     
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
     
        // Example use QSerialPortInfo
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            qDebug() << "Name        : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
     
            // Example use QSerialPort
            QSerialPort serial;
            serial.setPort(info);
            if (serial.open(QIODevice::ReadWrite))
                serial.close();
        }
       
        return a.exec();
    }

    注意: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:

    5、使用QSerialPort库

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

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

    (2)修改main函数

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

    #include <QtCore/QDebug>
    #include "QSerialPort"
    #include "QSerialPortInfo"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        
        // Example use QSerialPortInfo
        foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            qDebug() << "Name        : " << info.portName();
            qDebug() << "Description : " << info.description();
            qDebug() << "Manufacturer: " << info.manufacturer();
    
            // Example use QSerialPort
            QSerialPort serial;
            serial.setPort(info);
            if (serial.open(QIODevice::ReadWrite))
                serial.close();
        }
    
        return a.exec();
    }

    (3)编译后运行如下:

     转自:http://blog.csdn.net/chenlong12580/article/details/8976176#t18

  • 相关阅读:
    1028. Hanoi Tower Sequence
    sicily 1063. Who's the Boss
    ubuntu 12.04 x86_64:java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons
    ubuntu12.04 desktop默认无ssh支持
    取消putty右键粘贴功能
    gcc编译参数之m32 m64
    Invalid command 'RailsBaseURI'
    Ubuntu 12.4 server 安装 redmine
    查看Samba用户的方法
    【转】windows浏览共享切换用户登录的方法
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/5787798.html
Copyright © 2011-2022 走看看