-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewconnect.cpp
344 lines (322 loc) · 13.2 KB
/
newconnect.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
#include "newconnect.h"
#include "ui_newconnect.h"
#include <QSerialPort>
#include "settingsdialog.h"
#include "console.h"
#include <QtWidgets>
#include "mainwindow.h"
#include <QDebug>
#include "getstream.h"
#include "dataprofiler.h"
#include "txtmaskobj.h"
#include <QStandardPaths>
newconnect::newconnect(QWidget *parent) :
QWidget(parent),
m_settings (new SettingsDialog),
m_serial (new QSerialPort(this)),
m_console (new Console),
m_status (new QLabel),
gstream (new getStream),
datapool (new dataprofiler),
ui(new Ui::newconnect)
{
ui->setupUi(this);
m_console->setEnabled(false);
m_console->setParent(ui->consoleFrame);
m_console->show();
#ifdef Q_OS_WIN32
appHomeDir = qApp->applicationDirPath() + QDir::separator();
#endif
#ifdef Q_OS_ANDROID
appHomeDir = QStandardPaths::standardLocations(QStandardPaths::DataLocation)[1] + QDir::separator();
#endif
connect(m_serial, &QSerialPort::errorOccurred, this, &newconnect::handleError);
connect(m_serial, &QSerialPort::readyRead, this, &newconnect::readData);
connect(m_console, &Console::getData, this, &newconnect::writeData);
connect(gstream, &getStream::giveMyByte, datapool, &dataprofiler::getByte);
connect(datapool, &dataprofiler::deviceData, this, &newconnect::transData);
connect(datapool, &dataprofiler::ready4read, gstream, &getStream::readPermission);
connect(datapool, &dataprofiler::readNext, gstream, &getStream::readIntByte);
connect(m_settings, &SettingsDialog::restoreConsoleAndButtons, this, &newconnect::restoreWindowAfterApplySettings);
connect (m_settings, &SettingsDialog::writeTextLog, this, &newconnect::writeTextLog);
connect (m_settings, &SettingsDialog::writeBinLog, this, &newconnect::writeBinLog);
connect (m_settings, &SettingsDialog::writeJsonLog, this, &newconnect::writeJsonLog);
connect (timer, &QTimer::timeout, this, &newconnect::readFromFile);//читаем из файла по таймеру
connect (this, &newconnect::sendRawData, gstream, &getStream::getRawData);
connect (this, &newconnect::sendRawData, m_console, &Console::putData);
on_settingsButton_clicked();
}
newconnect::~newconnect()
{
delete m_serial;
delete ui;
}
void newconnect::on_settingsButton_clicked()
{
m_settings->setParent(this);
m_console->hide();
ui->connectButton->hide();
ui->settingsButton->hide();
ui->consoleFrame->hide();
m_settings->show();
}
void newconnect::openSerialPort()
{
const SettingsDialog::Settings p = m_settings->settings();
p_local = m_settings->settings();
if (p.readFromFileFlag)
{
readProfile();
pos = 0;//задаём позицию для чтения FileSplitted в readFromFile()
fileSplitted.clear();
int freq = 1000/((p_local.baudRate/8)/bytesPerOneShot);
QFile file(p_local.pathToBinFile);
file.open(QIODevice::ReadOnly);
showStatusMessage(tr("Bufferisation..."));
QByteArray fileBuffer = file.readAll();//читаем весь файл в память
for (int i = 0; i < fileBuffer.size();)
{
static QByteArray ch;
while (ch.size() < bytesPerOneShot && i < fileBuffer.size())//создаём список FileSplitted с кусками файла fileBuffer равными bytesPerOneShot
{
ch.append(fileBuffer.at(i));
i++;
}
fileSplitted.push_back(ch);
ch.clear();
}
fileBuffer.clear();
showStatusMessage(tr("Read file %1").arg(p.pathToBinFile));
timer->start(freq);//запускаем таймер, по нему читается по порядку FileSplitted функцией readFromFile()
}
else
{
m_serial->setPortName(p.name);
m_serial->setBaudRate(p.baudRate);
m_serial->setDataBits(p.dataBits);
m_serial->setParity(p.parity);
m_serial->setStopBits(p.stopBits);
m_serial->setFlowControl(p.flowControl);
if (m_serial->open(QIODevice::ReadWrite)) {
readProfile();
m_console->setEnabled(true);
showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6, %7")
.arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl).arg(p.profilePath));
} else {
QMessageBox::critical(this, tr("Error"), m_serial->errorString());
showStatusMessage(tr("Open error"));
}
}
}
void newconnect::readFromFile()
{
if (pos < fileSplitted.size())
{//если текущая позиция не в конце списка (костыль вместо итератора) то кусок по нужному номеру листа добавляем в fsba
fsba.append(fileSplitted.at(pos));//добавляем кусок по указателю
pos++;
readData();//вызываем читалку данных
}
else
{
showStatusMessage(tr("End of file"));
timer->stop();
on_connectButton_clicked();
}
}
void newconnect::closeSerialPort()
{
if (m_serial->isOpen())
{
m_serial->close();
showStatusMessage(tr("Disconnected"));
}
if (p_local.readFromFileFlag)
{
p_local.readFromFileFlag = false;
timer->stop();
}
}
void newconnect::writeData(const QByteArray &data)
{
m_serial->write(data);
}
void newconnect::readData()
{
static QByteArray data;
if (p_local.readFromFileFlag)
{
data = fsba;//если есть флаг чтения из файла, то читаем из fsba
fsba.clear();
}
else
{
data = m_serial->readAll();//если нет то читаем всё что есть с порта
}
emit sendRawData(data);
data.clear();
}
void newconnect::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), m_serial->errorString());
closeSerialPort();
}
}
void newconnect::showStatusMessage(QString message)
{
m_status->setText(message);
emit sendStatusStr(message);
}
void newconnect::on_connectButton_clicked()
{
if (m_serial->isOpen() || p_local.readFromFileFlag)
{
this->closeSerialPort();
p_local.readFromFileFlag = false;
if (!(m_serial->isOpen()) && !p_local.readFromFileFlag)
{
ui->connectButton->setText(tr("Connect"));
showStatusMessage(tr("Connection closed"));
}
emit stopLog();
}
else if (!(m_serial->isOpen()) || !p_local.readFromFileFlag)
{
openSerialPort();
if (m_serial->isOpen() || p_local.readFromFileFlag)
{
emit startLog();
emit cleanGraph();
createNewFileNamePermission = true;
ui->connectButton->setText(tr("Disconnect"));
}
}
}
void newconnect::transData(QVector<int> snapshot)
{
emit transmitData(snapshot);
}
void newconnect::prepareToSaveProfile()
{
const SettingsDialog::Settings p = m_settings->settings();
if (!p.readOnlyProfile)
{//очищаем список, выставляем разрешение для дальнейших операций по сохранению, даём сигнал на запрос всех масок
maskVectorsList = this->findChildren<txtmaskobj*>();
QListIterator<txtmaskobj*> maskVectorsListIt(maskVectorsList);
maskVectorsListIt.toFront();
while (maskVectorsListIt.hasNext())
maskVectorsListIt.next()->~txtmaskobj();
permission2SaveMasks = true;
emit saveAllMasks();
}
}
void newconnect::saveProfileSlot4Masks(int devNum, QString devName, int byteNum, QString byteName, int id, QString paramName, QString paramMask, int, double valueShift, double valueKoef, bool viewInLogFlag, int wordType, bool _drawGraphFlag, QString _drawGraphColor)
{
//перед сохранением все маски сигналом отправляются сюда, что-бы образовать перечень масок
//проверяется что этой маски тут ещё нет, после этого создаётся список с текстовым перечнем всех параметров
//создаются только описания масок, само сохранение будет в другой функции
if (permission2SaveMasks)
{
maskVectorsList = this->findChildren<txtmaskobj*>();
QListIterator<txtmaskobj*> maskVectorsListIt(maskVectorsList);
bool thisMaskHere = false;
maskVectorsListIt.toFront();
while (maskVectorsListIt.hasNext())
{
if ((QString::number(id,10) == maskVectorsListIt.peekNext()->lst.at(1))&&(QString::number(devNum,10) == maskVectorsListIt.peekNext()->lst.at(2))&&(QString::number(byteNum,10)==maskVectorsListIt.peekNext()->lst.at(3))&& (maskVectorsListIt.peekNext()->lst.at(0) == "thisIsMask"))
thisMaskHere = true;
maskVectorsListIt.next();
}
if (!thisMaskHere)
{
QList<QString> maskList;
maskList.append("thisIsMask");//0
maskList.append(QString::number(id,10));//1
maskList.append(QString::number(devNum,10));//2
maskList.append(QString::number(byteNum,10));//3
maskList.append(devName);//4
maskList.append(byteName);//5
maskList.append(paramName);//6
maskList.append(paramMask);//7
maskList.append(QString::number(valueShift,'g',6));//8
maskList.append(QString::number(valueKoef,'g',6));//9
maskList.append((viewInLogFlag ?"true":"false"));//10
maskList.append(QString::number(wordType));//11
maskList.append(_drawGraphFlag ?"true":"false");//12
maskList.append(_drawGraphColor);//13
txtmaskobj *savingMask = new txtmaskobj(maskList);
savingMask->setParent(this);
maskList.clear();
}
}
}
void newconnect::saveProfile()
{
if (permission2SaveMasks)
{
maskVectorsList = this->findChildren<txtmaskobj*>();
QListIterator<txtmaskobj*> maskVectorsListIt(maskVectorsList);
maskVectorsListIt.toFront();
const SettingsDialog::Settings p = m_settings->settings();
QFile profile(p.profilePath);
QFileInfo info(profile);
profile.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream txtStream(&profile);
txtStream << info.fileName() << "\n";
while (maskVectorsListIt.hasNext())
{
QListIterator<QString> lstIt(maskVectorsListIt.peekNext()->lst);
while (lstIt.hasNext())
txtStream << lstIt.next() << "\t";
txtStream << "\n";
maskVectorsListIt.next();
}
permission2SaveMasks = false;
emit sendStatusStr("Profile " + info.fileName() + " saved");
}
}
void newconnect::readProfile()
{
emit cleanDevListSig();
const SettingsDialog::Settings p = m_settings->settings();
QFile profile(p.profilePath);
QFileInfo info(profile);
currentProfileName = getProfileNameFromInfo(info);
emit profileName2log(currentProfileName);
profile.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream txtStream(&profile);
while (!txtStream.atEnd())
{
QString str = txtStream.readLine();
QStringList strLst = str.split('\t');
if (strLst.at(0)=="thisIsMask")
emit loadMask(strLst.at(2).toInt(0,10),strLst.at(4),strLst.at(3).toInt(0,10),strLst.at(5),strLst.at(1).toInt(0,10),strLst.at(6),strLst.at(7),0,strLst.at(8).toDouble(),strLst.at(9).toDouble(),((QString::compare(strLst.at(10), "true") == 0) ? true : false),strLst.at(11).toInt(0,10), ((QString::compare(strLst.at(12), "true") == 0) ? true : false), strLst.at(13));
strLst.clear();
}
}
void newconnect::resizeEvent(QResizeEvent *event)
{
if (event)
{
m_console->resize(event->size());
m_settings->resize(event->size());
}
}
void newconnect::restoreWindowAfterApplySettings()
{
m_console->show();
ui->connectButton->show();
ui->settingsButton->show();
ui->consoleFrame->show();
}
QDateTime newconnect::returnTimestamp()
{
quint64 timestamp = QDateTime::currentMSecsSinceEpoch();
QDateTime dt3 = QDateTime::fromMSecsSinceEpoch(timestamp);
return dt3;
}
QString newconnect::getProfileNameFromInfo(QFileInfo& info)
{
return (info.fileName());
}