PDA

View Full Version : Adding QtSerialPort as a library -



HSPalm
29th June 2012, 16:20
Hello!
Thank you for having this newbie forum :)

I am trying to add a library/add-on to my project in QT Creator 2.4.1, QtSerialPort (http://qt-project.org/wiki/QtSerialPort).
Actually, I have also tried QSerialDevice (http://qt-apps.org/content/show.php/QSerialDevice?content=112039&PHPSESSID=fcb410664e641afec5d0a355634137ac) (QT 4 based) and QExtSerialPort (http://code.google.com/p/qextserialport/), but haven't had any luck with either... Decided to try full out with QtSerialPort since it's a QT supported ad-on and is still updated.

I don't really know how many errors I have produced trying to do this, so I don't know which to post here. I was kinda hoping someone could clear a couple things up for me before I start posting code and error messages? :confused:

I finally got rid of many error by using MinGW instead of visual studio for compiling (?!). On the QSerialPort page it gives a description for making and adding the DLLs, but this one time I managed to compile it I didn't have no QT 5 folder where it could be output, as stated. But it says to support QT 4? Arrghh, I'm just so frustrated with this. Been sitting here all day and don't even know what questions to ask.

Can I follow the steps in the link (QtSerialPort (http://qt-project.org/wiki/QtSerialPort)) for installing/using this library, and to what extend? I appreciate your answers very much....

Thanks,
Henrik

edit: have calmed down a bit and will no try to do this from scratch while documenting.

1.Starting at http://qt-project.org/wiki/QtSerialPort I download the source code at "Getting the source code: for users" by clicking the "this" link. I extract the contents of the .tar.gz to C:\Qt in a folder called qtplayground-qtserialport (same as tarball).

2. I skip the part about building qt5 from source, jumping straight to the "Building" section (building the library). I open the serialport.pro in Qt Creator, then this pops up:
7912
I don't do any changes in this window and click "finish". Before doing anything I go in the project settings and changing "Qt version" from "Qt 4.8.0 for Desktop - MSVC 2010 (Qt SDK)" to "Qt 4.8.0 for Desktop - MinGW (Qt SDK)".

3. I then press the "build" button (hammer?), and get 336 warnings saying the same thing:

c:\Qt\serialport-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\.qmake.cache:1: warning: Unmatched quotes are deprecated.
It points to line 1 and 2 in .qmake.cache which consists of two lines:

"SERIALPORT_PROJECT_ROOT = C:/Qt/qtplayground-qtserialport"
"SERIALPORT_BUILD_ROOT = C:/Qt/serialport-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug"

4. I then press the build-button again, having done nothing since getting the 336 warnings, and it builds just fine. Pressing the play/run button gives this in the output dialog:

Starting C:\Qt\serialport-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\examples\enumerator\debug\en umerator.exe...
The program has unexpectedly finished.
C:\Qt\serialport-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\examples\enumerator\debug\en umerator.exe exited with code -1073741515

5. Guide now says
After building the module, there will be two library files (Release and Debug builds, respectively) in the Qt5 qtbase binary directory: but I have no such folder. I have also searched my computer for occurrences of these files, with no luck hehe. Where do I go from here?

high_flyer
29th June 2012, 16:57
It is my recommendation, that you first understand the following:
1. What is a library - and what is the difference between a static and dynamic libs.
2. How a lib is built, what linking means and what is needed for linking to succeed.

Once you know the above, I guess most of the problems you face now will be clear to you - or at least, you will know where your problem is and you can ask the correct questions :)

I'd suggest you take any online tutorial that explains these topics, and follow it, until you understand it.

I finally got rid of many error by using MinGW instead of visual studio for compiling (?!).
Read this:
http://www.mingw.org/wiki/MixingCompilers
It basically boils down to:
If you are building everything your self (your libs and your application) - then you can build it with any compiler you like, but build all of them with the same compiler.
If you use a ready built libs from a third party, you have to know with which compiler they are built with, and you you should build your application with the same compiler/tool chain.

(Take a look at the download page of Qt, and you will see Qt builds for various compilers)

Also:
You should distinguish between compile errors - and link errors, and from what you have written, its not clear if your problems are linking or compiling.


I then press the build-button again, having done nothing since getting the 336 warnings, and it builds just fine. Pressing the play/run button gives this in the output dialog:
Look in the detailed build output, and paste here the first few errors you got.

kuzulis
30th June 2012, 19:43
2 HSPalm



3. I then press the "build" button (hammer?), and get 336 warnings saying the same thing:
c:\Qt\serialport-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug\.qmake.cache:1: warning: Unmatched quotes are deprecated.


This is normal, just ignore these warnings.



but I have no such folder. I have also searched my computer for occurrences of these files, with no luck hehe. Where do I go from here?


In theory, after building the library, you must install it by typing:


$make install (if you use MinGW compiler)
or
$nmake install (if yuo use VS compiler)


But in reality it until does not work (incomplete), so to install the library, do the following:

!!!ONLY FOR Qt4.x!!!

1. Build library any way (or through QtCreator, or from the console) using serialport.pro as release and debug.
2. Copy folder /QtAddOnSerialPort (with all content) from library build directory to the Qt4 installation headers folder /include.
3. Copy file "serialport.prf" from source library folder /src/qt4support/ to the Qt4 installation folder /mkspecs/features/
4. Copy all builded libraries (as release ans as debug) SerialPort1.dll, SerialPortd1.dll, SerialPort1.lib (or *.a), SerialPortd1.lib (or *.a)
to the Qt4 installation library folder /lib

Now you can create a custom project that link library as follows:

Your *.pro file:


...
CONFIG += serialport
...


Your *.cpp file, for example:


#include <QCoreApplication>

#include <QtAddOnSerialPort/serialportinfo.h>
// and/or
#include <QtAddOnSerialPort/serialport.h>

#include <QDebug>

QT_USE_NAMESPACE_SERIALPORT

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

QList<SerialPortInfo> list = SerialPortInfo::availablePorts();

foreach (const SerialPortInfo &info, list) {
QString s = info.portName();
qDebug() << s;
}

return a.exec();
}


Enjoy.... :)

HSPalm
24th August 2012, 09:18
I AM SO SORRY, I thought this thread died a sudden death and I was discouraged to start a new one. Now that I've got my motivation back for re-posting I see all these wonderful answer which I will try out ASAP. Thanks guys!

HSPalm
24th August 2012, 13:26
YOU ARE MY HEROES

Thanks to both for enlightening me, I know understand better libraries and linking, and got help where I was stuck after that.

edit: there's a typo in there...

ilham
11th November 2012, 17:05
2. Copy folder /QtAddOnSerialPort (with all content) from library build directory to the Qt4 installation headers folder /include.


I have build the library through QtCreator 2.6
I couldn't find /QtAddOnSerialPort in the library build directory.

Where is it? Help me, please. :(

kuzulis
13th November 2012, 06:55
Please read Wiki (http://qt-project.org/wiki/QtSerialPort)

radix07
20th February 2013, 16:14
I have to agree ilham, there is no folder /QtAddOnSerialPort to copy to the Qt 4.8 installation after compilation.

The wiki doesn't address this.

I can get everything to compile and include correctly, but without the QT_USE_NAMESPACE_SERIALPORT I can't use this library.

Trying to incorporate this onto an ARM processor running linux, but did not have similar troubles using this library in Windows.

DEHiCKA
1st March 2013, 20:15
I have to agree ilham, there is no folder /QtAddOnSerialPort to copy to the Qt 4.8 installation after compilation.

It's QtSerialPort now

kuzulis
2nd March 2013, 16:57
Yes, now made a lot of changes before stabilizing and public in Qt 5.1.

So, just follow what is written in the Wiki, but not too close, because everything can change. :)

emueyes
26th April 2013, 07:46
I too am having problems with QtAddOnSerialPort. I'm trying to compile qSerialTerm, and am getting an error "mainwindow.h:26:42: fatal error: QtAddOnSerialPort/serialport.h: No such file or directory". And similarly, cannot find a QtAddOnSerialPort tree, or any mention of it, in the qtserialport code. It (qtserialport) seemed to make ok, though. I'm trying to build qSerialTerm so I can use it with the Bluetooth project I have going, so have been side-tracked a little by this.

The qSerialTerm Wiki, which I've been following to build it, hasn't helped here so I thought I'd google around for some people who look like they know what they're doing :)

UPDATE: I changed the files in qSerialTerm to use headers in the /usr/include/QtSerialPort instead if looking for them in QtAddOnSerialPort. That fixed the issue with qserialport.h and qserialportinfo.h, but now the build needs serialportwidget.h, which does not appear anywhere.

kuzulis
26th April 2013, 08:34
QtSerialPort API changed and was more or less stabilized since was this theme is written.

The freshest and actual invormation look in WIKI: http://qt-project.org/wiki/QtSerialPort

Now QtAddOnSerialPort isn't present, and also namespace is now QT_USE_NAMESPACE. Look examples to library in /examples in more detail.

pola
21st November 2013, 16:50
Hi guys,

I'm developing on an Arm board, and I have to use a RS485 interface. I'm trying to use QtSerialPort: I added libQtSerialPort.so to my qt/lib folder on the board, but I still have problem on running my test on it.

Compilation for both target and host runs well, and I also can run my simple test on the host linux pc. As I try to run in debug mode on the board, I get a "Could not load shared library symbols for 14 libraries, e.g. /usr/local/qt/lib/libQtSerialPort.so.1", even if this file exists in /usr/local/qt/lib.

my .pro file is

//////////////////////////////////////////////////////////////////////////////////////////
QT += core

QT -= gui

TARGET = QtRs485Test
target.files = QtRs485Test
target.path = /disk

INSTALLS += target
CONFIG += console
CONFIG += serialport
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp
////////////////////////////////////////////////////////////

and my .cpp file is

#include <QCoreApplication>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>

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

foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
qDebug() << "Name :" << info.portName();
qDebug() << "Description :" << info.description();
qDebug() << "Manufacturer:" << info.manufacturer();

QSerialPort serialPort;
serialPort.setPort(info);
if(serialPort.open(QIODevice::ReadWrite)){
QString s = "hello from " + info.portName();
serialPort.write(s.toUtf8());
serialPort.close();
}
}

return a.exec();
}

Thank you in advance

P

ChrisW67
22nd November 2013, 03:33
Is that file a symbolic link to another that does not exist?
What does:


ldd ./yourexecutable

tell you on the target machine?

dieven
26th June 2014, 16:16
Thank you! I had gotten as far as installing the headers and libraries, but the example .pro files have a step to include serialport.prf from $$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf. $$QTSERIALPORT_PROJECT_ROOT is an empty string on my machine.

Copying serialport.prf to /mkspecs/features was the missing piece for me. And use CONFIG += serialport rather than QT += serialport. Using Qt 4.8.6 here, and I have QT += widgets, but must use CONFIG += serialport.

Thanks again!