PDA

View Full Version : SerialProgramming qextserialport or QSerialDevice?



mischter-x
24th October 2011, 10:51
Hello,

I just started programming QT4 on an Windows 7 64bit OS and now I want to use the SerialPort.
I've already read a lot Thread in several forums, and I found the QextSerialPort and the QSerialDevice.
Would QSerialDevice be better, because QextSerialPort is not longer developed?

I downloaded both an tryed to compiled them. Compiling QextSerialPort gives me a libqextserialport.a and a qextserialport.dll, but do I have to do with these files?

Compiling QSerialPort gives me a few errors. Here is the Build Log:


Führe Build-Schritte für Projekt BuildLibrary aus... //Translation: Building Project BuildLibrary
Unveränderte Konfiguration, qmake-Schritt wird übersprungen. //Translation: Config not changed, skipping qmake
Starte "C:\QtSDK\mingw\bin\mingw32-make.exe"
cd src/ && C:/QtSDK/mingw/bin/mingw32-make -f Makefile
mingw32-make[1]: Entering directory `c:/Users/Philipp/Documents/Qt/QSerialPort/BuildLibrary-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release/src'
c:/QtSDK/mingw/bin/mingw32-make -f Makefile.Release
mingw32-make[2]: Entering directory `c:/Users/Philipp/Documents/Qt/QSerialPort/BuildLibrary-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release/src'
g++ -c -O2 -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/QtCore' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include' -I'../../qserialdevice-qserialdevice/src/qserialdevice' -I'../../qserialdevice-qserialdevice/src/qserialdeviceenumerator' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/include/ActiveQt' -I'build/moc' -I'../../qserialdevice-qserialdevice/src' -I'.' -I'c:/QtSDK/Desktop/Qt/4.7.4/mingw/mkspecs/win32-g++' -o build/obj/abstractserialnotifier.o ../../qserialdevice-qserialdevice/src/qserialdevice/abstractserialnotifier.cpp
In file included from ../../qserialdevice-qserialdevice/src/qserialdevice/abstractserialnotifier.cpp:2:
../../qserialdevice-qserialdevice/src/qserialdevice/nativeserialnotifier.h:8:50: error: QtCore/private/qwineventnotifier_p.h: No such file or directory
In file included from ../../qserialdevice-qserialdevice/src/qserialdevice/abstractserialnotifier.cpp:2:
../../qserialdevice-qserialdevice/src/qserialdevice/nativeserialnotifier.h:18: error: expected class-name before ',' token
../../qserialdevice-qserialdevice/src/qserialdevice/nativeserialnotifier.h:37: error: 'OVERLAPPED' in namespace '::' does not name a type
../../qserialdevice-qserialdevice/src/qserialdevice/nativeserialnotifier.h:38: error: 'DWORD' in namespace '::' does not name a type
../../qserialdevice-qserialdevice/src/qserialdevice/nativeserialnotifier.h:39: error: 'DWORD' in namespace '::' does not name a type
mingw32-make[2]: Leaving directory `c:/Users/Philipp/Documents/Qt/QSerialPort/BuildLibrary-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release/src'
mingw32-make[1]: Leaving directory `c:/Users/Philipp/Documents/Qt/QSerialPort/BuildLibrary-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release/src'
mingw32-make[2]: *** [build/obj/abstractserialnotifier.o] Error 1
mingw32-make[1]: *** [release] Error 2
mingw32-make: *** [sub-src-make_default] Error 2
Der Prozess "C:\QtSDK\mingw\bin\mingw32-make.exe" wurde mit dem Rückgabewert 2 beendet. //Returns 2
Fehler beim Erstellen des Projekts BuildLibrary(Ziel: Desktop) //Error while compiling Project BuildLibrary (Target: Desktop)
Bei der Ausführung von Build-Schritt 'Make' //At builing step 'Make'


What could be wrong?

Greeting
Philipp

kuzulis
24th October 2011, 11:36
error: QtCore/private/qwineventnotifier_p.h: No such file or directory
Search in Google (or in this forum) will save the newbies!

UPD: take while sources here (https://gitorious.org/qserialdevice/qserialdevice/archive-tarball/master)

mischter-x
24th October 2011, 12:17
Ok thank you, it's working now, I read this:
http://code.google.com/p/qextserialport/issues/detail?id=91

But where do I have to copie the files now? I want to make the lib available for all my projects.
I have now a libqserialdevice.a, 4 cpp files in the moc folder and the object files.

Greetings
Philipp

kuzulis
24th October 2011, 12:56
Plug directly over *.pri, without building the libraries, see the examples /test/guiapp.
So it will be easier.

Or, if you build the library, you need:
abstractserial.h
serialdeviceenumerator.h
qserialdevice_global.h
libqserialdevice.a(dll)

Copy these files to any location of your project
such as headers in /include, library in /lib and in *. pro project file write these paths.

To not take qserialdevice_global.h - copy its contents to abstractserial.h and serialdeviceenumerator.h

mischter-x
25th October 2011, 13:47
Hello,

thank you, it's working now.
I wrote a smal Test app, and I you copied the code from one QSerialDevice Test app.
But the programm crashes everytime I start it, because theres something wrong. The debugger shows me the qmap.h file in line 813:

template <class Key, class T>
Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const
{
QList<Key> res;
res.reserve(size());
const_iterator i = begin();
while (i != end()) {
res.append(i.key());
++i;
}
return res;
}

Here is my code:
main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "serialdeviceenumerator.h"
#include "abstractserial.h"

class AbstractSerial;
class SerialDeviceEnumerator;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
SerialDeviceEnumerator *enumerator;
AbstractSerial *serial;

//Inits
//void initEnumerator();
//void deinitEnumerator();
//void initSerial();

public slots:
void procEnumerate(const QStringList &l);
//void procSerialMessages(const QString &msg, QDateTime dt);
//void procSerialDataReceive();

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//Enumerator
if(!this->enumerator)
this->enumerator = SerialDeviceEnumerator::instance();
connect(this->enumerator,SIGNAL(hasChanged(QStringList)),this,SL OT(procEnumerate(QStringList)));
this->procEnumerate(this->enumerator->devicesAvailable());
}

