1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
QString Protocole::transaction(QString commande) {
this->timerFini = false;
QFile logTrames("logTrames.txt");
logTrames.open(QFile::WriteOnly | QFile::Append);
logTrames.write(commande.toAscii());
emit(this->envoiTrame(commande));
this->flagEtatCom = ETAT_ATTENTE;
QTimer timerCommunication;
timerCommunication.start(this->timeout);
connect(timerCommunication,SIGNAL(timeout()),this,SLOT(timeoutCom()));
// On attend timeout ms de recevoir la réponse
while(this->flagEtatCom == ETAT_ATTENTE && !this->timerFini) {
QCoreApplication::processEvents();
}
// Si pas de réponse
if(this->timerFini) {
emit(this->erreurTransmission());
return NULL;
}
this->flagEtatCom = ETAT_REPOS;
// Si erreur de commande
if(this->trame.at(1)==NAK) {
emit(this->erreurCommande());
return NULL;
}
logTrames.write(trame.toAscii());
logTrames.close();
return this->trame;
}
// Slot de lecture d'une trame
void Protocole::lectureTrame(QString data) {
qDebug()<<"Trame reçue : " << data.toAscii();
this->trame = data;
this->flagEtatCom = ETAT_LECTURE;
}
// Slot appelé lorsque le timer de communication est fini
void Protocole::timeoutCom() {
this->timerFini = true;
} |