Serial Port Interfacing in Qt

     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.

Gui for the application

  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
  • qmake
  • make
  • 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.

$sudo qtcreator

Now make the Gui as shown :

The objects are clear from the object and classes listing in the right side.

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:

port->setBaudRate(BAUD_9600);

port->setStopBit();

port->setDataBit(); etc..

Here is the final output screen.

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:

The vivibootloader communication via SerialManager

here is the final codeuartAssistant.tar for downloading…

Hope you find this useful 🙂 thankyou..

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *