PDA

View Full Version : QSerialDevice Problem



waynew
16th January 2010, 02:31
This is probably due to my total inexperience with libraries.
Trying to include QSerialDevice as a static library.
Generated the library file libqserialdevice.a and put it in my project directory.
Added to my .pro



INCLUDEPATH += C:\cpp\qt_projects\qtlogger\include
LIBS += -L"C:\cpp\qt_projects\qtlogger\" -llibqserialdevice.a


Builds ok, but doesn't look like the .exe file got any bigger. That worried me.
Then create a class to use the library functions in - SerialComm

Adding to SerialComm:

class AbstractSerial; // from the library

Causes build error: forward declaration of 'struct AbstractSerial'

Obviously not the right way. So what is?

kuzulis
16th January 2010, 10:07
1. What function SerialComm?
2. What operating system?
3. And what do you want to do? (more detail the essence of the problem)

PS: try to use the library from SVN:
http://fireforge.net/snapshots.php?group_id=199

PPS: to LIBS in my opinion you need to insert the library name without extension, ie instead -llibqserialdevice.a need to write -lqserialdevice

waynew
16th January 2010, 14:36
Thanks for your reply Kuzulis. I have changed the pro file as you suggested and built the library from the svn. Same build problem.
The SerialComm class was added to my project as a place to put the serial read and write methods - a place to call the library functions from.
I just need to be able to read and write to a serial port on Win, *nix, or MAC OS.
I have included the initial code below - just copied your writer as a starting point.
OS is XP/SP3



INCLUDEPATH += C:\cpp\qt_projects\qtlogger\qserialdevice\src
LIBS += -L"C:\cpp\qt_projects\qtlogger\qserialdevice\src\rele ase"
LIBS += -llibqserialdevice





#ifndef SERIALCOMM_H
#define SERIALCOMM_H

#include <QObject>
#include <QByteArray>

#include "abstractserial.h"
#include "abstractserialengine.h"
#include "datatypes.h"
#include "nativeserialengine.h"
#include "winserialnotifier.h"

class SerialComm : public QObject
{
Q_OBJECT
public:
explicit SerialComm(QObject *parent = 0);
void printDataToHex(const QByteArray &data);
void writeComm(QByteArray);

signals:

public slots:

};

#endif // SERIALCOMM_H




#include "serialcomm.h"
#include <iostream>
#include <QString>
#include <QDebug>
#include <QByteArray>

#include "abstractserial.h"
#include "abstractserialengine.h"
#include "datatypes.h"
#include "nativeserialengine.h"
#include "winserialnotifier.h"

SerialComm::SerialComm(QObject *parent) :
QObject(parent)
{
}

void SerialComm::printDataToHex(const QByteArray &data)
{
QByteArray baTmp;
baTmp.clear();
#if QT_VERSION >= 0x040300
baTmp = (data.toHex()).toUpper();
#else
quint8 n=0;
for (int i=0;i<data.size();i++) {
n=data.at(i);
if ((n >= 0) && (n <= 15)) baTmp.append(QByteArray::number(0,16).toUpper());
baTmp.append(QByteArray::number(n,16).toUpper());
}
#endif

for (int i=0;i<baTmp.size();i+=2) {
qDebug() << "[" << baTmp.at(i) << baTmp.at(i+1) << "]";
}
}

void SerialComm::writeComm(QByteArray ba) {
AbstractSerial *MyDevice = new AbstractSerial();

//cout << "Please enter serial device name, specific by OS : ";
//char dn[50];
//cin >> dn;
QString dn = "COM3";
MyDevice->setDeviceName(dn);

/*!
\warning
\~english To annex "correctly" to work - need to open the device with the flag Unbuffered!
\~russian Чтобы прилоР¶ÃÂµÃÂ½ÃÂ¸ÃÂµ "корреà ÂºÃ‘‚но" работà Â°ÃÂ»ÃÂ¾ - необхР¾ÃÂ´ÃÂ¸ÃÂ¼ÃÂ¾ открц¹ÃÂ²ÃÂ°Ã‘‚ÑŒ устрРйство с флагоР¼ Unbuffered!
*/
if (MyDevice->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
qDebug() << "Serial device " << MyDevice->deviceName() << " open in " << MyDevice->openMode();
/*
MyDevice->setDataBits(AbstractSerial::DataBits6);
MyDevice->setParity(AbstractSerial::ParityMark);
*/
qDebug() << "= Defaults parameters =";
qDebug() << "Device name : " << MyDevice->deviceName();
qDebug() << "Baud rate : " << MyDevice->baudRate();
qDebug() << "Data bits : " << MyDevice->dataBits();
qDebug() << "Parity : " << MyDevice->parity();
qDebug() << "Stop bits : " << MyDevice->stopBits();
qDebug() << "Flow : " << MyDevice->flowControl();
qDebug() << "Char timeout, msec : " << MyDevice->charIntervalTimeout();

QByteArray ba;
qint64 bw=0;
while (1) {
qDebug() << "Please enter count bytes for wtitten : ";
bw=0;
//cin >> bw;
qDebug() << "Starting writting " << bw << " bytes in time : " << QTime::currentTime();
ba.clear();
ba.resize(bw);
for (int i=0;i<bw;i++) { //filling data array
ba[i] = i;
}
bw=MyDevice->write(ba);
qDebug() << "Writed is : " << bw << " bytes";
qDebug() << "Tx : ";
printDataToHex(ba);
}
}
else {
qDebug() << "Error opened serial device " << MyDevice->deviceName();
}
MyDevice->close();
qDebug() << "Serial device " << MyDevice->deviceName() << " is closed";
delete MyDevice;
MyDevice=0;
}



The library headers are visible from my gui application now, but it still won't build.
Just returns: :-1: error: collect2: ld returned 1 exit status

No clue as to why it won't build. Any ideas?

kuzulis
16th January 2010, 17:29
I looked at your example and say the following:
1. You have in your code is # include "datatypes.h". However, the SVN is no longer the file datatypes.h and therefore it is not necessary.
2. Just plug the only # include "abstractserial.h" (see examples in / examples)
3. Show your project file *. pro, because instead of LIBS + =-llibqserialdevice to write LIBS + =-lqserialdevice

PS: but the main idea - that you downloaded, try to compile the examples from the directory examples without changing them. If writer, reader, or sreader will bring together - then you're in your project properly connect the library ;)

waynew
16th January 2010, 18:19
Thanks Kuzulis, builds ok now.
I'll let you know how it goes.

drizzt
21st January 2010, 14:59
hi,

i'm want to use QSerialDevice. (examples working fine). So i copied some code from examples to my project. I want to read data from serial port and write incoming data to a QTextField.
not a big thing, but my application always freeze after few seconds :(

had anyone the same problem and solved it?

kuzulis
21st January 2010, 15:10
1. If the amount of data is small, then they can be read using the signal readyRead() (see the example /examples/sreader)
2. If a large amount of data - then use the thread

kuzulis
21st January 2010, 15:11
1. If the amount of data is small, then they can be read using the signal [b] readyRead() [/ b] (see the example /examples/sreader)
2. If a large amount of data - then use the thread

drizzt
21st January 2010, 15:31
very fast answer thx :)

i'll try and tell you if it runs or not...


edit:
now i can start my app 2 times and send messages over serial port :) thx