Qt Serial Port: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Improve some docs about Abdroid support)
 
(58 intermediate revisions by 9 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|Български]]


[toc align_right="yes" depth="3"]
'''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 42: 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:


{background:#009900}. |''. Operating system |''. Support state |''. Note
{| class="wikitable"
|
!Operating system  
|Windows NT/2K/XP/Vista/7
!Support state  
!Note
|-
|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
|
|-
|Gnu/Linux
|Gnu/Linux
|YES
|YES
|Full support
|Full support
|
|-
|MacOSX
|MacOSX
|YES
|YES
|Full support
|Full support
|
|-
|Others Unix
|Others Unix
|YES
|YES
|All POSIX-compatible
|All POSIX-compatible
|
|}
|Symbian<ref>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.
</ref>
|YES
|Partially, tested only in the emulator
|


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 ===


h3. 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.
 
'''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:


{background:#009900}. |''. Operating system |''. Support state |''. Note
{| class="wikitable"
|
!Operating system  
|Windows NT/2K/XP/Vista/7
!Support state  
!|Note
|-
|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
|YES
|YES
|Full support
|Full support
|
|-
|Others Unix
|Others Unix
|YES
|YES
|All POSIX-compatible (only simple search in /dev)
|All POSIX-compatible (only simple search in /dev)
|
|}
|Symbian<ref>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.
</ref>
|YES
|Partially, tested only in the emulator
|


== View the source code ==
== Getting the source code ==


Recently launched a public mirror of the repository project at "Gitorious":http://qt.gitorious.org/qt/qtserialport
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.


Now everyone can freely and quickly view the latest changes through a WEB browser.
<code lang="bash">
$ git clone git://code.qt.io/qt/qtserialport.git
$ cd qtserialport
</code>


== Getting the source code ==
This repository contains both versions for Qt4 and Qt5.


=== For end-users ===
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>:


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 qt4-dev
</code>


Those who want to use Git can run the following command:
To take the version for Qt5 it is necessary to do:


<code>git clone git://gitorious.org/qt/qtserialport.git<code>
<code lang="bash">
$ git checkout qt5.x.y
</code>


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


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).
<code lang="bash">
$ git branch -a
</code>


You can then check out the repository using git, like this:
== Building and Installing ==


</code>git clone ssh://<username></code>codereview.qt.io:29418/qt/qtserialport.git</code>
There are two simple methods to compile and install library using the command line or the [http://doc.qt.io/qtcreator/ QtCreator].


…with your Gerrit user ID in place of <username>.
Before building you need to install Perl.  


== Building and Installing ==
'''''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 ===
=== Build and install from command line ===


Before building you need to:
'''''Note:''' ensure that the environment variables are set correctly:''
* 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.
* ''correctly specified the path to the installed Qt4/Qt5''
</ref>
* ''correctly specified the path to use the compiler''
* ensure that the environment variables are set correctly:
* ''correctly specified the path to Perl''
'''''' correctly specified the path to the installed Qt4/Qt5
 
'''''' correctly specified the path to use the compiler
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.
'''''' 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>
<code lang="bash">
* create a build directory that is in the same level as the directory with the source code
$ git clone git://code.qt.io/qt/qtserialport.git
<code>
$ mkdir qtserialport-build
/
$ cd qtserialport-build
|- /serialport-src
$ qmake ../qtserialport/qtserialport.pro
|- /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 lang="bash">
$ make
$ make install
</code>


<code>
'''''Note:''' on unix-like systems super user privileges might be required:''
cd serialport-build
<code lang="bash">
qmake ../serialport-src/qtserialport.pro
$ sudo make install
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]
</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>


h3. 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 212: Line 190:


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


h3. Qt5
=== Qt5 ===


<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 233: 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 243: 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 280: 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 />
<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.