Thursday, February 14, 2013

QtSerialPort in Qt 5.1

Hi everyone,

I would like to introduce you to a project I have been working on lately under the Qt Project umbrella. Hope, you find it useful. :)



The serial port technology is quite old, but still in active use today. It is mostly used for communicating with modems, sending control commands and so forth where the universal serial bus is not necessary. I am sure many of you recall the time when soldering the pins and cable manually to get it right for the embedded boards. :-)

About a year ago, I was looking for a library for my project to handle the serial port communication with the modems I needed. As I had been about to make a touch friendly UI with QML, I thought it would have been nice to have a Qt solution. It would have been phantastic to communicate with the modem on my Windows, or Linux host.

There has been an old library around for a while, called QExtSerialPort. Unfortunately, its API looked weird and the project had been abandoned for a while without people contributing to it. Then, a chinese person began to fix certain issues, but still: the design had been carried for long from the old ages.

Serial Port Cable

Qt Project Playground

The Qt Project established a nice opportunity for contributors to start a new project, or move an existing to the Playground area. There was a discussion on the development mailing list to figure out the rules for handling this. Around the time, when I requested the QtAudio3D project into the Playground area of the Qt Project, there was a russian person, Denis, who initiated a community project into Playground in his own pastime to make a great developer experience for serial port based projects across platforms.

The library was supporting only Qt5 back then, and my project was based on Qt4. This is understandable as Qt5 was not realeased for the time, so we were unable to take the risks of the moving target. I decided to give the project a go and make support for Qt4. Not much later (i.e. 1-2 weeks), I got the first changes into the Qt Project, following the Gerrit workflow. By that time, it was more or less possible to use QtSerialPort with Qt4.

Gerrit - Code Review Tool

Our work has been achieved by using the Qt instance of the awesome Gerrit codereview tool. Almost every commit got at least one review, but many times two, or sometimes even more. I think that is nice, and helped to guarantee the quality of the work even without having a Continous Integration (CI) in place for the time.

As time time had flied, I had more and more feature requests and own changes for those to implement. That is how the library kept rolling, getting new features and bugfixes. The API was also a bit too low-level, and I began to send patchs to refactor the API to be more Qt style'ish. As I am a geek, I instantly wrote a command line based example to enumerate the devices. It was necessary for me to get the project tested on my pandaboard where the UI was not available. There were also important features added like support for gadget serial driver on Linux and so forth.

The command line enumerator example

The initial plan was to make QtSerialPort available as an add-on module for Qt 5.0. This had been proposed on the development mailing list last year. You can read the platforms and environment supported in that threa. Unfortunately, the Qt Project did not have enough capacity for this integration. Likely, it was also good for us because we were able to clean up the remaining uncertain bits of the API. I am not claiming it is now perfect, but it looks much better than it used to be. :-)

Future plans


As you can read in the topic of this post, QtSerialPort makes it for Qt 5.1. This has been approved by Lars, the Qt Project Chief Maintainer, in the aforementioned thread. This is a really great and exciting news for us. It is also a very good prove for the Qt Project that the idea actually works. Community people can contribute with new modules under the same umbrella with the companies behind the Qt Project.

The main short term plan is to get the integration right in terms of Jira and Gerrit to move out of the Playground scope. As the CI integration has happened lately, the project is now also covered for build tests, so we cannot end up in situations like having regressions as before. It is not so simple to maintain such a project like this without proper CI integration because we experienced many regressions on one platform when we tried to fix issues or add new features.

FTDI USB-Serial adapter

As you may already know, it is not simple to test the serial port projects automatically. There had been an initiation for a Qt Mock project from me last April, but it did not yet manage to reach any progress. Nevertheless, we are working on making tests available as much as possible.

My personal plan is to write a KDE frontend for the library, preferrably also a terminal emulator. We already have a terminal emulator example in place to represent the developer and user experience for the project. It is not full-fledged, but it has been working nice for the basic needs. You can set the baudrate, parity and stop bits, flow control et cetera. Something like that would be nice in the KDE project, or perhaps even a plasmoid. There has been a CuteCom around for a while, but when I briefly discussed the project with Alexander Neundorf last summer at aKademy in Tallin, it did not support all the platforms we need.

Terminal example

As you may already know, the QNX platform is also getting closer to a Tier 1 stage in the Qt Project. Blackberry, formerly known as Research In Motion, and KDAB made an excellent job in that area. I have been considering to add a QNX backend support for the project at some point.



First of all, many thanks to the Qt Project!

Secondly, many thanks to all the contributors to QtSerialPort!


The reviewer statistic is generated by the following command: git log --grep '^ *Reviewed-by:' | sed -n 's/^ *Reviewed-by: *//p' |  sed 's/ *<.*>//' | sort | uniq -c -i | sort -n -r



  1. This is fantastic news, and quite a coincidence since I'm on the subway home from school, where I've been spending the afternoon hacking on a school project using QtSerialPort :) We are using it to communicate with a Bluetooth wristband sensor that measures electrodermal activity.

    We love your library! Congrats on making it into Qt 5.1!

  2. This is great news! The serial port may not be used on the common desktop much anymore, but definitely still is used for automation tasks. I've been planning to write an irrigation controller application that will utilize a USB relay card to control the valves. This relay card presents itself as a serial port to which you send commands to control the relays. So I very much look forward to this new API.

  3. Great timing for me too, serial ports are still very much active. Although from the DB-9 picture at the top I now realize I bought a male adapter when I should have bought a female adapter instead. Crap.

  4. What a lovely coincidence! I'm working on reading data from a 60Tm industrial scale and the equipment interfaces are RS232 (Rx/Tx) or RS485 !

  5. Cool I have quite some stuff using serial on my Qt app, I have been setting the flags using c libs and using QSocketNotifier to know when there's something to read, looking forward to replace my code with it :)

  6. Thank you to all for the feedback. You rock. :)

    Please let us know if you have any problems with the API for the current functionality, if the doc was unclear, or if there is anything you think should be improved. As for now, we prefer to stablize the current functionality for the release rather than adding new features.

    We still have some time to work together on such issues before the release.