Serial Port Interfacing In Qt
Posted by Shantanu Sharma
Well, for an embedded system guy, serial port is one of the most popular way to make your PC communicate with a target board, be it SBC or some serial based automation sort of work. Now, challenge is to use your own serial terminal software 😛 Well that’s the least what can be expected from a Qt Application developer.
So this post is all about building an application which opens a serial port, and communicate with it.
Now, the first need of the project is the library to work with. However, unlike Qt5.0, Qt4.0 does not come with serial port libraries, we have to include external library for the project.
One of such library is QextSerialPort which is supported by Qt4.0…
Follow these steps for downloading, building this library.
- cd <any folder (preferrably to the QtSDK installation folder)>
git clone https://code.google.com/p/qextserialport/
- cd qextserialport
- make install
This will build this library. Now you can browse the folder for example programs.
Coming to the project. Since we need super-user permission to work with serial port. so better open Qt in su mode.
Now make the Gui as shown :
The use of all these comboBox elements are obvious from the names. There are two modes for the Polling is the method which utilises a timer to collect the data from the serial buffer after the timer runs out.
The status label in the bottom is just to show the status of the port, whether it is opened, closed etc..
Now comes the programming part:
1. Create a Qt Gui based dialog project named “serialManager.pro”.
2. Create the UI as above. You can take the help from the example program in qextserialport library.
3. For static compilation of the projects, which is preferred here, these are the steps:
- Add following line to the serialManager.pro : (include /opt/QtSDK/qextserialport/src/qextserialport.pri)
- Just include qextserialport.h in serialManager.h
4. To create a port:
QextSerialPort *port = new QextSerialPort(<portname>, <Querymode>);
to add the setting like databit, stopbit, etc syntax is as follows:
Here is the final output screen.
However, make sure to short-circuit the RX -TX pins of DB-9 connector 😛
In case of my mini2440 connection to serialPort @115200 baudrate
the output screen looks like this:
here is the final codeuartAssistant.tar for downloading…
Hope you find this useful 🙂 thankyou..