-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
210 lines (152 loc) · 5.63 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
GlobalVars::initEveryThing();
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
ui->PortBox->addItem(info.portName());
}
ui->PortBox->setCurrentIndex(0);
ui->BaudBox->setCurrentIndex(5); //
timerSingleShot = new QTimer(this);
timerSingleShot->setSingleShot(true);
connect(timerSingleShot, SIGNAL(timeout()), this, SLOT(onTimerSingleShotElapsed()));
timerSingleShot->start(1000);
qDebug()<<"all cleared";
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pbClear_clicked()
{
receiveIndex = 0;
ui->textBrowser->clear();
}
void MainWindow::on_pbStart_clicked()
{
}
void MainWindow::on_pbClose_clicked()
{
m_serialPort.close();
this->close();
}
void MainWindow::on_openButton_clicked()
{
toggleOpenSerial = !toggleOpenSerial ;
if(toggleOpenSerial ) {
m_serialPort.setPortName(ui->PortBox->currentText());
m_serialPort.setBaudRate(ui->BaudBox->currentText().toInt());
m_serialPort.flush();
m_serialPort.clear();
if(m_serialPort.open(QIODevice::ReadWrite)){
m_serialPort.flush();
m_serialPort.clear();
ui->openButton->setText(tr("listening\nMode"));
ui->openButton->setStyleSheet("background-color:lime; color: black;");
qDebug()<<" Serial Port opend: name: "<<m_serialPort.portName();
} else {
ui->openButton->setText(tr("Error"));
ui->openButton->setStyleSheet("background-color:red; color: black;");
qDebug()<<"+++++++++ Serial Port Error: name: "<<m_serialPort.portName();
}
m_serialPort.flush();
m_serialPort.clear();
connect(&m_serialPort, SIGNAL(readyRead()), this, SLOT(onSerialReadyData()));
ui->PortBox->setEnabled(false);
ui->BaudBox->setEnabled(false);
}
else {
m_serialPort.close();
m_serialPort.clear();
ui->PortBox->setEnabled(true);
ui->BaudBox->setEnabled(true);
ui->openButton->setText(tr("Stopped"));
ui->openButton->setStyleSheet("background-color:transparent; color: black; border:0px solid black; border-left:2px solid black; border-bottom:2px solid black;");
}
}
void MainWindow::onTimerSingleShotElapsed()
{
on_openButton_clicked();
}
void MainWindow::onSerialReadyData()
{
receivedBytes = m_serialPort.readAll();
qDebug()<<" Read Data:: "<<receivedBytes;
ui->textBrowser->setTextColor( QColor( "red" ) );
ui->textBrowser->append(QString(QDateTime::currentDateTime().toString("HH:mm:ss:zzz")+" : " + receivedBytes.toHex().toUpper()));
/*
mlocker.tryLock();
serialData += receivedBytes;
mlocker.unlock();
processSerialData();
*/
}
void MainWindow::processSerialData(){
while(serialData.length()>4){
//qDebug()<<"SerialPort Process: "<<serialData;
indexOfHeader = serialData.indexOf("\xEA\x92");
if(indexOfHeader > 0)
serialData.remove(0, indexOfHeader);
indexOfHeader = serialData.indexOf("\xAA");
//qDebug()<<" index of Header is: "<<indexOfHeader;
singlePacket = serialData.left(indexOfHeader+1);
processSerialSinglePacket(singlePacket);
serialData.remove(0, indexOfHeader+1);
}
}
void MainWindow::processSerialSinglePacket(QByteArray ba){
currentPacketType = static_cast<int>(singlePacket[2]);
int channel = static_cast<int>(singlePacket[3]);
int status = static_cast<int>(singlePacket[4]);
qDebug()<<receiveIndex<<". Single Packet ID: "<<singlePacket<<" -> "<<currentPacketType;
ui->textBrowser->setTextColor( QColor( "red" ) );
ui->textBrowser->append(QString(QDateTime::currentDateTime().toString("HH:mm:ss:zzz")+" : " + ba.toHex().toUpper()));
//ui->textBrowser->append(QString(QString::number(receiveIndex)+" : " + singlePacket.toHex().toUpper()));
}
void MainWindow::serialSendTheseBytes(QByteArray ba)
{
//qDebug()<<" ------------------------";
//qDebug()<<" sending:: "<<ba<<" length:"<<ba.length();
if(ba != serialBytesTxhandShake ){
qDebug()<<" sending:: "<<ba<<" length:"<<ba.length();
ui->textBrowser->setTextColor( QColor( "green" ) );
//ui->textBrowser->append(QString(QDateTime::currentDateTime().toString("HH:mm:ss:zzz")+" : " + ba.toHex().toUpper()));
ui->textBrowser->append(QString(QString::number(receiveIndex)+" : " + ba.toHex().toUpper()));
receiveIndex++;
}
qDebug()<<"number of Bytes written: "<<m_serialPort.write(ba, ba.length());
qDebug()<<"\n";
}
float MainWindow::randomNumberGeneratorFloat(double min, double max)
{
//srand((unsigned)time(NULL));
int n = max - min;
int remainder = RAND_MAX % n;
int x;
do{ x = rand(); }while (x >= RAND_MAX - remainder);
QRandomGenerator gen1 = QRandomGenerator::securelySeeded();
return (float)((min + x % n) + gen1.generateDouble());
}
void MainWindow::on_pbSettings_clicked()
{
portSettingObj = new PortSettings(this);
connect(portSettingObj, SIGNAL(txNewSerialPortConfig(QString)), this, SLOT(rxNewSerialPortConfig(QString)));
portSettingObj->show();
}
void MainWindow::rxNewSerialPortConfig(QString portName)
{
qDebug()<<"rxNewSerialPortConfig :: Selected :"<<portName;
if(toggleOpenSerial){
on_openButton_clicked();
ui->PortBox->setCurrentText(portName);
on_openButton_clicked();
}
else {
ui->PortBox->setCurrentText(portName);
on_openButton_clicked();
}
}