PDA

View Full Version : Problems with Threads connect



Max123
28th March 2010, 20:45
Hi,

I have created a Thread class:

#ifndef SERIALTHREAD_H
#define SERIALTHREAD_H

#include <QThread>
#include <qDebug>
#include "qextserialport.h"
class SerialThread : public QThread
{
Q_OBJECT
public:
SerialThread(QString name) {
portName=name;
}

void run();


private:
QString portName;
QextSerialPort *port;

private slots:
void onReadyRead();

};

#endif // SERIALTHREAD_H


#include "SerialThread.h"

void SerialThread::run() {
port = new QextSerialPort(portName, QextSerialPort::EventDriven);
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadWrite);
exec();
}

void SerialThread::onReadyRead()
{
QByteArray bytes;
int a = port->bytesAvailable();
bytes.resize(a);
port->read(bytes.data(), bytes.size());
qDebug() << "bytes read:" << bytes.size();
qDebug() << "bytes:" << bytes;
}



In the MainWindow I call this Thread:

MainWindow::MainWindow() {
setupUi(this);
t1 = new SerialThread("/dev/cu.usbserial-ftDIHUS6");
t1->start();
}

it seems to be a problem with the signals. When I send:
u I get:

bytes read: 1
bytes: "u"
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""
bytes read: 0
bytes: ""


Without Threads it works so what's the problem?

^NyAw^
29th March 2010, 09:05
Hi,

Why do you want to use QextSerialPort inside a thread if you are using it as EventDriven?

Max123
29th March 2010, 09:44
It was only I try. Later I wanted to use a protocol and some heavy calculations. But it shouldn't be a problem, when a thread have it's one event loop.

^NyAw^
29th March 2010, 12:20
Hi,

Think on that QextSerialPort 1.2 is an Alpha version (not a beta and not a release) so it may contain a lot of errors(the creator have closed the development). Try checking the bytes obtained on "bytesAvailable" and if it is 0 just ignore it.