Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Qt Serial Port: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Improve some docs about Abdroid support)
 
(50 intermediate revisions by 8 users not shown)
Line 1: Line 1:
'''English'''
{{LangSwitch}}
| [[:QtSerialPort_German|Deutsch]]
__TOC__
| [[:QtSerialPort_Russian|Русский]]
| [[:QtSerialPort_Polish|Polski]]
| [[:QtSerialPort_Dutch|Nederlands]]
| [[:QtSerialPort_Portuguese|Português]]
| [[:QtSerialPort_Spanish|Español]]
| [[:QtSerialPort_Ukrainian|Українською]]
| [[:QtSerialPort_Persian|فارسی]]
| [[:QtSerialPort_Arabic|عربي]]
| [[:QtSerialPort_Korean|한국어]]
| [[:QtSerialPort_Finnish|Suomi]]
| [[:Wiki Home Catalan|Català]]
| [[:Wiki Home Italian|Italiano]]
| [[:QtSerialPort_Japanese|日本語]]
| [[:QtSerialPort_French|Français]]
| [[:Wiki Home Hungarian|Magyar]]
| [[:QtSerialPort_Kannada|ಕನ್ನಡ]]
| [[:QtSerialPort_Hindi|हिन्दी]]
| [[:QtSerialPort_Thai|ภาษาไทย]]
| [[:QtSerialPort_SimplifiedChinese|简体中文]]
| [[:QtSerialPort_Bulgarian|Български]]


 
'''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]]'''
= QtSerialPort =
 
'''''Note''': this page is work in progress, needs review.''
 
== News ==
 
Fill up
 
02/26/2013 - The project moved from the playground to the trunk of Qt5!


== Brief description ==
== 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.
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.''
'''''Note:''' Also added support for Qt4.''
Line 41: Line 12:
Serial interfaces, due to their simplicity and reliability, are still popular in some industries like the development of embedded systems, robotics, etc.
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.
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.
 
== History ==
 
'''QtSerialPort''' originated from the third-party library "QSerialDevice":http://gitorious.org/qserialdevice/qserialdevice/trees/2.0 (branch 2.0), which was recently moved to a repository on https://codereview.qt.io/
 
This was done to allow more open development, and to gather and coordinate a community that is interested in developing this module.


== Functionality ==
== Functionality ==


Currently, the module API contains two classes: '''QSerialPort''' and '''QSerialPortInfo'''.
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 ===
=== QSerialPort ===


