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;
}
Q_OBJECT
public:
~CAN_transfer();
protected:
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
#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
To copy to clipboard, switch view to plain text mode
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
) : 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->setTextColor(Qt::black);
_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
) {
switch (e->type()) {
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...."));
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks