-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
378 lines (337 loc) · 10.8 KB
/
mainwindow.cpp
File metadata and controls
378 lines (337 loc) · 10.8 KB
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "extserialportmodbusdevice.h"
#include "datasreceivethread.h"
#include "databuffer.h"
#include "./Global/global.h"
#include "./Global/algorithm.h"
#include "./Global/sysconfig.h"
#include <QMessageBox>
#include <QDebug>
#include <QTimer>
#include <QThread>
#include <QHostAddress>
#include <QFile>
#include <QFileDialog>
#ifdef _MSC_VER
#pragma execution_character_set("utf-8")
#endif
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
m_pModbusDevice = NULL;
ui->setupUi(this);
ui->pushButton_send->setEnabled(false);
ui->textEdit_recv->setReadOnly(true);
ui->checkBox_addCRC->setEnabled(false);
ui->checkBox_timing->setEnabled(false);
ui->spinBox_timing->setEnabled(false);
InitSerialPortList();
InitSerialPortBaudRateList();
m_pRecvThread = new DatasReceiveThread(this);
m_pDataBuf = new DataBuffer(this);
m_pRecvThread->setDataBuffer(m_pDataBuf);
readState();
// 构造定时器,设置超时为 100 毫秒
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(100);
// 构造定时器,定时发送数据
sendTimer = new QTimer(this);
connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendData()));
}
MainWindow::~MainWindow()
{
saveState();
delete ui;
}
void MainWindow::readState()
{
SysConfig::getInstance().readConfig();
ui->comboBox_deviceType->setCurrentText(QString(SysConfig::getInstance().m_ModbusDevice.deviceType));
ui->comboBox_spBaudRate->setCurrentIndex(SysConfig::getInstance().m_ModbusDevice.spBaudRate);
ui->comboBox_spDataBits->setCurrentIndex(SysConfig::getInstance().m_ModbusDevice.spDataBits);
ui->comboBox_spStopBits->setCurrentIndex(SysConfig::getInstance().m_ModbusDevice.spStopBits);
ui->comboBox_spParity->setCurrentText(QString(SysConfig::getInstance().m_ModbusDevice.spParity));
if(SysConfig::getInstance().m_ModbusDevice.isSendHEX)
{
ui->textEdit_send->append(SysConfig::getInstance().m_ModbusDevice.hexData);
}
else
{
ui->textEdit_send->append(SysConfig::getInstance().m_ModbusDevice.strData);
}
ui->checkBox_hexSend->setChecked(SysConfig::getInstance().m_ModbusDevice.isSendHEX);
ui->checkBox_hexShow->setChecked(SysConfig::getInstance().m_ModbusDevice.isShowHEX);
ui->checkBox_addCRC->setChecked(SysConfig::getInstance().m_ModbusDevice.isCRC16);
}
void MainWindow::saveState()
{
SysConfig& cfg = SysConfig::getInstance();
cfg.m_ModbusDevice.deviceType = ui->comboBox_deviceType->currentText().toInt();
cfg.m_ModbusDevice.spBaudRate = ui->comboBox_spBaudRate->currentIndex();
cfg.m_ModbusDevice.spDataBits = ui->comboBox_spDataBits->currentIndex();
cfg.m_ModbusDevice.spStopBits = ui->comboBox_spStopBits->currentIndex();
cfg.m_ModbusDevice.spParity = ui->comboBox_spParity->currentText().toInt();
if(ui->checkBox_hexSend->isChecked())
{
cfg.m_ModbusDevice.hexData = ui->textEdit_send->toPlainText();
}
else
{
cfg.m_ModbusDevice.strData = ui->textEdit_send->toPlainText();
}
cfg.m_ModbusDevice.isSendHEX = ui->checkBox_hexSend->isChecked();
cfg.m_ModbusDevice.isShowHEX = ui->checkBox_hexShow->isChecked();
cfg.m_ModbusDevice.isCRC16 = ui->checkBox_addCRC->isChecked();
SysConfig::getInstance().writeConfig();
}
void MainWindow::update()
{
Q_ASSERT(m_pDataBuf != NULL);
QList<DataInfo> lstFrames;
m_pDataBuf->popAllFrames(lstFrames);
for( int i=0; i<lstFrames.size(); ++i)
{
showFrame(lstFrames[i]);
}
}
void MainWindow::showFrame(const DataInfo &frame)
{
QString strText;
if (ui->checkBox_hexShow->isChecked())
strText = Algorithm::ByteArray2String(frame.data);
else
strText = frame.data.data();
QString strShowData = QString("[%1] %2-> %3")
.arg(frame.time.toString("HH:mm:ss.zzz"))
.arg(frame.bSend?QString("Send"):QString("Receive"))
.arg(strText);
ui->textEdit_recv->append(strShowData);
//m_lstFrames.append(frame);
}
void MainWindow::InitSerialPortList()
{
spInfoList = QSerialPortInfo::availablePorts();
ui->comboBox_serialport->clear();
foreach (QSerialPortInfo spInfo, spInfoList)
{
ui->comboBox_serialport->addItem(spInfo.portName());
}
}
void MainWindow::InitSerialPortBaudRateList()
{
int curIndex = ui->comboBox_spBaudRate->currentIndex();
spBaudRateList.clear();
spBaudRateList<<300<<600<<1200<<2400<<4800<<9600<<12500<<25000<<19200<<38400
<<57600<<115200<<256000;
ui->comboBox_spBaudRate->clear();
foreach (qint32 baudRate, spBaudRateList)
{
ui->comboBox_spBaudRate->addItem(tr("%1bps").arg(baudRate, 6));
}
ui->comboBox_spBaudRate->setCurrentIndex(curIndex);
}
void MainWindow::on_pushButton_open_clicked()
{
int devIndex = ui->comboBox_serialport->currentIndex();
int btIndex = ui->comboBox_spBaudRate->currentIndex();
qDebug()<<devIndex;
if (devIndex < 0)
{
QMessageBox msgBox;
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("请你选择串口"));
msgBox.exec();
return;
}
if (btIndex < 0)
{
QMessageBox msgBox;
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("请你选择波特率"));
msgBox.exec();
return;
}
m_pModbusDevice = new ExtSerialPortModbusDevice(spInfoList[devIndex].portName(),
spBaudRateList[btIndex]);
int dataBits = ui->comboBox_spDataBits->currentIndex();
m_pModbusDevice->setDataBits((ModbusDataBits)dataBits);
int dtopBits = ui->comboBox_spStopBits->currentIndex();
m_pModbusDevice->setStopBits((ModbusStopBits)dtopBits);
int darityBits = ui->comboBox_spParity->currentIndex();
m_pModbusDevice->setParityBits((ModbusParityBits)darityBits);
if(m_pModbusDevice->open())
{
ui->spinBox_timing->setEnabled(false);
ui->pushButton_send->setEnabled(true);
ui->label_indicatorLight->setPixmap(QPixmap(":/png/Image/point_green.png"));
ui->label_indicatorLight->setScaledContents(true);
ui->checkBox_addCRC->setEnabled(true);
ui->checkBox_timing->setEnabled(true);
ui->pushButton_refreash->setEnabled(false);
ui->spinBox_timing->setEnabled(true);
ui->groupBox->setEnabled(false);
ui->pushButton_open->setEnabled(false);
//启动接收线程
m_pRecvThread->setModbusDevice(m_pModbusDevice);
m_pRecvThread->start();
}
else
{
QMessageBox msgBox;
msgBox.setWindowTitle(tr("提示"));
msgBox.setText(tr("设备打开失败,请检查串口是否被占用"));
msgBox.exec();
return;
}
}
void MainWindow::on_pushButton_refreash_clicked()
{
InitSerialPortList();
InitSerialPortBaudRateList();
}
void MainWindow::seTimer(bool isTiming)
{
int time = ui->spinBox_timing->value();
if(isTiming)
{
sendTimer->start(time);
}
else
{
sendTimer->stop();
}
}
void MainWindow::on_pushButton_send_clicked()
{
sendData();
}
void MainWindow::sendData()
{
//1、从控件读取输入
QString strText = ui->textEdit_send->toPlainText();
//2、根据是否16进制发送转换数据
if(ui->checkBox_hexSend->isChecked())
{
m_sendDatas = Algorithm::String2ByteArray(strText);
}
else
{
QStringList Mylist = strText.split(" ");
QStringList Newlist;
for(int i = 0; i<Mylist.size();++i)
{
int a = Mylist.at(i).toInt(Q_NULLPTR,16);
QChar c = QChar(a);
Newlist.append(c);
}
QString show = Newlist.join("");
m_sendDatas = show.toLatin1();
}
//3、添加CRC
if (ui->checkBox_addCRC->isChecked())
Algorithm::AppendCRC16(m_sendDatas);
//4、发送
m_pModbusDevice->sendData(m_sendDatas);
//5、显示和存储
DataInfo info;
info.bSend = true;
info.time = QDateTime::currentDateTime();
info.data = m_sendDatas;
showFrame(info);
m_lstFrames.append(info);
}
void MainWindow::on_pushButton_clear_clicked()
{
m_lstFrames.clear();
ui->textEdit_recv->clear();
}
void MainWindow::on_pushButton_close_clicked()
{
if(ui->checkBox_timing->isChecked())
{
ui->checkBox_timing->setChecked(false);
m_isTiming = false;
seTimer(m_isTiming);
}
//关闭线程
m_pRecvThread->m_bRunning = false;
m_pRecvThread->wait();
//关闭并且释放通信设备
if (m_pModbusDevice != NULL)
{
m_pModbusDevice->close();
delete m_pModbusDevice;
m_pModbusDevice = NULL;
}
ui->pushButton_send->setEnabled(false);
ui->label_indicatorLight->setPixmap(QPixmap(":/png/Image/point_red.png"));
ui->label_indicatorLight->setScaledContents(true);
ui->checkBox_addCRC->setEnabled(false);
ui->checkBox_timing->setEnabled(false);
ui->pushButton_refreash->setEnabled(true);
ui->spinBox_timing->setEnabled(false);
ui->groupBox->setEnabled(true);
ui->pushButton_open->setEnabled(true);
}
/**
* @brief 16进制数据转ASCII字符数据
* @param checked
*/
void MainWindow::on_checkBox_hexShow_clicked(bool checked)
{
ui->textEdit_recv->clear();
}
void MainWindow::on_checkBox_hexSend_clicked(bool checked)
{
if(checked)
{
m_strData = ui->textEdit_send->toPlainText();
ui->textEdit_send->clear();
ui->textEdit_send->append(m_hexData);
}
else
{
m_hexData = ui->textEdit_send->toPlainText();
ui->textEdit_send->clear();
ui->textEdit_send->append(m_strData);
}
}
void MainWindow::on_checkBox_timing_clicked(bool checked)
{
if(!checked)
{
m_isTiming = false;
seTimer(m_isTiming);
ui->spinBox_timing->setEnabled(true);
}
else
{
m_isTiming = true;
seTimer(m_isTiming);
ui->spinBox_timing->setEnabled(false);
}
}
void MainWindow::on_spinBox_timeout_valueChanged(int arg1)
{
Q_ASSERT(m_pRecvThread != NULL);
m_pRecvThread->m_nTimeoutMS = arg1;
}
void MainWindow::on_pushButton_save_clicked()
{
QString filePath = QFileDialog::getSaveFileName(this, tr("Save Data"), "",
tr("Data Files (*.txt)"));
if (filePath.isNull())
return;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly|QIODevice::Text))
{
QMessageBox::information(this, QString::fromStdWString(L"提示"),
QString::fromStdWString(L"无法创建文件"));
return;
}
QString data = ui->textEdit_recv->toPlainText();
file.write(data.toLatin1());
}