-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdltlultimeter.cpp
276 lines (233 loc) · 6.69 KB
/
dltlultimeter.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* @licence app begin@
* Copyright (C) 2021 Alexander Wenzel
*
* This file is part of the DLT Multimeter project.
*
* \copyright This code is licensed under GPLv3.
*
* \author Alexander Wenzel <[email protected]>
*
* \file dltmultimeter.cpp
* @licence end@
*/
#include "dltmultimeter.h"
#include <QDebug>
#include <QFile>
DLTMultimeter::DLTMultimeter(QObject *parent) : QObject(parent)
{
clearSettings();
value = 0;
lastValue = -1;
}
DLTMultimeter::~DLTMultimeter()
{
stop();
}
void DLTMultimeter::start()
{
value = 0;
lastValue = -1;
// set serial port parameters
serialPort.setBaudRate(QSerialPort::Baud2400);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setParity(QSerialPort::NoParity);
serialPort.setStopBits(QSerialPort::OneStop);
serialPort.setPortName(interface);
// open serial port
if(serialPort.open(QIODevice::ReadWrite)==true)
{
// open with success
// connect slot to receive data from serial port
connect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
status(QString("started"));
qDebug() << "DLTMultimeter: started" << interface;
}
else
{
// open failed
qDebug() << "DLTMultimeter: Failed to open interface" << interface;
status(QString("error"));
}
// connect slot watchdog timer and start watchdog timer
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
timer.start(5000);
watchDogCounter = 0;
watchDogCounterLast = 0;
}
void DLTMultimeter::stop()
{
// stop communication
status(QString("stopped"));
qDebug() << "DLTMultimeter: stopped" << interface;
// close serial port, if it is open
if(serialPort.isOpen())
{
serialPort.close();
// disconnect slot to receive data from serial port
disconnect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
// stop watchdog timer
timer.stop();
disconnect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
void DLTMultimeter::readyRead()
{
QByteArray data = serialPort.readAll();
for(int num=0;num<data.length();num++)
{
switch(data[num]&0xf0)
{
case 0x10:
rawData.clear();
rawData+=data[num];
break;
case 0xe0:
data.clear();
rawData+=data[num];
qDebug() << "DLTMultimeter: Raw Data " << rawData.toHex();
calculateValue();
if(lastValue!=value)
{
valueMultimeter(QString("%1").arg(value),unit);
lastValue = value;
}
watchDogCounter++;
qDebug() << "DLTMultimeter: Value received" << interface << value << unit;
break;
default:
rawData+=data[num];
break;
}
}
}
void DLTMultimeter::timeout()
{
// watchdog timeout
// check if watchdog was triggered between last call
if(watchDogCounter!=watchDogCounterLast)
{
watchDogCounterLast = watchDogCounter;
status(QString("started"));
}
else
{
// no watchdog was received
qDebug() << "DLTMultimeter: Watchdog expired try to reconnect" ;
// if serial port is open close serial port
if(serialPort.isOpen())
{
serialPort.close();
disconnect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
// try to reopen serial port
if(serialPort.open(QIODevice::ReadWrite)==true)
{
// retry was succesful
// connect slot to receive data from serial port
connect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
status(QString("reconnect"));
qDebug() << "DLTMultimeter: reconnect" << interface;
}
else
{
// retry failed
qDebug() << "DLTMultimeter: Failed to open interface" << interface;
status(QString("error"));
}
}
}
void DLTMultimeter::clearSettings()
{
}
void DLTMultimeter::writeSettings(QXmlStreamWriter &xml)
{
/* Write project settings */
xml.writeStartElement("DLTMultimeter");
xml.writeTextElement("interface",interface);
xml.writeEndElement(); // DLTMultimeter
}
void DLTMultimeter::readSettings(const QString &filename)
{
bool isDLTMultimeter = false;
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
return;
QXmlStreamReader xml(&file);
while (!xml.atEnd())
{
xml.readNext();
if(xml.isStartElement())
{
if(isDLTMultimeter)
{
/* Project settings */
if(xml.name() == QString("interface"))
{
interface = xml.readElementText();
}
}
else if(xml.name() == QString("DLTMultimeter"))
{
isDLTMultimeter = true;
}
}
else if(xml.isEndElement())
{
/* Connection, plugin and filter */
if(xml.name() == QString("DLTMultimeter"))
{
isDLTMultimeter = false;
}
}
}
if (xml.hasError())
{
qDebug() << "Error in processing filter file" << filename << xml.errorString();
}
file.close();
}
void DLTMultimeter::calculateValue()
{
if(rawData.length()!=14)
return;
value=calculateNumber(rawData[1]&0x7,rawData[2]&0xf);
value=value*10+calculateNumber(rawData[3]&0x7,rawData[4]&0xf);
value=value*10+calculateNumber(rawData[5]&0x7,rawData[6]&0xf);
value=value*10+calculateNumber(rawData[7]&0x7,rawData[8]&0xf);
if(rawData[1]&0x08) value*=-1;
if(rawData[3]&0x08) value/=1000;
if(rawData[5]&0x08) value/=100;
if(rawData[7]&0x08) value/=10;
if(rawData[10]&0x08) value/=1000; // milli
if(rawData[9]&0x08) value/=1000; // mycro
if(rawData[12]&0x08)
unit = "A";
else if(rawData[12]&0x04)
unit = "V";
else
unit = "unknown";
}
int DLTMultimeter::calculateNumber(unsigned char a,unsigned char b)
{
if(a==0x7 && b==0x0d)
return 0;
else if(a==0x0 && b==0x05)
return 1;
else if(a==0x5 && b==0x0b)
return 2;
else if(a==0x1 && b==0x0f)
return 3;
else if(a==0x2 && b==0x07)
return 4;
else if(a==0x3 && b==0x0e)
return 5;
else if(a==0x7 && b==0x0e)
return 6;
else if(a==0x1 && b==0x05)
return 7;
else if(a==0x7 && b==0x0f)
return 8;
else if(a==0x3 && b==0x0f)
return 9;
}