What's New in Qt 5.6.0: QtSerialBus

Jeff Tranter


Jeff Tranter is a Qt Consulting Manager at ICS. Jeff oversees the architectural and high-level design of software systems for clients. Jeff’s leadership organizes global teams of developers on desktop and embedded applications.

By Jeff Tranter | Wednesday, March 16, 2016

QtSerialBus is a new Qt module, introduced as a technical preview in Qt 5.6.0. It supports two serial interface protocols that are commonly used on embedded systems: CAN bus and Modbus. In this blog post we'll take a brief look at this new module and what it provides.

What is CAN bus?

CAN bus (1), or Controller Area Network bus, is a vehicle bus standard that allows microcontrollers and devices to communicate with each other in applications without a host computer. It is a multi-master serial bus where all nodes are connected to each other through a two wire bus. A message-based protocol, it was originally designed for multiplexed electrical wiring within automobiles, but is also used in many other contexts.

CAN bus is one of five protocols used in the OBD-II (on-board diagnostics) standard, which has been mandatory for consumer vehicles in North America since 1996. A similar standard, EOBD, applies to vehicles sold in Europe. Unless your vehicle is very old, you will find an OBD-II connector under the dashboard, and all North American vehicles built since 2008 support CAN bus on the connector.

What is Modbus?

Modbus (2) is a serial communications protocol that has become a de facto standard, commonly used for connecting industrial electronic devices. It allows communication among multiple devices connected to the same network, often to connect a supervisory computer with a remote terminal unit in Supervisory Control and Data Acquisition (SCADA) systems.

Each device intended to communicate using Modbus is assigned a unique address. Many of the data types are named from its original use in driving relays. A single bit physical output is called a coil, and a single bit physical input is called a discrete input or a contact. Versions of the Modbus protocol exist for communication over serial ports and over Ethernet.

How To Obtain and Build QtSerialBus?

QtSerialBus is part of the Qt 5.6.0 (and later) releases. You can also get it directly from Git (3), which may allow you to use it with versions of Qt 5 older than Qt 5.6.0.

It is licensed, like most Qt modules, under the LGPLv3, GPLv2, and GPLv3 licenses or under a commercial license from The Qt Company.

If you want to build it from Git, here are some brief instructions on how to do so under Ubuntu Linux. It can be built in a similar manner on other platforms (the commands I typed are shown in bold):

# Get the latest code from Git
git clone http://code.qt.io/qt/qtserialbus.git
cd qtserialbus

# Run qmake
qmake
Info: creating cache file /home/tranter/work/qtserialbus/.qmake.cache
Checking for socketcan... yes
Checking for socketcan_fd... yes

# Build it
make

# Install it
sudo make install

What Hardware is Supported?

The QtSerialBus library uses back end drivers specific to different interfaces. For CAN bus, it currently supports the following back ends:

  1. SocketCAN, which uses Linux sockets and open source drivers.
  2. Peak CAN, which supports PCAN adaptors from PEAK-System Technik GmbH.
  3. TinyCAN, with support for Tiny-CAN adapters from MHS Elektronik.

For Modbus, there is currently one back end which doesn't depend on any external libraries. It supports RTU (serial) and TCP (Ethernet) communications.

Code Examples

The QtSerialBus module provides about 18 different C++ classes to support the two protocols in an object-oriented manner. Full documentation can be found from within Qt Assistant or the on-line version here (4).

It comes with one CAN bus programming example and three Modbus examples. All can be found in the examples folder. A screen shot of the CAN bus example is shown below. In order for it to function you will need a supported CAN bus interface, although with SocketCAN there is a virtual device driver you can load for testing purposes.

Screen shots of the three Modbus examples are shown below. Since Modbus can operate through a serial port or TCP socket, you can run them on a standard desktop system and send and receive data packets.

    

Be sure that you have a serial port (preferably two ports or two connected machines if you want to actually transfer data) or you can use TCP networking using the local machine and a non-privileged port (e.g. 127.0.0.1:1502).

Summary

A hallmark of Qt is being able to write code that is portable to multiple platforms. With QtSerialBus you now have a cross-platform way for your application to support CAN bus and Modbus. It is still considered a Technology Preview in Qt 5.6.0, so the module is expected to evolve and some of the APIs could change in the future. This will likely be finalized in Qt 5.7.0. Support for more hardware back ends is expected to be added in the future, as well as more serial protocols. Some possible protocols that come to mind are SPI, SMBus and I2C, all of which are commonly used on embedded systems.

References

  1. CAN bus, Wikipedia article, last accessed 1 Feb 2016, https://en.wikipedia.org/wiki/CAN_bus
  2. Modbus, Wikipedia article, last accessed 1 Feb 2016, https://en.wikipedia.org/wiki/Modbus
  3. QtSerialBus, Git repository, last accessed 1 Feb 2016, http://code.qt.io/cgit/qt/qtserialbus.git
  4. Qt Serial Bus, Qt documentation, last accessed 16 Mar 2016, http://doc.qt.io/qt-5/qtserialbus-index.html


Comments

Comment: 
Make sure that the "make" and "make install" succeeded. It should install a shared library under your Qt install directoy in lib/libQt5SerialBus.so Unfortunately, I doubt that it will work with a Qt version that old. It fails to build for me with Qt 5.5.1. Your best option is probably to try to use Qt 5.6.0 or later which includes the QtSerialBus module.