Hello everyone
I've encountered a problem while trying to implement a polling function to fetch data from a serial port.
Qt Version is 4.3.4 and I'm using QDevelop as IDE. When I hit compile, I get a debug and a release version.
At first, I tried to set up a timer and connect its signal to the polling-method. This did work, but only in the release version. It doesn't work in the debug version nor in a static version. This seems to indicate that I created some kind of half-working solution which I just hate.
So I started reading and searching for this problem on the internet and found that setting up a timer only works when it is inside a eventloop. Since my polling class is connected to the mainWindow-class via pointer, I figured this could be the problem (Is it?).
Therefore I created a QThread-inheritance(TimerThread) to contain the polling timer. This worked eventually, but still only in the release-version of my project.
Hmpf. There still seems to be something about these signals/slots I just don't get.
My goal is having a class that handles one serial port via qextserialport and fetches data from it on a fixed timetick completely on its own. Occasionally, it gets called and returns a chunk of data. And it should be able to be included everywhere by setting up a pointer with the type of this class and construct an object on it.
This is my implementation of the thread-class whick inherits QThread:
#include "TimerThread.h"
//******************************************************************************
TimerThread::TimerThread(SerialPort *port)
{
this->port=port;
}
//******************************************************************************
void TimerThread::run()
{
timer->start(30);
connect(timer, SIGNAL(timeout()), port, SLOT(fetchData()));
exec();
}
//******************************************************************************
#include "TimerThread.h"
//******************************************************************************
TimerThread::TimerThread(SerialPort *port)
{
this->port=port;
}
//******************************************************************************
void TimerThread::run()
{
timer=new QTimer(this);
timer->start(30);
connect(timer, SIGNAL(timeout()), port, SLOT(fetchData()));
exec();
}
//******************************************************************************
To copy to clipboard, switch view to plain text mode
So, the object of TimerThread knows the object it has been created by and uses this knowledge to connect the timer-signal to the method of its creator that collects data from the serial port.
This class-object gets created like that:
timerPort = new TimerThread(this);
timerPort = new TimerThread(this);
To copy to clipboard, switch view to plain text mode
Would really appreciate any help because I totally ran out of ideas.
Bookmarks