'''QSerialPort''' is the base class of the module and provides a set of basic methods and properties to access resources on serial ports.
[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:
Supports the following operating systems:


{| class="wikitable"
{| class="wikitable"
! Operating system  
!Operating system  
! Support state  
!Support state  
! Note
!Note
|-
|-
|Windows NT/2K/XP/Vista/7
|Windows XP/Vista/7/8/10
|YES
|YES
|Full support
|Full support
|-
|-
|Windows CE
|Windows CE
|YES
|NO (since 5.7)
|Tested only on 5 and 6 platforms in the emulator
|Tested only on 5 and 6 platforms in the emulator
|-
|-
Line 83: Line 48:
|YES
|YES
|All POSIX-compatible
|All POSIX-compatible
|-
|Symbian¹
|YES
|Partially, tested only in the emulator
|}
|}


¹For OS Symbian development of the module is suspended due to lack of developers, as well as the fact that Nokia has abandoned the platform and will not include support for it in Qt5.
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 ===
=== QSerialPortInfo ===


'''QSerialPortInfo''' is a helper class. It provides information on the available serial ports on the system.
[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:
Supports the following operating systems:


{| class="wikitable"
{| class="wikitable"
|Operating system  
!Operating system  
! Support state  
!Support state  
! Note
!|Note
|-
|-
|Windows NT/2K/XP/Vista/7
|Windows XP/Vista/7/8/10
|YES
|YES
|Full support (using SetupAPI)
|Full support (using SetupAPI)
|-
|-
|Windows CE
|Windows CE
|YES
|NO (since 5.7)
|Tested only on 5 and 6 platforms in the emulator
|Tested only on 5 and 6 platforms in the emulator
|-
|-
|Gnu/Linux
|Gnu/Linux
|YES
|YES
|Full support (using libudev or simple search in /dev)
|Full support (using libudev, sysfs or simple search in /dev)
|-
|-
|MacOSX
|MacOSX
Line 121: Line 82:
|YES
|YES
|All POSIX-compatible (only simple search in /dev)
|All POSIX-compatible (only simple search in /dev)
|-
|Symbian²
|YES
|Partially, tested only in the emulator
|}
|}


²Symbian development of the module is suspended due to lack of developers, as well as the fact that Nokia has abandoned the platform and will not include support for it in Qt5.
== 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.


== View the source code ==
<code lang="bash">
$ git clone git://code.qt.io/qt/qtserialport.git
$ cd qtserialport
</code>


Recently launched a public mirror of the repository project at "Gitorious":http://qt.gitorious.org/qt/qtserialport
This repository contains both versions for Qt4 and Qt5.  


Now everyone can freely and quickly view the latest changes through a WEB browser.
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>:


== Getting the source code ==
<code lang="bash">
$ git checkout qt4-dev
</code>


=== For end-users ===
To take the version for Qt5 it is necessary to do:


To get the current snapshot of the source code as an archive click on "this":http://qt.gitorious.org/qt/qtserialport/archive-tarball/stable link.
<code lang="bash">
$ git checkout qt5.x.y
</code>


Those who want to use Git can run the following command:
where x and y is a versions of available branches, which can be displayed after:


<code>git clone git://gitorious.org/qt/qtserialport.git<code>
<code lang="bash">
$ git branch -a
</code>


=== For developers ===
== Building and Installing ==


To get the source code and contribute to the development you need to register a Qt developer account and set it up for use with Gerrit and Git (see "Setting up Gerrit":http://wiki.qt.io/Setting_up_Gerrit).
There are two simple methods to compile and install library using the command line or the [http://doc.qt.io/qtcreator/ QtCreator].


You can then check out the repository using git, like this:
Before building you need to install Perl.


</code>git clone ssh://<username>@codereview.qt.io:29418/qt/qtserialport.git</code>
'''''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.''


…with your Gerrit user ID in place of <username>.
=== Build and install from command line ===


== Building and Installing ==
'''''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''


=== Build and install from command line ===
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.


Before building you need to:
<code lang="bash">
* install Perl<ref>Perl is required only in the case of Qt5, see "here":http://wiki.qt.io/Building_Qt_5_from_Git. When using Qt4 just skip this point.
$ git clone git://code.qt.io/qt/qtserialport.git
</ref>
$ mkdir qtserialport-build
* ensure that the environment variables are set correctly:
$ cd qtserialport-build
'''''' correctly specified the path to the installed Qt4/Qt5
$ qmake ../qtserialport/qtserialport.pro
'''''' correctly specified the path to use the compiler
'''''' correctly specified the path to Perl<ref>Perl is required only in the case of Qt5, see "here":http://wiki.qt.io/Building_Qt_5_from_Git. When using Qt4 just skip this point.
</ref>
* create a build directory that is in the same level as the directory with the source code
<code>
/
|- /serialport-src
|- /serialport-build
</code>
</code>


The following are the recommended steps for building the '''QtSerialPort''' library on Qt4/Qt5 from the command line.
and next if you use GCC compiler then to do:


<code>
<code lang="bash">
cd serialport-build
$ make
qmake ../serialport-src/qtserialport.pro
$ make install
make [or 'nmake' for MSVC compiler, or 'mingw32-make' for MinGW compiler]
</code>
make install [or 'nmake install' for MSVC compiler, or 'mingw32-make install' for MinGW compiler]
 
'''''Note:''' on unix-like systems super user privileges might be required:''
<code lang="bash">
$ sudo make install
</code>
</code>


'''''Note:''' on '''nix systems super user privileges might be required:_
if you use MinGW compiler then to do:
<code>
 
sudo make install
<code lang="bash">
$ mingw32-make
$ mingw32-make install
</code>
</code>


=== Build and install from the QtCreator ===
if you use MSVC compiler then to do:


Using QtCreator is the simplest and fastest way to manually install the library.
<code lang="bash">
$ nmake
$ nmake install
</code>


Before building you need to:
=== Build and install from QtCreator ===


''' install Perl<ref>Perl is required only in the case of Qt5, see "here":http://wiki.qt.io/Building_Qt_5_from_Git. When using Qt4 just skip this point.
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.
</ref> and check that the path to Perl was correctly specified in a global env
* check that the desired toolchains (kits) of QtCreator have been correctly configured


Recommended steps to build the '''QtSerialPort''' library for Qt4/Qt5 from QtCreator:
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 QtSerialPort sources
* download and unpack the [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] sources
* run QtCreator and open the "qtserialport.pro" project file
* run [http://doc.qt.io/qtcreator/ QtCreator] and open the root "qtserialport/qtserialport.pro" project file
* get to "Projects->(Your Kit)->Build->Build Steps"
* get to "Projects->(Your Kit)->Build->Build Steps"
* add a new make "Build Step" and write to the "Make arguments" the '''install''' target
* add a new make "Build Step" and write to the "Make arguments" the '''install''' target
* from the menus, select "Rebuild Project qtserialport"
* 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).
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 *nix systems this method can be failed if Qt was installed from the repositories into system directories. Super user privileges may be required for "make install" target_
'''''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 ==
== Usage ==
Line 217: Line 190:


<code>
<code>
CONFIG ''= serialport
CONFIG += serialport
</code>
</code>


Line 223: Line 196:


<code>
<code>
QT''= serialport
QT += serialport
</code>
</code>


Include the header files of QtSerialPort where appropriate:
Include the header files of [http://doc.qt.io/qt-5/qtserialport-index.html QtSerialPort] where appropriate:
<code>
<code>
#include <QtSerialPort/QSerialPort>
#include <QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QSerialPortInfo>
</code>
</code>
Line 238: Line 211:
Below is a simple example of ''main.cpp'':
Below is a simple example of ''main.cpp'':
<code>
<code>
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QtCore/QDebug>
#include <QDebug>


#include <QtSerialPort/QSerialPort>
#include <QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QSerialPortInfo>


QT_USE_NAMESPACE
QT_USE_NAMESPACE
Line 248: Line 221:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
QCoreApplication a(argc, argv);
    QCoreApplication a(argc, argv);


// Example use QSerialPortInfo
    // Example use QSerialPortInfo
foreach (const QSerialPortInfo &amp;info, QSerialPortInfo::availablePorts()) {
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
qDebug() << "Name : " << info.portName();
        qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
        qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
        qDebug() << "Manufacturer: " << info.manufacturer();


// Example use QSerialPort
        // Example use QSerialPort
QSerialPort serial;
        QSerialPort serial;
serial.setPort(info);
        serial.setPort(info);
if (serial.open(QIODevice::ReadWrite))
        if (serial.open(QIODevice::ReadWrite))
serial.close();
            serial.close();
}
    }


return a.exec();
    return a.exec();
}
}
</code>
</code>
'''''Note:''' CONFIG ''= serial port / QT''= serialport must be the first or second line in your .pro file.''


== Documentation generation ==
== Documentation generation ==


There is no need to build the documentation, because the documentation is available "here":http://doc-snapshot.qt.io/qt5-stable/qtserialport-index.html.
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 ==
== For those wishing to contribute to the development ==


All development is done through "Gerrit":http://wiki.qt.io/Setting_up_Gerrit. Therefore, those wishing to be involved in the development process must have a Qt developer account.
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 "here":http://wiki.qt.io/Code_Reviews
More information about the registration process and development can be found [http://wiki.qt.io/Code_Reviews here]


== Possible problems ==
== Possible problems ==
Line 285: Line 256:
== Bug Tracking ==
== Bug Tracking ==


Use "bugreports.qt.io":https://bugreports.qt.io, Project "Qt", Component "Serial Port".
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.