PDA

View Full Version : Question about using of static QTimer::singleShot() function



another_one
31st January 2010, 10:48
Hello!

I'm trying to use the static QTimer::singleShot() function to create delay between write and read cycles via serial interface.

I have done the following code:

Hedaer:


#ifndef CAN_TRANSFER_H
#define CAN_TRANSFER_H

#include <QMainWindow>
#include "qextserialport.h"
#include <Qtimer.h>


namespace Ui {
class CAN_transfer;

}

class CAN_transfer : public QMainWindow {
Q_OBJECT

public:
CAN_transfer(QWidget *parent = 0);
~CAN_transfer();


protected:
void changeEvent(QEvent *e);

private:
Ui::CAN_transfer *ui;
QextSerialPort* _comPort;
unsigned char mas[64];
unsigned char* DATA_pointer;

private slots:

void readBR(unsigned char t);
void on_store_all_button_clicked();
void on_store_sel_button_clicked();
void on_connectbutton_clicked();
void timer_event();
};

#endif // CAN_TRANSFER_H


Source:



#include "can_transfer.h"
#include "ui_can_transfer.h"
#include "qextserialport.h"
#include "win_qextserialport.h"
//#include <Qtimer.h>
#include <QTimer>
unsigned char clkdt,te;
CAN_transfer::CAN_transfer(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CAN_transfer)
{
ui->setupUi(this);
ui->logw->setReadOnly(true);
ui->logw->setFontWeight(QFont::Bold);
ui->logw->append(tr("CAN transfer configurator is ready to use!"));
ui->logw->setFontWeight(QFont::QFont::Normal);
ui->logw->setTextColor(Qt::black);
QString s="COM1";
_comPort = new QextSerialPort(s);
_comPort->setBaudRate(BAUD9600);
_comPort->setFlowControl(FLOW_OFF);
_comPort->setParity(PAR_NONE);
_comPort->setDataBits(DATA_8);
_comPort->setStopBits(STOP_1);
DATA_pointer = &mas[0];
clkdt = 0;
}

CAN_transfer::~CAN_transfer()
{
_comPort->close();
delete(_comPort);
delete ui;
}

void CAN_transfer::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


void CAN_transfer :: timer_event()
{
te = 0;
timer->stop();
}
void CAN_transfer::on_connectbutton_clicked()
{unsigned char rb=0;
ui->logw->append(tr("COM1 configurating...."));
_comPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
ui->logw->append(tr("conneting...."));
*DATA_pointer = 0x55;
if(_comPort->write((const char*)DATA_pointer, 1))
{

te = 1;
QTimer :: singleShot(100, this, SLOT(timer_event()));
while(te);
rb = _comPort->bytesAvailable();
if(rb)
{
_comPort->read((char*)DATA_pointer,rb);
if(*DATA_pointer==0xAA)
ui->logw->append(tr("connection established!"));
}
else
{
ui->logw->append(tr("no connection"));
}

}

statusBar()->showMessage(tr("Connected"));
//_comPort->close();
}







The problem is in that what application do not affect the SLOT(event_timer) code, at least it seems to be.

Compiler don not issue any warning or errors and I casue of that I don't understand in what the problem is.

I need some help to solve this problem, I would glad of any information about it.


Thanks in advance

franz
31st January 2010, 15:07
Why not use the QThread::msleep() function for that?

Lesiok
31st January 2010, 16:38
Problem is with line 65. With this construction You disable event dispatcher. Yours loop must be like this :
while(te)
QCoreApplication::processEvents();

another_one
31st January 2010, 16:56
Problem is with line 65. With this construction You disable event dispatcher. Yours loop must be like this :
while(te)
QCoreApplication::processEvents();

Thank You very much, finally it works!