-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectionsettings.cpp
484 lines (441 loc) · 19.4 KB
/
connectionsettings.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/******************************************************************************
* *File: connectionsettings.cpp *
* *Date: 2013-09-04 *
* *Version: 1.0 *
* *Author(s): Jochen Bauer <[email protected]> *
* Lukas Kern <[email protected]> *
* Carsten Klein <[email protected]> *
* *
* *License information: *
* *
* Copyright (C) [2013] [Jochen Bauer, Lukas Kern, Carsten Klein] *
* *
* This file is part of PLCANALYZER. PLCANALYZER is free software; you can *
* redistribute it and/or modify it under the terms of the GNU General *
* Public License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301, USA. *
* *
* *Program name: PLCANALYZER *
* *What this program does: Connects to most SIMATIC S7/S5 Controllers *
* * Reads memory areas *
* * Draws a graph over time for operands *
* *Have fun! *
******************************************************************************/
#include "connectionsettings.h"
#include <iostream>
#include <QStandardItemModel>
ConnectionSettings::ConnectionSettings(QWidget *parent):
QDialog(parent),
ui(new Ui::ConnectionSettings)
{
// Build userinterface
ui->setupUi(this);
// Fill comboboxes for area and formats with data
comboBoxesArea = findChildren<QComboBox*>(QRegExp("comboBoxArea_.*"));
comboBoxesFormat = findChildren<QComboBox*>(QRegExp("comboBoxFormat_.*"));
comboBoxesLength = findChildren<QComboBox*>(QRegExp("comboBoxLength_.*"));
lineEditsBits = findChildren<QLineEdit*>(QRegExp("lineEditBit_.*"));
lineEditsAddress = findChildren<QLineEdit*>(QRegExp("lineEditAddress_.*"));
lineEditsDB = findChildren<QLineEdit*>(QRegExp("lineEditDB_.*"));
// Sorting algorithms for the right order of the objects
qSort(comboBoxesArea.begin(), comboBoxesArea.end(),
comboBoxPointerLessThan);
qSort(comboBoxesFormat.begin(), comboBoxesFormat.end(),
comboBoxPointerLessThan);
qSort(comboBoxesLength.begin(), comboBoxesLength.end(),
comboBoxPointerLessThan);
qSort(lineEditsBits.begin(), lineEditsBits.end(),
lineEditPointerLessThan);
qSort(lineEditsDB.begin(), lineEditsDB.end(),
lineEditPointerLessThan);
qSort(lineEditsAddress.begin(), lineEditsAddress.end(),
lineEditPointerLessThan);
//set the text of the comboboxes items
comboItemsArea << " " << "E" << "A" << "M" << "DB";
comboItemsLength << " " << "BIT" << "BYTE" << "WORD" << "DWORD";
comboItemsFormat << " " << "BOOL" << "BIN" << "DEZ" << "HEX" << "FLOAT" << "CHAR";
//set the equivalent values for the items of the comboboxes
comboValuesArea << 0x0 << daveInputs << daveOutputs << daveFlags << daveDB;
comboValuesLength << 0x0 << DatLenBit << DatLenByte << DatLenWord << DatLenDWord;
comboValuesFormat << 0x0 << AnzFormatBool << AnzFormatBinaer
<< AnzFormatDezimal << AnzFormatHexadezimal
<< AnzFormatGleitpunkt << AnzFormatZeichen;
// Initialize with two for loops. Q_FOREACH would fit too,
// seems simpler and faster but then an additional iterator
// value declaration outside the loop was necessary.
for(int i=0;i<comboBoxesArea.count();++i)
{
for(int j=0;j<comboItemsArea.count();++j)
comboBoxesArea[i]->addItem(comboItemsArea[j],comboValuesArea[j]);
}
for(int i=0;i<comboBoxesFormat.count();++i)
{
for(int j=0;j<comboItemsFormat.count();++j)
comboBoxesFormat[i]->addItem(comboItemsFormat[j],
comboValuesFormat[j]);
}
for(int i=0;i<comboBoxesLength.count();++i)
{
for(int j=0;j<comboItemsLength.count();++j)
comboBoxesLength[i]->addItem(comboItemsLength[j],
comboValuesLength[j]);
}
//Connect the format- and length-combo boxes' indexChanged signal to the handling
//Slot for conditional disabling of Bit field
for(int i=0;i<comboBoxesArea.count();++i)
{
connect(comboBoxesArea[i],SIGNAL(currentIndexChanged(int)),
this,SLOT(comboBoxIndexChanged(int)));
connect(comboBoxesLength[i],SIGNAL(currentIndexChanged(int)),
this,SLOT(comboBoxIndexChanged(int)));
}
// Fill ComboBox "Protocol" with current settings
ui->ComboBox_Protokoll->addItem("MPI for S7 300/400", daveProtoMPI);
ui->ComboBox_Protokoll->addItem("PPI for S7 200", daveProtoPPI);
ui->ComboBox_Protokoll->addItem("S5 programming port protocol",
daveProtoAS511);
#ifdef BCCWIN
ui->ComboBox_Protokoll->addItem("use s7onlinx.dll for transport",
daveProtoS7online);
#endif
ui->ComboBox_Protokoll->addItem("ISO over TCP", daveProtoISOTCP);
ui->ComboBox_Protokoll->addItem("ISO over TCP with CP243",
daveProtoISOTCP243);
ui->ComboBox_Protokoll->addItem("ISO over TCP with Routing",
daveProtoISOTCPR);
// Validator for IP Address Input
QRegExpValidator *RegExp_IP = new QRegExpValidator(this);
QRegExp rx("((1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})\\.)"\
"{3,3}(1{0,1}[0-9]{0,2}|2[0-4]{1,1}[0-9]{1,1}|25[0-5]{1,1})");
RegExp_IP->setRegExp(rx);
ui->lineEdit_IP->setValidator(RegExp_IP);
// Validator for MPI Address (0..127) input
QRegExpValidator *RegExp_MPI = new QRegExpValidator(this);
rx.setPattern("[0-9]{0,2}|1{1,1}[0-1]?[0-9]?|1{1,1}2{1,1}[0-7]{1,1}");
RegExp_MPI->setRegExp(rx);
ui->lineEdit_CPU_MPI->setValidator(RegExp_MPI);
ui->lineEdit_local_MPI->setValidator(RegExp_MPI);
graphColors << Qt::green << Qt::black << Qt::blue << Qt::cyan
<< Qt::darkYellow << Qt::magenta << Qt::darkRed << Qt::darkCyan;
moduloValue=graphColors.size();
//Integer Validator for the Address and Bit fields
myBitValidator = new QIntValidator(0,7,this);
myByteAddressValidator = new QIntValidator(0,4096,this);
for(int i=0;i<lineEditsBits.count();++i)
{
lineEditsBits[i]->setValidator(myBitValidator);
lineEditsAddress[i]->setValidator(myByteAddressValidator);
lineEditsDB[i]->setValidator(myByteAddressValidator);
}
}
// Destructor of ConnectionSettings
ConnectionSettings::~ConnectionSettings()
{
delete ui;
}
// Changes confirmed with "OK" button
void ConnectionSettings::on_buttonBox_accepted()
{
// Write changes to member variable
m_DiagSets->IP_Adr = ui->lineEdit_IP->text();
m_DiagSets->localMPI = ui->lineEdit_local_MPI->text().toInt();
m_DiagSets->plcMPI = ui->lineEdit_CPU_MPI->text().toInt();
m_DiagSets->speed = ui->comboBox_Speed->currentIndex();
m_DiagSets->useProto = ui->ComboBox_Protokoll->itemData(ui->ComboBox_Protokoll->currentIndex()).toInt();
m_DiagSets->rack = ui->lineEdit_Rack->text().toInt();
m_DiagSets->slot = ui->lineEdit_Slot->text().toInt();
// Emit Signal "Settings Changed"
emit SettingsChanged(m_DiagSets);
int iAnzahlSlots = 0;
// Count number of areas != NULL
for(int i = 0; i < comboBoxesArea.count(); i++)
{
if(comboBoxesArea[i]->itemData(comboBoxesArea[i]->currentIndex()).toInt())
{
// This Area setting is choosed, need for a slot
iAnzahlSlots++;
}
else
{
// The first empty entry -> ignore all other entries
break;
}
}
// Has the number of Slots changed?
if(newSlots.isEmpty() || newSlots.size()!=iAnzahlSlots)
{
//initialize the conslot vector with the number of slots
newSlots.resize(iAnzahlSlots);
}
//Insert Slot data in Vector
for(int i=0; i < iAnzahlSlots; ++i){
newSlots[i].iBitnummer = lineEditsBits[i]->text().toInt();
newSlots[i].iStartAdr = lineEditsAddress[i]->text().toInt();
newSlots[i].iAnzFormat = comboBoxesFormat[i]->itemData(comboBoxesFormat[i]->currentIndex()).toInt();
newSlots[i].iAdrBereich = comboBoxesArea[i]->itemData(comboBoxesArea[i]->currentIndex()).toInt();
newSlots[i].iDBnummer = lineEditsDB[i]->text().toInt();
newSlots[i].graphColor = graphColors[i % moduloValue];
newSlots[i].iDatenlaenge = comboBoxesLength[i]->itemData(comboBoxesLength[i]->currentIndex()).toInt();
}
// Emit the signal that the Slots have changed
emit SlotsChanged(newSlots);
}
void ConnectionSettings::SetSettings(ConSets *CurrentSets)
{
// Set the membervariables with the current changes
m_DiagSets = CurrentSets;
// Set input boxes with the current values
ui->lineEdit_IP->setText(m_DiagSets->IP_Adr);
ui->lineEdit_local_MPI->setText(QString::number(m_DiagSets->localMPI));
ui->lineEdit_CPU_MPI->setText(QString::number(m_DiagSets->plcMPI));
ui->comboBox_Speed->setCurrentIndex(m_DiagSets->speed);
ui->ComboBox_Protokoll->setCurrentIndex(ui->ComboBox_Protokoll->findData(m_DiagSets->useProto));
ui->lineEdit_Rack->setText(QString::number(m_DiagSets->rack));
ui->lineEdit_Slot->setText(QString::number(m_DiagSets->slot));
}
void ConnectionSettings::on_ComboBox_Protokoll_currentIndexChanged(int index)
{
// Set the enable status of input boxes corresponding the current protocol type
int iCurrProto = ui->ComboBox_Protokoll->itemData(index).toInt();
switch(iCurrProto)
{
case daveProtoMPI:
case daveProtoPPI:
case daveProtoAS511:
ui->comboBox_Speed->setEnabled(true);
ui->lineEdit_CPU_MPI->setEnabled(true);
ui->lineEdit_IP->setEnabled(false);
ui->lineEdit_local_MPI->setEnabled(true);
ui->lineEdit_Rack->setEnabled(true);
ui->lineEdit_Slot->setEnabled(true);
break;
case daveProtoS7online:
ui->comboBox_Speed->setEnabled(false);
ui->lineEdit_CPU_MPI->setEnabled(false);
ui->lineEdit_IP->setEnabled(false);
ui->lineEdit_local_MPI->setEnabled(false);
ui->lineEdit_Rack->setEnabled(true);
ui->lineEdit_Slot->setEnabled(true);
break;
case daveProtoISOTCP:
case daveProtoISOTCP243:
case daveProtoISOTCPR:
ui->comboBox_Speed->setEnabled(false);
ui->lineEdit_CPU_MPI->setEnabled(false);
ui->lineEdit_IP->setEnabled(true);
ui->lineEdit_local_MPI->setEnabled(false);
ui->lineEdit_Rack->setEnabled(true);
ui->lineEdit_Slot->setEnabled(true);
break;
default:
ui->comboBox_Speed->setEnabled(false);
ui->lineEdit_CPU_MPI->setEnabled(false);
ui->lineEdit_IP->setEnabled(false);
ui->lineEdit_local_MPI->setEnabled(false);
ui->lineEdit_Rack->setEnabled(false);
ui->lineEdit_Slot->setEnabled(false);
break;
}
}
void ConnectionSettings::comboBoxIndexChanged(int index)
{
// get the object name of the object which invoked the Slot
// Afterwards check which vector element it belongs to and adjust
// the visibility / content of the conslot elements on the ui as
// well as in the vector itself
QComboBox *sendingBox = (QComboBox *)sender();
int dataItem = sendingBox->itemData(index).toInt();
int lineNumber = 0;
int originOfEvent = 0;
//get the corresponding line
if(comboBoxesArea.contains(sendingBox)){
lineNumber = findCorrespondingLine(comboBoxesArea,sendingBox);
originOfEvent = 1;
}else if(comboBoxesFormat.contains(sendingBox)){
lineNumber = findCorrespondingLine(comboBoxesFormat,sendingBox);
originOfEvent = 2;
}else if(comboBoxesLength.contains(sendingBox))
{
lineNumber = findCorrespondingLine(comboBoxesLength,sendingBox);
originOfEvent = 3;
}
switch(originOfEvent)
{
case 1:
// Area has been changed
// Dis/Enable the following AreaComboboxes
if(comboBoxesArea.count() - 1 > lineNumber)
{
if(comboBoxesArea[lineNumber]->currentIndex() == 0)
{
comboBoxesArea[lineNumber + 1]->setDisabled(true);
comboBoxesArea[lineNumber + 1]->setCurrentIndex(0);
}
else
{
comboBoxesArea[lineNumber + 1]->setEnabled(true);
}
}
// Dis/Enable the current line
if(comboBoxesArea[lineNumber]->currentIndex() == 0)
{
// Nothing is selected
comboBoxesLength[lineNumber]->setDisabled(true);
comboBoxesFormat[lineNumber]->setDisabled(true);
lineEditsDB[lineNumber]->setDisabled(true);
lineEditsAddress[lineNumber]->setDisabled(true);
comboBoxesLength[lineNumber]->setCurrentIndex(0);
comboBoxesFormat[lineNumber]->setCurrentIndex(0);
lineEditsDB[lineNumber]->clear();
lineEditsAddress[lineNumber]->clear();
}
else
{
// One item is selected
comboBoxesLength[lineNumber]->setEnabled(true);
if(comboBoxesArea[lineNumber]->currentIndex() == 4)
{
// DB is selected
lineEditsDB[lineNumber]->setEnabled(true);
}
else
{
// DB is deselected
lineEditsDB[lineNumber]->setDisabled(true);
lineEditsDB[lineNumber]->clear();
}
}
break;
case 2:
break;
case 3:
// Lenght has been changed
// Set the available representation for this lenght
enableSwitcher(dataItem, lineNumber);
if(!comboBoxesFormat.contains(sendingBox) && !readingFromFile){
comboBoxesFormat[lineNumber]->setCurrentIndex(0);
}
// Format has been changed
if(comboBoxesLength[lineNumber]->currentIndex() == 1)
{
// Datalenght Bit is selected
lineEditsBits[lineNumber]->setEnabled(true);
}
else
{
lineEditsBits[lineNumber]->setDisabled(true);
lineEditsBits[lineNumber]->clear();
}
if(comboBoxesLength[lineNumber]->currentIndex() != 0)
{
lineEditsAddress[lineNumber]->setEnabled(true);
comboBoxesFormat[lineNumber]->setEnabled(true);
}
break;
default:
break;
}
}
int ConnectionSettings::findCorrespondingLine(QList<QComboBox*> areaBoxes,
QComboBox* sendingBox)
{
int lineNumber = 0;
int count = 0;
// Q_FOREACH need less chars to type ;-)
Q_FOREACH(QComboBox *areaBox,areaBoxes)
{
if(areaBox == sendingBox) {lineNumber = count;}
count++;
}
return lineNumber;
}
void ConnectionSettings::setSlots(QVector<ConSlot> ¤tSlots)
{
// The boolean value prevents the slot, invoced by the current index changed signal of the
// UI Elements to do its work in that very case.
readingFromFile = true;
for(int i=0; i<currentSlots.count();++i)
{
comboBoxesArea[i]->setCurrentIndex(comboValuesArea.indexOf(currentSlots[i].iAdrBereich));
comboBoxesFormat[i]->setCurrentIndex(comboValuesFormat.indexOf(currentSlots[i].iAnzFormat));
lineEditsBits[i]->setText(QString::number(currentSlots[i].iBitnummer));
lineEditsAddress[i]->setText(QString::number(currentSlots[i].iStartAdr));
lineEditsDB[i]->setText(QString::number(currentSlots[i].iDBnummer));
comboBoxesLength[i]->setCurrentIndex(comboValuesLength.indexOf(currentSlots[i].iDatenlaenge));
}
readingFromFile = false;
}
//LessThan Comparison for combo boxes
bool ConnectionSettings::comboBoxPointerLessThan(QComboBox *cb1, QComboBox *cb2)
{
return cb1->objectName() < cb2->objectName();
}
//LessThan Comparison for line edits
bool ConnectionSettings::lineEditPointerLessThan(QLineEdit* le1, QLineEdit* le2)
{
return le1->objectName() < le2->objectName();
}
//switch to select case
void ConnectionSettings::enableSwitcher(int iDataitem, int iLinenumber)
{
//create a temporary array with 6 elements
bool *bEnablerArray = new bool[6];
bEnablerArray[1] = true; //enable binary
bEnablerArray[2] = true; //enable decimal
//select the settings for the selectable property
switch (iDataitem)
{
case DatLenBit:
bEnablerArray[0] = true; //enable bool
bEnablerArray[3] = false; //disable hex
bEnablerArray[4] = false; //disable float
bEnablerArray[5] = false; //disable char
break;
case DatLenByte:
case DatLenWord:
bEnablerArray[0] = false; //diable bool
bEnablerArray[3] = true; //enable hex
bEnablerArray[4] = false; //disable float
bEnablerArray[5] = true; //enable char
break;
case DatLenDWord:
bEnablerArray[0] = false; //diable bool
bEnablerArray[3] = true; //enable hex
bEnablerArray[4] = true; //enable float
bEnablerArray[5] = true; //enable char
break;
default:
bEnablerArray[0] = false; //disable bool
bEnablerArray[1] = false; //disable binary
bEnablerArray[2] = false; //disable decimal
bEnablerArray[3] = false; //disable hex
bEnablerArray[4] = false; //disable float
bEnablerArray[5] = false; //disable char
}
//enable or disable the elements
for (int i = 0; i < 6; i++)
{
Enabler(iLinenumber, bEnablerArray [i], i + 1);
}
//free the allocated block of memory
delete [] bEnablerArray;
}
//enables or disables the selectable attribute of the item in the combobox
void ConnectionSettings::Enabler (int iLinenumber, bool bEnable, int iIndex)
{
qobject_cast<QStandardItemModel *>(comboBoxesFormat[iLinenumber]->model())->item(iIndex)->setEnabled(bEnable);
}
void ConnectionSettings::clearSlots()
{
// Quick and dirty hack to clear the slots
comboBoxesArea[0]->setCurrentIndex(0);
}