-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealdevice.cpp
95 lines (74 loc) · 2.32 KB
/
realdevice.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
91
92
93
94
95
#include "realdevice.h"
#include <QDebug>
#include <QMessageBox>
RealDevice::RealDevice(const int tNum, DeviceController &ctrl, QString devPort):
Device(tNum, ctrl)
{
isRealDevice = true;
_initPort(devPort);
}
void RealDevice::run() {
connect(_port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
while(_stopSig == false){
float oldDesiredTemp = _desiredTemp;
_desiredTemp = _ctrl.getTargetTemp();
if (oldDesiredTemp != _desiredTemp){
_transmitToDevice(_desiredTemp);
}
msleep(20);
}
qDebug() << "RealDevice" << _tNum << "terminated";
}
void RealDevice::stop() {
qDebug() << "RealDevice" << _tNum << "stopping...";
_stopSig = true;
}
void RealDevice::_initPort(QString devPort) {
_port = new QextSerialPort(devPort);
_port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
if(!_port->isOpen()) {
qDebug() << "Impossible d'ouvrir le port" << devPort;
}
_port->setBaudRate(BAUD9600);
_port->setFlowControl(FLOW_OFF);
_port->setParity(PAR_NONE);
_port->setDataBits(DATA_8);
_port->setStopBits(STOP_1);
}
void RealDevice::onReadyRead() {
_getFromDevice();
}
void RealDevice::_getFromDevice()
{
float oldTemp = _currentTemp;
bool oldHeating = _heating;
if(_port != NULL && _port->isOpen()){
qDebug() << "Byte available: " << _port->bytesAvailable();
// On ne charge pas si le buffer n'est pas complet
if (_port->bytesAvailable() == 3){
char* byte = new char[_port->bytesAvailable()];
_port->read(byte, _port->bytesAvailable());
_currentTemp = (int) byte[1];
_heating = (int) byte[0] == 2;
qDebug() << "Value read:" << (int) byte[0] << "";
qDebug() << "Target temp:" << (int) byte[2] << "";
}
_port->flush(); // on attend la fin de la transmission
}
if (oldTemp != _currentTemp){
_ctrl.setRealTemp(_tNum, _currentTemp);
}
if (oldHeating != _heating){
_ctrl.heating(_tNum, _heating);
}
}
void RealDevice::_transmitToDevice(int value)
{
char* byte = new char[1];
byte[0] = value;
if(_port != NULL){
qint64 bw = _port->write(byte);
qDebug() << bw << "byte(s) written | Value sent:" << value << "(dec)";
_port->flush();
}
}