MainWindow::~MainWindow()
{
delete ui;
}




//Slots
void MainWindow::procEnumerate(const QStringList &l)
{
// Fill ports box.
ui->comboBox_Port->clear();
ui->comboBox_Port->addItems(l);
}

I think there is an fault with a QList, but I can't find my fault.

M
Greeting
Philipp

P.s. I forgot the .pro file:


QT += core gui

TARGET = Autorino_GUI_0_1_Beta
TEMPLATE = app

include(qserialdeviceenumerator/qserialdeviceenumerator.pri)
include(qserialdevice/qserialdevice.pri)


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

win32 {
LIBS += -lsetupapi -luuid -ladvapi32
}
unix:!macx {
LIBS += -ludev
}


Added after 1 23 minutes:

Sorry for doublepost, I tryed to safe this->enumerator->serialDevicesAvailable() in a QStringList, but there is also the same error.


//connect(this->enumerator,SIGNAL(hasChanged(QStringList)),this,SL OT(procEnumerate(QStringList)));
//this->procEnumerate(this->enumerator->devicesAvailable());
QStringList qsl = this->enumerator->devicesAvailable();


Greetings
Philipp

kuzulis
25th October 2011, 15:00
In constructor MainWindow
try remove:


...
//Enumerator
if(!this->enumerator)
...

mischter-x
27th October 2011, 19:46
Hello,

this prt is working now.
But I have a new problem now, with the function "this->serial->isOpen();"

This is my code:

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

initEnumerator();
initSerial();
initButtons();
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::initEnumerator()
{
this->enumerator = SerialDeviceEnumerator::instance();
connect(this->enumerator,SIGNAL(hasChanged(QStringList)),this,SL OT(procEnumerate(QStringList)));
this->procEnumerate(this->enumerator->devicesAvailable());
}

void MainWindow::initSerial()
{
ui->plainTextEdit_ComMessage->setVisible(false);
if (this->serial)
return;
this->serial = new AbstractSerial(this);
connect(this->serial, SIGNAL(signalStatus(QString,QDateTime)), this, SLOT(procSerialMessages(QString,QDateTime)));
connect(this->serial, SIGNAL(readyRead()), this, SLOT(procSerialDataReceive()));

// Enable emmiting signal signalStatus
this->serial->enableEmitStatus(true);

ui->plainTextEdit_ComMessage->setVisible(false);
}

void MainWindow::initButtons()
{
connect(ui->pushButton_connect,SIGNAL(clicked()),this,SLOT(pro cPushButtonConnect()));
}


//Slots
void MainWindow::procEnumerate(const QStringList &l)
{
// Fill ports box.
ui->comboBox_Port->clear();
ui->comboBox_Port->addItems(l);
}

void MainWindow::procSerialMessages(const QString &msg, QDateTime dt)
{
QString s = dt.time().toString() + " > " + msg;
ui->plainTextEdit_ComMessage->appendPlainText(s);
}

void MainWindow::procSerialDataReceive()
{
if (this->serial && this->serial->isOpen()) {

}
}

void MainWindow::procPushButtonConnect()
{
if(this->serial)
{
bool result = this->serial->isOpen();
if(result)
{
this->serial->close();
ui->plainTextEdit_ComMessage->setVisible(true);
ui->plainTextEdit_ComMessage->setEnabled(true);
ui->pushButton_connect->setText("Connect");
}
else
{
this->serial->setDeviceName(ui->comboBox_Port->currentText());
result = this->serial->open(QIODevice::ReadWrite);
ui->plainTextEdit_ComMessage->setVisible(false);
ui->plainTextEdit_ComMessage->setEnabled(false);
ui->pushButton_connect->setText("Disconnect");
}
}
}

The Debugger points on this line "bool result = this->serial->isOpen();"
When I choose a Port, and click the button, I get this error message:

The process was stop, after getting a Signal from the OS.
Name of Signal: SIGSEGV
Meaning: Segmentation fault

Greetings
Philipp

kuzulis
28th October 2011, 06:19
This time you do that the same error as before.
You have to study C + +, so do not do such a banal and stupid mistakes.
These errors are not due to QSerialDevice.

PS:
I give you a hint: Always reset (try equal zero) pointers before using them!
ie in your case, in order not to change all the code, you simply set equal to zero pointers to the port and the enumerator in constructors.


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
enumerator(0), serial(0)
{
....
....
}


And take a closer look examples - there's all there.