Qt Serial Port: Difference between revisions

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


[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
'''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 21: 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 &quot;QSerialDevice&amp;quot;: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<br />|<br />|Windows NT/2K/XP/Vista/7<br />|YES<br />|Full support<br />|<br />|Windows CE<br />|YES<br />|Tested only on 5 and 6 platforms in the emulator<br />|<br />|Gnu/Linux<br />|YES<br />|Full support<br />|<br />|MacOSX<br />|YES<br />|Full support<br />|<br />|Others Unix<br />|YES<br />|All POSIX-compatible<br />|<br />|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.
{| class="wikitable"
</ref><br />|YES<br />|Partially, tested only in the emulator<br />|
!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].


<br />h3. QSerialPortInfo
=== QSerialPortInfo ===
<br />'''QSerialPortInfo''' is a helper class. It provides information on the available serial ports on the system.
<br />Supports the following operating systems:
<br />{background:#009900}. |''. Operating system |''. Support state |''. Note<br />|<br />|Windows NT/2K/XP/Vista/7<br />|YES<br />|Full support (using SetupAPI)<br />|<br />|Windows CE<br />|YES<br />|Tested only on 5 and 6 platforms in the emulator<br />|<br />|Gnu/Linux<br />|YES<br />|Full support (using libudev or simple search in /dev)<br />|<br />|MacOSX<br />|YES<br />|Full support<br />|<br />|Others Unix<br />|YES<br />|All POSIX-compatible (only simple search in /dev)<br />|<br />|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><br />|YES<br />|Partially, tested only in the emulator<br />|


== View the source code ==
[http://doc.qt.io/qt-5/qserialportinfo.html QSerialPortInfo] is a helper class. It provides information on the available serial ports on the system.


Recently launched a public mirror of the repository project at &quot;Gitorious&amp;quot;:http://qt.gitorious.org/qt/qtserialport
Supports the following operating systems:


Now everyone can freely and quickly view the latest changes through a WEB browser.
{| 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 ==
== Getting the source code ==


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


To get the current snapshot of the source code as an archive click on &quot;this&amp;quot;:http://qt.gitorious.org/qt/qtserialport/archive-tarball/stable link.
<code lang="bash">
$ git clone git://code.qt.io/qt/qtserialport.git
$ cd qtserialport
</code>


Those who want to use Git can run the following command:
This repository contains both versions for Qt4 and Qt5.


<code>git clone git://gitorious.org/qt/qtserialport.git<code>
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>:


=== For developers ===
<code lang="bash">
$ git checkout qt4-dev
</code>


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 &quot;Setting up Gerrit&amp;quot;:http://wiki.qt.io/Setting_up_Gerrit).
To take the version for Qt5 it is necessary to do:


You can then check out the repository using git, like this:
<code lang="bash">
$ git checkout qt5.x.y
</code>


</code>git clone ssh://&amp;lt;username&amp;gt;</code>codereview.qt.io:29418/qt/qtserialport.git</code>
where x and y is a versions of available branches, which can be displayed after:


…with your Gerrit user ID in place of &lt;username&amp;gt;.
<code lang="bash">
$ git branch -a
</code>


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


Before building you need to:<br />* install Perl<ref>Perl is required only in the case of Qt5, see &quot;here&amp;quot;:http://wiki.qt.io/Building_Qt_5_from_Git. When using Qt4 just skip this point.
'''''Note:''' ensure that the environment variables are set correctly:''
</ref><br />* ensure that the environment variables are set correctly:<br />'''''' correctly specified the path to the installed Qt4/Qt5<br />'''''' correctly specified the path to use the compiler<br />'''''' correctly specified the path to Perl<ref>Perl is required only in the case of Qt5, see &quot;here&amp;quot;: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><br />* create a build directory that is in the same level as the directory with the source code<br /><code><br />/<br />|- /serialport-src<br />|- /serialport-build<br /></code>
* ''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.


The following are the recommended steps for building the '''QtSerialPort''' library on Qt4/Qt5 from the command line.
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]:


<code><br />cd serialport-build<br />qmake ../serialport-src/qtserialport.pro<br />make [or 'nmake' for MSVC compiler, or 'mingw32-make' for MinGW compiler]<br />make install [or 'nmake install' for MSVC compiler, or 'mingw32-make install' for MinGW compiler]<br /></code>
* 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"


'''''Note:''' on '''nix systems super user privileges might be required:_<br /><code><br />sudo make install<br /></code>
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).
<br />h3. Build and install from the QtCreator
 
