/
connectionthread.cpp
90 lines (84 loc) · 2.33 KB
/
connectionthread.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "connectionthread.h"
ConnectionThread::ConnectionThread(QObject *parent)
: QThread(parent), waitTimeout(0), quit(false), test(nullptr)
{
}
ConnectionThread::~ConnectionThread()
{
mutex.lock();
quit = true;
mutex.unlock();
wait();
}
void ConnectionThread::startSlave(const QString &portName, int waitTimeout, Test *test, bool stepByStep)
{
if (!isRunning())
{
this->portName = portName;
this->waitTimeout = waitTimeout;
this->test = test;
this->stepByStep = stepByStep;
start();
}
}
void ConnectionThread::run()
{
if(stepByStep)
sendNextCommand = false;
else
sendNextCommand = true;
QSerialPort serial;
serial.setPortName(portName);
if (!serial.open(QIODevice::ReadWrite))
{
emit error(tr("Can't open %1, error code %2").arg(portName).arg(serial.error()));
return;
}
while (!quit) {
if(stepByStep)
{
while(!sendNextCommand);
mutex.lock();
sendNextCommand = false;
mutex.unlock();
}
// Envoi microcommande
serial.write(test->recupProchaineMicrocommande().c_str());
if(!serial.waitForBytesWritten(waitTimeout))
{
emit timeout(tr("Wait write microcommande timeout %1").arg(QTime::currentTime().toString()));
}
//Accusé de reception
if(serial.waitForReadyRead(waitTimeout))
{
QByteArray received = serial.readAll();
if(received.toStdString().compare(COMMANDE_RECEIVED) != 0)
{
emit error("Not wanted response");
}
}
else
{
emit timeout(tr("Wait read response timeout %1").arg(QTime::currentTime().toString()));
}
//Pret a recevoir prochaine commande
if(serial.waitForReadyRead(waitTimeout))
{
QByteArray received = serial.readAll();
if(received.toStdString().compare(READY_FOR_NEXT_COMMAND) != 0)
{
emit error("Not wanted response");
}
}
else
{
emit timeout(tr("Wait read response timeout %1").arg(QTime::currentTime().toString()));
}
}
}
void ConnectionThread::sendCommand()
{
mutex.lock();
sendNextCommand = true;
mutex.unlock();
}