Qt Serial Port: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Improve some docs about Abdroid support)
 
(61 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{LangSwitch}}
__TOC__


'''NOTE: As of Qt 5.1, Qt Serial Port is now officially part of Qt. You no longer need to build Qt Serial Port manually; simply install Qt 5.1 or later and go to section [[Qt Serial Port#Usage|Usage]]'''
== Brief description ==
The [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] module is an add-on module for the Qt5 library, providing a single interface for both hardware and virtual serial ports.
'''''Note:''' Also added support for Qt4.''
Serial interfaces, due to their simplicity and reliability, are still popular in some industries like the development of embedded systems, robotics, etc.
Using the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] module, developers can significantly reduce the time needed to implement Qt applications that require access to a serial interface.
== Functionality ==
Currently, the module API contains two classes: [http://doc.qt.io/qt-5/qserialport.html QSerialPort] and [http://doc.qt.io/qt-5/qserialportinfo.html QSerialPortInfo].
=== QSerialPort ===
[http://doc.qt.io/qt-5/qserialport.html QSerialPort] is the base class of the module and provides a set of basic methods and properties to access resources on serial ports.
Supports the following operating systems:
{| class="wikitable"
!Operating system
!Support state
!Note
|-
|Windows XP/Vista/7/8/10
|YES
|Full support
|-
|Windows CE
|NO (since 5.7)
|Tested only on 5 and 6 platforms in the emulator
|-
|Gnu/Linux
|YES
|Full support
|-
|MacOSX
|YES
|Full support
|-
|Others Unix
|YES
|All POSIX-compatible
|}
Non-official partial Android Support (on own risk, only for rooted devices, in which is it possible to get/set a permissions to the /dev/ttyXYZ nodes) [http://www.qtcentre.org/threads/57928-Serial-Port-in-Android 1] [https://forum.qt.io/topic/34000/does-qt-support-serialport-in-android-system/19 2].
=== QSerialPortInfo ===
[http://doc.qt.io/qt-5/qserialportinfo.html QSerialPortInfo] is a helper class. It provides information on the available serial ports on the system.
Supports the following operating systems:
{| class="wikitable"
!Operating system
!Support state
!|Note
|-
|Windows XP/Vista/7/8/10
|YES
|Full support (using SetupAPI)
|-
|Windows CE
|NO (since 5.7)
|Tested only on 5 and 6 platforms in the emulator
|-
|Gnu/Linux
|YES
|Full support (using libudev, sysfs or simple search in /dev)
|-
|MacOSX
|YES
|Full support
|-
|Others Unix
|YES
|All POSIX-compatible (only simple search in /dev)
|}
== Getting the source code ==
You should clone from the [http://code.qt.io/ official mirror] and track changes from there in order to keep the load on Gerrit down.
<code lang="bash">
$ git clone git://code.qt.io/qt/qtserialport.git
$ cd qtserialport
</code>
This repository contains both versions for Qt4 and Qt5.
To take the version for Qt4 it is necessary to do<ref>Now QtSerialPort has a separate branch for Qt4 (similar to Qt4 itself), which will be only in one instance.</ref>:
<code lang="bash">
$ git checkout qt4-dev
</code>
To take the version for Qt5 it is necessary to do:
<code lang="bash">
$ git checkout qt5.x.y
</code>
where x and y is a versions of available branches, which can be displayed after:
<code lang="bash">
$ git branch -a
</code>
== Building and Installing ==
There are two simple methods to compile and install library using the command line or the [http://doc.qt.io/qtcreator/ QtCreator].
Before building you need to install Perl.
'''''Note:''' Perl is required only in the case of Qt5, see [http://wiki.qt.io/Building_Qt_5_from_Git here]. When using Qt4 just skip this point.''
=== Build and install from command line ===
'''''Note:''' ensure that the environment variables are set correctly:''
* ''correctly specified the path to the installed Qt4/Qt5''
* ''correctly specified the path to use the compiler''
* ''correctly specified the path to Perl''
The following are the recommended steps for building the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] library for Qt4/Qt5 from the command line.
<code lang="bash">
$ git clone git://code.qt.io/qt/qtserialport.git
$ mkdir qtserialport-build
$ cd qtserialport-build
$ qmake ../qtserialport/qtserialport.pro
</code>
and next if you use GCC compiler then to do:
<code lang="bash">
$ make
$ make install
</code>
'''''Note:''' on unix-like systems super user privileges might be required:''
<code lang="bash">
$ sudo make install
</code>
if you use MinGW compiler then to do:
<code lang="bash">
$ mingw32-make
$ mingw32-make install
</code>
if you use MSVC compiler then to do:
<code lang="bash">
$ nmake
$ nmake install
</code>
=== Build and install from QtCreator ===
Using [http://doc.qt.io/qtcreator/ QtCreator] is the simplest and fastest way to manually install the library. Before building you need to check that the desired toolchains (kits) of [http://doc.qt.io/qtcreator/ QtCreator] have been correctly configured.
Recommended steps to build the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] library for Qt4/Qt5 from [http://doc.qt.io/qtcreator/ QtCreator]:
* download and unpack the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] sources
* run [http://doc.qt.io/qtcreator/ QtCreator] and open the root "qtserialport/qtserialport.pro" project file
* get to "Projects->(Your Kit)->Build->Build Steps"
* add a new make "Build Step" and write to the "Make arguments" the '''install''' target
* from the menus, select "Rebuild Project qtserialport"
As a result, the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] library will be automatically compiled and installed into the desired Qt instance (according to the selected Kit).
'''''Note:''' on unix-like systems this method can be failed if Qt was installed into system directories. Super user privileges may be required for "install" target, so need to do:''
<code lang="bash">
sudo make install
</code>
''from the shadow directory.''
== Usage ==
To use the library, add serialport to the ''*.pro'' file of your project:
=== Qt4 ===
<code>
CONFIG += serialport
</code>
=== Qt5 ===
<code>
QT += serialport
</code>
Include the header files of [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] where appropriate:
<code>
#include <QSerialPort>
#include <QSerialPortInfo>
</code>
=== Simple example ===
Below is a simple example of ''main.cpp'':
<code>
#include <QCoreApplication>
#include <QDebug>
#include <QSerialPort>
#include <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();
}
</code>
== Documentation generation ==
There is no need to build the documentation, because the documentation is available [http://doc-snapshot.qt.io/qt5-stable/qtserialport-index.html here].
== For those wishing to contribute to the development ==
All development is done through [http://wiki.qt.io/Setting_up_Gerrit Gerrit]. Therefore, those wishing to be involved in the development process must have a Qt developer account.
More information about the registration process and development can be found [http://wiki.qt.io/Code_Reviews here]
== Possible problems ==
Fill up
== Bug Tracking ==
Use [https://bugreports.qt.io bugreports.qt.io], Project "Qt", Component "Serial Port".
<references />

Latest revision as of 11:32, 29 May 2018

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

NOTE: As of Qt 5.1, Qt Serial Port is now officially part of Qt. You no longer need to build Qt Serial Port manually; simply install Qt 5.1 or later and go to section Usage

Brief description

The QtSerialPort module is an add-on module for the Qt5 library, providing a single interface for both hardware and virtual serial ports.

Note: Also added support for Qt4.

Serial interfaces, due to their simplicity and reliability, are still popular in some industries like the development of embedded systems, robotics, etc.

Using the QtSerialPort module, developers can significantly reduce the time needed to implement Qt applications that require access to a serial interface.

Functionality

Currently, the module API contains two classes: QSerialPort and QSerialPortInfo.

QSerialPort

QSerialPort is the base class of the module and provides a set of basic methods and properties to access resources on serial ports.

Supports the following operating systems:

Operating system Support state Note
Windows XP/Vista/7/8/10 YES Full support
Windows CE NO (since 5.7) Tested only on 5 and 6 platforms in the emulator
Gnu/Linux YES Full support
MacOSX YES Full support
Others Unix YES All POSIX-compatible

Non-official partial Android Support (on own risk, only for rooted devices, in which is it possible to get/set a permissions to the /dev/ttyXYZ nodes) 1 2.

QSerialPortInfo

QSerialPortInfo is a helper class. It provides information on the available serial ports on the system.

Supports the following operating systems:

Operating system Support state Note
Windows XP/Vista/7/8/10 YES Full support (using SetupAPI)
Windows CE NO (since 5.7) Tested only on 5 and 6 platforms in the emulator
Gnu/Linux YES Full support (using libudev, sysfs or simple search in /dev)
MacOSX YES Full support
Others Unix YES All POSIX-compatible (only simple search in /dev)

Getting the source code

You should clone from the official mirror and track changes from there in order to keep the load on Gerrit down.

$ git clone git://code.qt.io/qt/qtserialport.git
$ cd qtserialport

This repository contains both versions for Qt4 and Qt5.

To take the version for Qt4 it is necessary to do[1]:

$ git checkout qt4-dev

To take the version for Qt5 it is necessary to do:

$ git checkout qt5.x.y

where x and y is a versions of available branches, which can be displayed after:

$ git branch -a

Building and Installing

There are two simple methods to compile and install library using the command line or the QtCreator.

Before building you need to install Perl.

Note: Perl is required only in the case of Qt5, see here. When using Qt4 just skip this point.

Build and install from command line

Note: ensure that the environment variables are set correctly:

  • correctly specified the path to the installed Qt4/Qt5
  • correctly specified the path to use the compiler
  • correctly specified the path to Perl

The following are the recommended steps for building the QtSerialPort library for Qt4/Qt5 from the command line.

$ git clone git://code.qt.io/qt/qtserialport.git
$ mkdir qtserialport-build
$ cd qtserialport-build
$ qmake ../qtserialport/qtserialport.pro

and next if you use GCC compiler then to do:

$ make
$ make install

Note: on unix-like systems super user privileges might be required:

$ sudo make install

if you use MinGW compiler then to do:

$ mingw32-make
$ mingw32-make install

if you use MSVC compiler then to do:

$ nmake
$ nmake install

Build and install from QtCreator

Using QtCreator is the simplest and fastest way to manually install the library. Before building you need to check that the desired toolchains (kits) of QtCreator have been correctly configured.

Recommended steps to build the QtSerialPort library for Qt4/Qt5 from QtCreator:

  • download and unpack the QtSerialPort sources
  • run QtCreator and open the root "qtserialport/qtserialport.pro" project file
  • get to "Projects->(Your Kit)->Build->Build Steps"
  • add a new make "Build Step" and write to the "Make arguments" the install target
  • from the menus, select "Rebuild Project qtserialport"

As a result, the QtSerialPort library will be automatically compiled and installed into the desired Qt instance (according to the selected Kit).

Note: on unix-like systems this method can be failed if Qt was installed into system directories. Super user privileges may be required for "install" target, so need to do:

sudo make install

from the shadow directory.

Usage

To use the library, add serialport to the *.pro file of your project:

Qt4

CONFIG += serialport

Qt5

QT += serialport

Include the header files of QtSerialPort where appropriate:


#include <QSerialPort>
#include <QSerialPortInfo>

Simple example

Below is a simple example of main.cpp:

#include <QCoreApplication>
#include <QDebug>

#include <QSerialPort>
#include <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();
}

Documentation generation

There is no need to build the documentation, because the documentation is available here.

For those wishing to contribute to the development

All development is done through Gerrit. Therefore, those wishing to be involved in the development process must have a Qt developer account.

More information about the registration process and development can be found here

Possible problems

Fill up

Bug Tracking

Use bugreports.qt.io, Project "Qt", Component "Serial Port".

  1. Now QtSerialPort has a separate branch for Qt4 (similar to Qt4 itself), which will be only in one instance.