-
Notifications
You must be signed in to change notification settings - Fork 1
/
dltrelais.cpp
388 lines (330 loc) · 10.8 KB
/
dltrelais.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* @licence app begin@
* Copyright (C) 2021 Alexander Wenzel
*
* This file is part of the DLT Relais project.
*
* \copyright This code is licensed under GPLv3.
*
* \author Alexander Wenzel <[email protected]>
*
* \file dltrelais.h
* @licence end@
*/
#include "dltrelais.h"
#include <QDebug>
#include <QFile>
#include <QSerialPortInfo>
DLTRelais::DLTRelais(QObject *parent) : QObject(parent)
{
clearSettings();
}
DLTRelais::~DLTRelais()
{
stop();
}
void DLTRelais::checkPortName()
{
/* check if information is stored, if not do not check */
if(interfaceSerialNumber.isEmpty() && interfaceProductIdentifier==0 && interfaceVendorIdentifier==0)
return;
/* check if port information still matches port name */
if((QSerialPortInfo(interface).serialNumber()!=interfaceSerialNumber ||
QSerialPortInfo(interface).productIdentifier()!=interfaceProductIdentifier ||
QSerialPortInfo(interface).vendorIdentifier()!=interfaceVendorIdentifier))
{
qDebug() << "Port" << interface << "not found anymore";
/* port name has changed, try to find new port name */
QList<QSerialPortInfo> availablePorts = QSerialPortInfo::availablePorts();
for(int num = 0; num<availablePorts.length();num++)
{
if(availablePorts[num].serialNumber()==interfaceSerialNumber &&
availablePorts[num].productIdentifier()==interfaceProductIdentifier &&
availablePorts[num].vendorIdentifier()==interfaceVendorIdentifier)
{
// The following is not working, if several interfaces have the same name
// Must be improved in future, e.g. with Serial Id of each device
// qDebug() << "Port name has changed from" << interface << "to" << availablePorts[num].portName();
// interface = availablePorts[num].portName();
}
}
}
}
void DLTRelais::start()
{
if(!active)
{
status(QString("not active"));
return;
}
// start communication
// checkPortName();
// set serial port parameters
serialPort.setBaudRate(QSerialPort::Baud115200);
serialPort.setDataBits(QSerialPort::Data8);
serialPort.setParity(QSerialPort::NoParity);
serialPort.setStopBits(QSerialPort::OneStop);
serialPort.setFlowControl(QSerialPort::NoFlowControl);
serialPort.setPortName(interface);
// open serial port
if(serialPort.open(QIODevice::ReadWrite)==true)
{
// open with success
// prevent flash mode of Wemos D1 mini and Wemos D1, not for Arduino boards
if(type==2) // Arduino Boards
serialPort.setDataTerminalReady(true);
else
serialPort.setDataTerminalReady(false);
// connect slot to receive data from serial port
connect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
status(QString("started"));
qDebug() << "DLTRelais: started" << interface;
}
else
{
// open failed
qDebug() << "DLTRelais: 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 DLTRelais::stop()
{
if(!active)
{
return;
}
// stop communication
status(QString("stopped"));
qDebug() << "DLTRelais: 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 DLTRelais::readyRead()
{
// data on serial port was received
// loop as long as data is available
while(serialPort.bytesAvailable())
{
char data[256];
// read one line form serial port
qint64 size = serialPort.readLine(data,sizeof(data));
if(size>0)
{
// line is not empty
//qDebug() << "DLTRelais: readLine" << data;
if(QString(data) == "WD\r\n")
{
// watchdog message received
watchDogCounter++;
}
else
{
// all other messages forward to status signal
status(QString(data));
}
}
}
}
void DLTRelais::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() << "DLTRelais: 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()));
}
// check if port name has changed
// checkPortName();
// try to reopen serial port
if(serialPort.open(QIODevice::ReadWrite)==true)
{
// retry was succesful
// prevent flash mode of Wemos D1 mini
serialPort.setDataTerminalReady(false);
// connect slot to receive data from serial port
connect(&serialPort, SIGNAL(readyRead()), this, SLOT(readyRead()));
status(QString("reconnect"));
qDebug() << "DLTRelais: reconnect" << interface;
}
else
{
// retry failed
qDebug() << "DLTRelais: Failed to open interface" << interface;
status(QString("error"));
}
}
}
int DLTRelais::getType() const
{
return type;
}
void DLTRelais::setType(int newType)
{
type = newType;
}
void DLTRelais::clearSettings()
{
// clear settings
type = 0;
for(int num=0;num<4;num++)
relaisName[num] = QString("Relais%1").arg(num+1);
active = 0;
interfaceSerialNumber = "";
interfaceProductIdentifier = 0;
interfaceVendorIdentifier = 0;
}
void DLTRelais::writeSettings(QXmlStreamWriter &xml,int num)
{
// Write project settings to XML file
xml.writeStartElement(QString("DLTRelais%1").arg(num));
xml.writeTextElement("relaisName1",relaisName[0]);
xml.writeTextElement("relaisName2",relaisName[1]);
xml.writeTextElement("relaisName3",relaisName[2]);
xml.writeTextElement("relaisName4",relaisName[3]);
xml.writeTextElement("interface",interface);
xml.writeTextElement("interfaceSerialNumber",QSerialPortInfo(interface).serialNumber());
xml.writeTextElement("interfaceProductIdentifier",QString("%1").arg(QSerialPortInfo(interface).productIdentifier()));
xml.writeTextElement("interfaceVendorIdentifier",QString("%1").arg(QSerialPortInfo(interface).vendorIdentifier()));
xml.writeTextElement("type",QString("%1").arg(type));
xml.writeTextElement("active",QString("%1").arg(active));
xml.writeEndElement(); // DLTRelais
}
void DLTRelais::readSettings(const QString &filename,int num)
{
// read settings from XML file
bool isDLTRelais = false;
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
return;
QXmlStreamReader xml(&file);
while (!xml.atEnd())
{
xml.readNext();
if(xml.isStartElement())
{
if(isDLTRelais)
{
/* Project settings */
if(xml.name() == QString("relaisName1"))
{
relaisName[0] = xml.readElementText();
}
else if(xml.name() == QString("relaisName2"))
{
relaisName[1] = xml.readElementText();
}
else if(xml.name() == QString("relaisName3"))
{
relaisName[2] = xml.readElementText();
}
else if(xml.name() == QString("relaisName4"))
{
relaisName[3] = xml.readElementText();
}
else if(xml.name() == QString("interface"))
{
interface = xml.readElementText();
}
else if(xml.name() == QString("interfaceSerialNumber"))
{
interfaceSerialNumber = xml.readElementText();
}
else if(xml.name() == QString("interfaceProductIdentifier"))
{
interfaceProductIdentifier = xml.readElementText().toUShort();
}
else if(xml.name() == QString("interfaceVendorIdentifier"))
{
interfaceVendorIdentifier = xml.readElementText().toUShort();
}
else if(xml.name() == QString("type"))
{
type = xml.readElementText().toInt();
}
else if(xml.name() == QString("active"))
{
active = xml.readElementText().toInt();
}
}
else if(xml.name() == QString("DLTRelais%1").arg(num))
{
isDLTRelais = true;
}
}
else if(xml.isEndElement())
{
/* Connection, plugin and filter */
if(xml.name() == QString("DLTRelais%1").arg(num))
{
isDLTRelais = false;
}
}
}
if (xml.hasError())
{
qDebug() << "Error in processing filter file" << filename << xml.errorString();
}
file.close();
}
void DLTRelais::trigger(int num,unsigned int duration)
{
if(!active)
return;
// trigger a Relais for 500ms
qDebug() << "DLTRelais: trigger" << num << duration;
serialPort.write(QString("R%1T%2\n").arg(num).arg(duration).toLatin1());
}
void DLTRelais::on(int num)
{
if(!active)
return;
// set Relais to on
qDebug() << "DLTRelais: on" << num;
if(num==1)
serialPort.write("R11\n");
else if(num==2)
serialPort.write("R21\n");
else if(num==3)
serialPort.write("R31\n");
else if(num==4)
serialPort.write("R41\n");
}
void DLTRelais::off(int num)
{
if(!active)
return;
// set Relais to off
qDebug() << "DLTRelais: off" << num;
if(num==1)
serialPort.write("R10\n");
else if(num==2)
serialPort.write("R20\n");
else if(num==3)
serialPort.write("R30\n");
else if(num==4)
serialPort.write("R40\n");
}