zoukankan      html  css  js  c++  java
  • 使用QT来制作串口终端

    为什么要使用QT,因为它是跨平台的。

    我现在使用的环境是Win7 64bit,使用VS的编译器来编译QT工程。

    安装这套环境简单说一下:先到QT官网下载qt-windows-opensource-5.1.1-msvc2012_opengl-x86_64-offline,然后安装它

    (BUT,最近我把QtCreator改成使用Mingw编译了,直接下载带有Mingw32的版本就可以了,这样可以编译出同时在64和32下运行的程序!!)

    再自己安装一个VS,我这里安装的是VS2012的

    这样就可以在QT Creator中找到编译器了

    我现在是打算使用QT来做一个串口收发工具,网上找了找相关的资料,发现有两种:qextserialport和QtSerialPort,前者在sourceforge里可以下载得到,后者嘛是QT自带的。我这里选择使用后者,可以参考下面网页上的资料:

    http://qt-project.org/doc/qt-5.1/qtserialport/examples.html(已经失效)

    http://qt-project.org/doc/qt-5/qtserialport-examples.html

    由于我对QT也不太熟悉,所以我先弄一个最简单的控制台程序来开始串口编程。这里的例子就是cenumerator

    新建一个QT控制台程序,很简单,选择了控制台程序之后都下一步就可以了。

    把里面的源码下载下来,覆盖掉main.cpp

     1 /****************************************************************************
     2 **
     3 ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
     4 ** Contact: http://www.qt-project.org/legal
     5 **
     6 ** This file is part of the QtSerialPort module of the Qt Toolkit.
     7 **
     8 ** $QT_BEGIN_LICENSE:LGPL$
     9 ** Commercial License Usage
    10 ** Licensees holding valid commercial Qt licenses may use this file in
    11 ** accordance with the commercial license agreement provided with the
    12 ** Software or, alternatively, in accordance with the terms contained in
    13 ** a written agreement between you and Digia.  For licensing terms and
    14 ** conditions see http://qt.digia.com/licensing.  For further information
    15 ** use the contact form at http://qt.digia.com/contact-us.
    16 **
    17 ** GNU Lesser General Public License Usage
    18 ** Alternatively, this file may be used under the terms of the GNU Lesser
    19 ** General Public License version 2.1 as published by the Free Software
    20 ** Foundation and appearing in the file LICENSE.LGPL included in the
    21 ** packaging of this file.  Please review the following information to
    22 ** ensure the GNU Lesser General Public License version 2.1 requirements
    23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    24 **
    25 ** In addition, as a special exception, Digia gives you certain additional
    26 ** rights.  These rights are described in the Digia Qt LGPL Exception
    27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    28 **
    29 ** GNU General Public License Usage
    30 ** Alternatively, this file may be used under the terms of the GNU
    31 ** General Public License version 3.0 as published by the Free Software
    32 ** Foundation and appearing in the file LICENSE.GPL included in the
    33 ** packaging of this file.  Please review the following information to
    34 ** ensure the GNU General Public License version 3.0 requirements will be
    35 ** met: http://www.gnu.org/copyleft/gpl.html.
    36 **
    37 **
    38 ** $QT_END_LICENSE$
    39 **
    40 ****************************************************************************/
    41 
    42 #include <QTextStream>
    43 #include <QCoreApplication>
    44 #include <QtSerialPort/QSerialPortInfo>
    45 
    46 QT_USE_NAMESPACE
    47 
    48 int main(int argc, char *argv[])
    49 {
    50     QCoreApplication a(argc, argv);
    51     QTextStream out(stdout);
    52     QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();
    53 
    54     out << QObject::tr("Total number of ports available: ") << serialPortInfoList.count() << endl;
    55 
    56     foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) {
    57         out << endl
    58             << QObject::tr("Port: ") << serialPortInfo.portName() << endl
    59             << QObject::tr("Location: ") << serialPortInfo.systemLocation() << endl
    60             << QObject::tr("Description: ") << serialPortInfo.description() << endl
    61             << QObject::tr("Manufacturer: ") << serialPortInfo.manufacturer() << endl
    62             << QObject::tr("Vendor Identifier: ") << (serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : QByteArray()) << endl
    63             << QObject::tr("Product Identifier: ") << (serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : QByteArray()) << endl
    64             << QObject::tr("Busy: ") << (serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl;
    65     }
    66 
    67     return 0;
    68 }

    再把cenumerator.pro文件下载下来覆盖新建工程的pro文件,如果嫌烦那只要在我们的QT += core后面加上serialport即可(与core空格隔开)。

     1 #-------------------------------------------------
     2 #
     3 # Project created by QtCreator 2013-12-12T09:17:54
     4 #
     5 #-------------------------------------------------
     6 
     7 QT       += core serialport
     8 
     9 QT       -= gui
    10 
    11 TARGET = cenumerator
    12 CONFIG   += console
    13 CONFIG   -= app_bundle
    14 
    15 TEMPLATE = app
    16 
    17 SOURCES += main.cpp

    编译运行即可了。编译目录会在工程的上一级菜单中!这样的结构容易会把工程弄乱。

    这会我把构建和运行中的默认构建目录改成当前文件,在删除了所有makefile文件之后,QT Creator可以正确地编译出exe文件来,但会提示“警告:Qmake不支持源文件目录下的构建目录”什么的云云。

     

    虽然我们现在编译是没有问题,但这终究是一个隐患。

    所以,最后我还是决定不去修改默认构建目录了,让它恢复成../build-%{CurrentProject:Name}-%{CurrentKit:FileSystemName}-%{CurrentBuild:Name},这样默认情况下它还是在上级目录中生成中间文件及最终应用程序。但我对工程本身进行一些设置,使得QT会把编译得的中间文件保存到工程目录下面:

    打开项目 -> 构建和运行 -> 概要,不再选中Shadow build,这样工程的编译就会在工程目录下面进行了,也不会再提示“警告:Qmake不支持源文件目录下的构建目录”的错误了。

     喏,看现在所有文件都平躺着了,更清晰一些。

     来运行一看看情况吧,枚举串口:

  • 相关阅读:
    数学+高精度 ZOJ 2313 Chinese Girls' Amusement
    最短路(Bellman_Ford) POJ 1860 Currency Exchange
    贪心 Gym 100502E Opening Ceremony
    概率 Gym 100502D Dice Game
    判断 Gym 100502K Train Passengers
    BFS POJ 3278 Catch That Cow
    DFS POJ 2362 Square
    DFS ZOJ 1002/HDOJ 1045 Fire Net
    组合数学(全排列)+DFS CSU 1563 Lexicography
    stack UVA 442 Matrix Chain Multiplication
  • 原文地址:https://www.cnblogs.com/tfanalysis/p/3470630.html
Copyright © 2011-2022 走看看