<br />Using QtCreator is the simplest and fastest way to manually install the library.
'''''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:''
<br />Before building you need to:
<code lang="bash">
<br />''' install Perl<ref>Perl is required only in the case of Qt5, see &quot;here&amp;quot;:http://wiki.qt.io/Building_Qt_5_from_Git. When using Qt4 just skip this point.
sudo make install
</ref> and check that the path to Perl was correctly specified in a global env<br />* check that the desired toolchains (kits) of QtCreator have been correctly configured
</code>
<br />Recommended steps to build the '''QtSerialPort''' library for Qt4/Qt5 from QtCreator:
''from the shadow directory.''
<br />* download and unpack the QtSerialPort sources<br />* run QtCreator and open the &quot;qtserialport.pro&amp;quot; project file<br />* get to &quot;Projects-&gt;(Your Kit)<s>&gt;Build</s>&gt;Build Steps&amp;quot;<br />* add a new make &quot;Build Step&amp;quot; and write to the &quot;Make arguments&amp;quot; the '''install''' target<br />* from the menus, select &quot;Rebuild Project qtserialport&amp;quot;
<br />As a result, the QtSerialPort library will be automatically compiled and installed into the desired Qt instance (according to the selected Kit).
<br />'''''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 &quot;make install&amp;quot; target_


== Usage ==
== Usage ==
Line 104: Line 189:
=== Qt4 ===
=== Qt4 ===


<code><br />CONFIG ''= serialport<br /></code>
<code>
<br />h3. Qt5
CONFIG += serialport
<br /><code><br />QT''= serialport<br /></code>
</code>


Include the header files of QtSerialPort where appropriate:<br /><code><br /><br />#include &lt;QtSerialPort/QSerialPort&amp;gt;<br />#include &lt;QtSerialPort/QSerialPortInfo&amp;gt;<br />…<br /></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 ===
=== Simple example ===


Below is a simple example of ''main.cpp'':<br /><code><br />#include &lt;QtCore/QCoreApplication&amp;gt;<br />#include &lt;QtCore/QDebug&amp;gt;
Below is a simple example of ''main.cpp'':
<code>
#include <QCoreApplication>
#include <QDebug>


#include &lt;QtSerialPort/QSerialPort&amp;gt;<br />#include &lt;QtSerialPort/QSerialPortInfo&amp;gt;
#include <QSerialPort>
#include <QSerialPortInfo>


QT_USE_NAMESPACE
QT_USE_NAMESPACE


int main(int argc, char *argv[])<br />{<br /> QCoreApplication a(argc, argv);
int main(int argc, char *argv[])
 
{
// Example use QSerialPortInfo<br /> foreach (const QSerialPortInfo &amp;info, QSerialPortInfo::availablePorts()) {<br /> qDebug() &lt;&lt; &quot;Name : &quot; &lt;&lt; info.portName();<br /> qDebug() &lt;&lt; &quot;Description : &quot; &lt;&lt; info.description();<br /> qDebug() &lt;&lt; &quot;Manufacturer: &quot; &lt;&lt; info.manufacturer();
    QCoreApplication a(argc, argv);


// Example use QSerialPort<br /> QSerialPort serial;<br /> serial.setPort(info);<br /> if (serial.open(QIODevice::ReadWrite))<br /> serial.close();<br /> }
    // Example use QSerialPortInfo
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        qDebug() << "Name : " << info.portName();
        qDebug() << "Description : " << info.description();
        qDebug() << "Manufacturer: " << info.manufacturer();


return a.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
        // Example use QSerialPort
        QSerialPort serial;
        serial.setPort(info);
        if (serial.open(QIODevice::ReadWrite))
            serial.close();
    }


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


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


There is no need to build the documentation, because the documentation is available &quot;here&amp;quot;: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 &quot;Gerrit&amp;quot;: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 &quot;here&amp;quot;: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 144: Line 256:
== Bug Tracking ==
== Bug Tracking ==


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