Hi no problem,
this is my thread-class:
#ifndef THREADS_H
#define THREADS_H
#include <QThread>
class serial; //forward decl.
{
Q_OBJECT
public:
threads
(QObject *parent,
bool);
//here i push the this-pointer of my serial class, so my Threads can call serial-class methods ~threads();
threads(threads &);
void stopAll(); // to stop my threads static var is set
protected:
void run();
private:
bool readerFlagIsSet,writerFlagIsSet; //ctor will set what kind of thread it is
static bool threadDone;
serial *serialPtr; // pointer to serial class object
};
#endif // THREADS_H
#ifndef THREADS_H
#define THREADS_H
#include <QThread>
class serial; //forward decl.
class threads : public QThread
{
Q_OBJECT
public:
threads(QObject *parent,bool); //here i push the this-pointer of my serial class, so my Threads can call serial-class methods
~threads();
threads(threads &);
void stopAll(); // to stop my threads static var is set
protected:
void run();
private:
bool readerFlagIsSet,writerFlagIsSet; //ctor will set what kind of thread it is
static bool threadDone;
serial *serialPtr; // pointer to serial class object
};
#endif // THREADS_H
To copy to clipboard, switch view to plain text mode
this is my serial class:
#ifndef SERIAL_H
#define SERIAL_H
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QList>
#include <QMutex>
#include "define.h"
#include "storage.h"
#include <windows.h>
#include "threads.h" // my thread class (above)
#include "Serial\Serial.h" // this is the foreign serial lib I use
{
Q_OBJECT
public:
~serial();
serial(serial&);
bool closeserialport();
QList<QString> scanSerialPorts();
void ResetEvents();
//threads fct
bool read(); //called by reader-thread in its loop.
//inside the read fct it waits for com events (WaitForMultipleObjects (Win32)) and blocks until data is there
bool isOpen();
bool isWriteJobAvailable(); //called by writer thread, to check if there is data in outgoing queue (to write)
void writeNewJob();// called by writer thread, to get data from queue and call write(QByteArray)
bool write
(QByteArray);
//called indirectly by writer thread to write data on device
signals:
void dataIN(); //emit signal when data is read in reader-thread, to inform another thread
private:
CSerial cserial; //foreign serial lib I use to access COM port
threads *readerThread,*writerThread; // pointer of type Threads
bool fContinue;
bool portIsOpen;
};
#endif // SERIAL_H
#ifndef SERIAL_H
#define SERIAL_H
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QList>
#include <QMutex>
#include "define.h"
#include "storage.h"
#include <windows.h>
#include "threads.h" // my thread class (above)
#include "Serial\Serial.h" // this is the foreign serial lib I use
class serial : public QObject
{
Q_OBJECT
public:
serial(QObject *parent);
~serial();
serial(serial&);
bool openserialport(QString);
bool closeserialport();
QList<QString> scanSerialPorts();
void ResetEvents();
//threads fct
bool read(); //called by reader-thread in its loop.
//inside the read fct it waits for com events (WaitForMultipleObjects (Win32)) and blocks until data is there
bool isOpen();
bool isWriteJobAvailable(); //called by writer thread, to check if there is data in outgoing queue (to write)
void writeNewJob();// called by writer thread, to get data from queue and call write(QByteArray)
bool write(QByteArray); //called indirectly by writer thread to write data on device
signals:
void dataIN(); //emit signal when data is read in reader-thread, to inform another thread
private:
CSerial cserial; //foreign serial lib I use to access COM port
threads *readerThread,*writerThread; // pointer of type Threads
QByteArray *readBuf;
bool fContinue;
bool portIsOpen;
};
#endif // SERIAL_H
To copy to clipboard, switch view to plain text mode
In my ctor of my serial class i create two instances, one for readerThread and one for writerThread and start the Threads.
Ctor of Serial Class:
reader = new threads(this,true);
writer = new threads(this,false);
reader->start();
writer->start();
reader = new threads(this,true);
writer = new threads(this,false);
reader->start();
writer->start();
To copy to clipboard, switch view to plain text mode
run() of Threads:
void threads::run()
{
if(isReaderThread)
while(!threadDone){ //static var which will be set to stop the threads
serialPtr->read(); //serial class fct called with the pointer (got it inside from ctor)
msleep(50);
}
if(isWriterThread)
while(!threadDone){
if(serialPtr->isWriteJobAvailable()) //serial class fct called with the pointer (got it inside from ctor)
serialPtr->writeNewJob();
msleep(50);
}
}
void threads::run()
{
if(isReaderThread)
while(!threadDone){ //static var which will be set to stop the threads
serialPtr->read(); //serial class fct called with the pointer (got it inside from ctor)
msleep(50);
}
if(isWriterThread)
while(!threadDone){
if(serialPtr->isWriteJobAvailable()) //serial class fct called with the pointer (got it inside from ctor)
serialPtr->writeNewJob();
msleep(50);
}
}
To copy to clipboard, switch view to plain text mode
Thanks!
Bookmarks