QT and QExtSerialPort

So I’ve successfully compiled and built the QT framework on my Windows Laptop using MinGW C/C++ compiler and installed QT Creator 0.9 alpha as my IDE. I originally got complimation working with Eclipse but opted for QT Creator as it looks handy since it made especially for QT.

I am not going to go straight into coding without designing my program first, what is needed etc so I decided I’d be best working out how to do serial I/O. Serial I/O is very simple on Linux since everything on Linux is a file! /dev/ttySxx is usually the file associated with the serial charactor device. But this is no good on my Windows PC so I searched for a POSIX based Serial I/O class or library. Luckly a POSIX library for Serial Comm was developed for QT. The project is located at http://qextserialport.sourceforge.net/. I downloaded version 1.1 to see how easy it would be to do serial communication.

So I took at a look at the QT project file and saw TEMPLATE = lib so this project builds a library what is expected. I navigated to the directory and done a qmake to create the Makefile and a mingw32-make.exe to build the library using MinGW and QT. (I had to set my path to ensure that all the minGW bin utilities were found system wide set PATH=%PATH%;C:\MingGW\bin. After the building I found two files in the build folder: qextserialport.dll and libqextserialport.a.

Built files of QExtSerialPort

Built files of QExtSerialPort

So now it was time to create a project and test it.

I created a test QT console project. I copied the two library files into the root directory of my project. I also copied in the required header files that are required by the compiler during complimation time. All these files are found in the QExtSerialPort root directory. There’s a few of them there. So now ready to start using them.

To use the files wasn’t as easy as just including the header file, qextserialport.h. I also had to modify the QT Project file in order to include the library for use:

I had to include these few lines:

INCLUDEPATH += C:/Documents and Settings/Donal/Desktop/Applications/qextserialport-1.1/qextserialport
QMAKE_LIBDIR += C:/Documents and Settings/Donal/Desktop/Applications/qextserialport-1.1/qextserialport/build

LIBS += -lqextserialport
unix:DEFINES = _TTY_POSIX_
win32:DEFINES = _TTY_WIN_ QWT_DLL QT_DLL

I wrote some basic code to open the serial port and send a command to the ECUEmu program. I had problems opening the com port, (Com13) however. Under further investigation it is actually \\\\.\\COM13 i had to use as the port name since it > Com 8 (http://support.microsoft.com/kb/115831). However, I changed my USB->Serial adaptor to COM8 (a free one) to save hassle in other programs that only allowed to select from 1 – 8.

The code is as follows:


#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <qextserialport.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

PortSettings portSettings;
portSettings.BaudRate = BAUD9600;
portSettings.DataBits = DATA_8;
portSettings.Parity = PAR_NONE;
portSettings.StopBits = STOP_1;
portSettings.FlowControl = FLOW_OFF;
portSettings.Timeout_Millisec = 0;

QextSerialPort* port = new QextSerialPort(“COM8″,portSettings);

bool res = false;
res = port->open(QextSerialPort::ReadWrite);

if(res)
qDebug(“Connected!!\n”);
else
qDebug(“Connection failed!!\n”);

QString message(“010C\r”);
int total = port->write(message.toAscii(),message.length());
qDebug (“Total written = %d”, total );

port->close();
return a.exec();
}

Output:

QExtSerialPort Test Program

QExtSerialPort Test Program

Friday, January 16th, 2009 QT Embdedded Programming

7 Comments to QT and QExtSerialPort

  • Vinay M says:

    I’m new to Qt, Now I want to build a GUI for serial port communication and use the serial PORT which is available on my board. Plz can you suggest me, How to communicate with serial port>……..

  • Dmitriy says:

    Appreciated for this article. I started to work with QT creator not to long ago, to develop GUI test application for my embedded devices while communicating trough the serial port.
    The application work fine using QextSerialPort under windows os. I am looking for example that would compile and work under Linux (Slackware) using serial port (/dev/ttyS0).
    including the QextSerialport.h file as in windows will not compile under Linux platform.
    Any suggestions would be appreciated.
    Thank you in advance.

  • Donal says:

    Hey There,

    I got this working on Linux using /dev/ttyS0 no problem. What problem are you having? I think it compiled straight out of the box after downloading Qextserialport.

    Thanks,
    Donal

  • Dmitriy says:

    the show of problem starts here:
    port = new QextSerialPort(“/dev/ttyS0″);

    error message:
    /home/dmitriys/QTprojects/VMS_linux/Linux_VMS_App/vms.cpp:46: error: invalid use of incomplete type ‘struct QextSerialPort’

  • Donal says:

    Hi,

    Sounds like it cannot find QextSerial port.

    Have you something like this in your QMake pro file:

    INCLUDEPATH += path/to/qextserialport/includedir/
    QMAKE_LIBDIR += path/to/qextserialport/build
    LIBS += -Lpath/to/qextserialport/build/ \
    -lqextserialport

    Then in your file:

    #include “qextserialport.h”

    Make sure the .a file is in the build output after you’ve built the QExtSerial port library.

    Hope that helps

  • Mark Mikaelsen says:

    Hi
    Im very new with Qt so appologies if this is a newbie question :)

    I tried out implementing the “test.pro” (in my case called SerialTest.pro) with your code and the newest version (feb 7 2010) of QextSerialPort from http://code.google.com/p/qextserialport however when i compile the project i get the error
    “c:\QtTutorial1\SerialTest\SerialTest\SerialTest.pro:14: Parse Error (‘QWT_DLL QT_DLL’)”

    First off, the QextSerialPort lib compiled just fine and I have placed the lib and dll files correctly, so no problem there.

    my main file looks like this:

    INCLUDEPATH += C:/QtTutorial1/SerialTest/SerialTest
    QMAKE_LIBDIR += C:/QtTutorial1/SerialTest/SerialTest/build

    LIBS += -lqextserialport

    unix:DEFINES = _TTY_POSIX_
    win32:DEFINES = _TTY_WIN_ \

    QWT_DLL \
    QT_DLL

    QT -= gui
    TARGET = SerialTest
    CONFIG += console
    CONFIG -= app_bundle
    TEMPLATE = app
    SOURCES += main.cpp
    HEADERS +=

    Hope you can spot the error.

  • Mark Mikaelsen says:

    I can see it didnt get the “HEADERS += qextserialport.h” in my previous post. It is included.

  • Leave a Reply

    You must be logged in to post a comment.