小程序-Ⅱ型铁心数据读取程序

引言

小程序使用Qt Creator 14.0.0制作。
小程序使用标准modbus 485协议进行通信。
下发询问四个寄存器,返回两个四字节float型数据,大端存储。
可以设置下发问询设备数量。
可以设置对解析数据的告警。
可以设备告警值的偏差范围。
图片

实现

文件结构

顶栏图标
图片
程序图标
图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
COMtraver
|-COMtraver.pro
头文件
|-mythread.h
|-travercom.h
源文件
|-main.cpp
|-mythread.cpp
|-travercom.cpp
界面文件
|-tarvercom.ui
资源
picture.qrc
/
1.png

COMtraver.pro

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
QT       += core gui serialbus serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp \
mythread.cpp \
travercom.cpp

HEADERS += \
mythread.h \
travercom.h

FORMS += \
travercom.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
picture.qrc

RC_FILE = COMtraver.rc

mythread.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QtSerialPort/QtSerialPort>
#include <QWidget>
#include "travercom.h"

class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(TraverCOM * parent, QSerialPort *port);
protected:
void run();
protected:
QSerialPort * port;
TraverCOM * parent;
signals:
void sendResult(int code, QString str);
};
#endif // MYTHREAD_H

travercom.h

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
#ifndef TRAVERCOM_H
#define TRAVERCOM_H

#include <QWidget>
#include <QtSerialPort/QtSerialPort>
#include <QList>
#include <QtSerialPort/QSerialPortInfo>
#include <QMessageBox>
#include <QString>

QT_BEGIN_NAMESPACE
namespace Ui {
class TraverCOM;
}
QT_END_NAMESPACE

class TraverCOM : public QWidget
{
Q_OBJECT

public:
TraverCOM(QWidget *parent = nullptr);
~TraverCOM();
public:
QTimer* Timer_1;
QTimer* Timer_2;
private slots:

void on_COM_currentTextChanged(const QString &arg1);

void on_COM_search_clicked();

void on_Baud_rate_currentTextChanged(const QString &arg1);

void on_stop_bit_currentTextChanged(const QString &arg1);

void on_addrfix_textEdited(const QString &arg1);

void on_function_code_textEdited(const QString &arg1);

void on_register_addrH_textEdited(const QString &arg1);

void on_register_addrL_textEdited(const QString &arg1);

void on_register_numberH_textEdited(const QString &arg1);

void on_register_numberL_textEdited(const QString &arg1);

void on_CRCL_textEdited(const QString &arg1);

void on_CRCH_textEdited(const QString &arg1);

void on_Open_addrfix_clicked();

void on_check_clicked();

void timer1_event();

void Display_inmessage();

QString to_hexString(unsigned char pBuf);

void on_alarm_value_textEdited(const QString &arg1);

void on_device_number_textEdited(const QString &arg1);

void on_error_range_textEdited(const QString &arg1);

void Message_analysis();

float hexToFloat(QString);

void data_limiter();

void input_message_change(QString);

void dispaly_date(QString,QString);
void on_check_2_clicked();

private:
Ui::TraverCOM *ui;
};
#endif // TRAVERCOM_H


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "travercom.h"


#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TraverCOM w;
w.show();
return a.exec();
}

mythread.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
#include "mythread.h"
#include <QMessageBox>
#include <QString>
#include "travercom.h"

MyThread::MyThread(TraverCOM * parent, QSerialPort *port)
{
this->parent = parent;//传入父对象,以便给它发信号
this->port = port;//传入串口对象,以便在线程中使用
}


unsigned int comlong=0;
unsigned char fbuf[1024]={0};//接收报文储存
extern unsigned int outCOMnumber;
extern unsigned char Message[8];
extern unsigned int device_number;
void MyThread::run()
{
unsigned char *buf;
unsigned int i;
buf = (unsigned char *)malloc(8);
//关联信号与槽,用于给父对象信号,告知通讯结果
buf[0] = Message[0];
buf[1] = Message[1];
buf[2] = Message[2];
buf[3] = Message[3];
buf[4] = Message[4];
buf[5] = Message[5];
buf[6] = Message[6];
buf[7] = Message[7];
port->write(reinterpret_cast<const char *>(buf), 8);//发送数据
port->waitForBytesWritten(200);
port->flush();//让数据立刻从串口发送出去,不要在缓冲区里墨迹
free(buf);
buf = NULL;
//检查串口接收到了多少数据
if((outCOMnumber>=0)&&(outCOMnumber<device_number))
{
outCOMnumber++;
Message[0]=outCOMnumber;
}else
{
outCOMnumber=0;
}
int num = static_cast<int>(port->bytesAvailable());
if(num < 6)
{
qDebug() << "未获取有效数据";
return;
}else
{
qDebug() << "获取有效数据";
QByteArray array = port->readAll();
port->waitForBytesWritten(200);
for(i=0;i<array.size();i++)
{
fbuf[i]=array[i];
}
comlong = array.size();
}
port->clear();
msleep(200);
}

travercom.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include "travercom.h"
#include "ui_travercom.h"
#include "mythread.h"
#include <QMessageBox>
#include <charconv>

TraverCOM::TraverCOM(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TraverCOM)
{
ui->setupUi(this);
Timer_1= new QTimer();
Timer_1->setInterval(500); //200ms
connect(Timer_1, SIGNAL(timeout()), this, SLOT(timer1_event()));
ui->tableWidget->setColumnWidth(0,114);
ui->tableWidget->setColumnWidth(1,114);
ui->tableWidget->setColumnWidth(2,114);
ui->tableWidget->setColumnWidth(3,114);
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget->setAlternatingRowColors(true);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:lightblue;}");
ui->tableWidget->verticalHeader()->setStyleSheet("QHeaderView::section{background:lightblue;}");
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //行头自适应表格
ui->alarm_value->setToolTip("范围为0-10000,单位MA");
ui->alarm_value->setPlaceholderText("MA");
ui->error_range->setPlaceholderText("%");
ui->device_number->setPlaceholderText("件");
}

TraverCOM::~TraverCOM()
{
delete ui;
}
//----以下为串口设置变量----
QString COMarg; //COM口
QString Baudarg; //波特率
QString bitarg; //停止位
//----以上为串口设置变量----
//----以下为报文设置变量----
unsigned char Message[8]; //八字节长度

//----以上为报文设置变量----
unsigned int alarm_value=0; //设置报警值
int device_number=10; //设置下发设备数量
unsigned int error_range=0; //设置误差值

QSerialPort port;
unsigned int outCOMnumber=0;
extern unsigned int comlong;
extern unsigned char fbuf[1024];//接收报文储存
MyThread *th;


float TraverCOM::hexToFloat(QString hexString)
{
uint hex_uint = hexString.toUInt(nullptr, 16);
float hex_res = *(float*)&hex_uint;
return hex_res;
}

//CRC校验
unsigned short out_CRC16(unsigned char *pBuf,unsigned short len)
{
unsigned short uCRC=0xFFFF;
int i,j;
for(i=0;i<len;i++)
{
uCRC^=(*(pBuf+i));
for(j=0;j<8;j++)
{
if((uCRC&0x0001)==0x0001)
{
uCRC=(uCRC>>1);
uCRC^=0xA001;
}
else
uCRC=(uCRC>>1);
}
}
return uCRC;
}

QString TraverCOM::to_hexString(unsigned char pBuf)
{
QString hexString;
unsigned char Message1;
Message1 = pBuf;
hexString = QString("%1 ").arg(static_cast<unsigned>(Message1), 2, 16, QChar('0')).toUpper();
return hexString;
}
int Char_int(QString arg1)//将两字节的字符串变成int型
{
char* buf;
QByteArray arg2;
unsigned char number[2];
unsigned int number2[2];
unsigned int COMnumber=0;
arg2=arg1.toLatin1();//
buf = arg2.data();//
for(int i=0;i<2;i++)
{
number[i] = *(buf+i);
if('0'==number[i])
{
number2[i]=0;
}else if ('1'==number[i])
{
number2[i]=1;
}else if ('2'==number[i])
{
number2[i]=2;
}else if ('3'==number[i])
{
number2[i]=3;
}else if ('4'==number[i])
{
number2[i]=4;
}else if ('5'==number[i])
{
number2[i]=5;
}else if ('6'==number[i])
{
number2[i]=6;
}else if ('7'==number[i])
{
number2[i]=7;
}else if ('8'==number[i])
{
number2[i]=8;
}else if ('9'==number[i])
{
number2[i]=9;
}else if ('A'==number[i])
{
number2[i]=10;
}else if ('B'==number[i])
{
number2[i]=11;
}else if ('C'==number[i])
{
number2[i]=12;
}else if ('D'==number[i])
{
number2[i]=13;
}else if ('E'==number[i])
{
number2[i]=14;
}else if ('F'==number[i])
{
number2[i]=15;
}else
{
number2[i]=0;
}
if(0==i)
{
number2[i]=number2[i]*0x10;
}
}
COMnumber = number2[0]+number2[1];
return COMnumber;
}

void TraverCOM::on_COM_search_clicked()
{
// 清除当前显示的端口号
ui->COM->clear();
//检索端口号
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())//QSerialPort需要头文件支持以及在.pro文件中添加QT +=serialport;
{
ui->COM->addItem(info.portName());//将获取的COM信息显示到下拉框
}
}

void TraverCOM::on_COM_currentTextChanged(const QString &arg1)
{
COMarg = arg1;
}

void TraverCOM::on_Baud_rate_currentTextChanged(const QString &arg1)
{
Baudarg = arg1;
}

void TraverCOM::on_stop_bit_currentTextChanged(const QString &arg1)
{
bitarg = arg1;
}

void TraverCOM::on_addrfix_textEdited(const QString &arg1)
{
if(2==arg1.length())
{
ui->addrfix->setMaxLength(2);
outCOMnumber = Char_int(arg1);
Message[0]=outCOMnumber;
}
}




void TraverCOM::on_function_code_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->function_code->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[1]=COMnumber;
}
}


void TraverCOM::on_register_addrH_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->register_addrH->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[2]=COMnumber;
}
}


void TraverCOM::on_register_addrL_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->register_addrL->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[3]=COMnumber;
}
}


void TraverCOM::on_register_numberH_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->register_numberH->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[4]=COMnumber;
}
}


void TraverCOM::on_register_numberL_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->register_numberL->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[5]=COMnumber;
}
}


void TraverCOM::on_CRCL_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->CRCL->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[6]=COMnumber;
}
}


void TraverCOM::on_CRCH_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(2==arg1.length())
{
ui->CRCH->setMaxLength(2);
COMnumber = Char_int(arg1);
Message[7]=COMnumber;
}
}
/*读取输入告警值
*input:char 告警值
*/
void TraverCOM::on_alarm_value_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(4>=arg1.length())
{
ui->alarm_value->setMaxLength(5);
COMnumber = arg1.toInt();
alarm_value=COMnumber;
}
}
/*读取输入设备数量
*input:char
*/
void TraverCOM::on_device_number_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(3>=arg1.length())
{
ui->device_number->setMaxLength(4);
COMnumber = arg1.toInt();
device_number=COMnumber;
}
}
/*读取输入误差范围
*input:char
*/
void TraverCOM::on_error_range_textEdited(const QString &arg1)
{
unsigned int COMnumber=0;
if(3>=arg1.length())
{
ui->error_range->setMaxLength(4);
COMnumber = arg1.toInt();
error_range=COMnumber;
}
}

unsigned int put_log=0;
void TraverCOM::on_Open_addrfix_clicked()
{
QString name = ui->Open_addrfix->text();//获取按钮上的文字
if(name == tr("读取数据"))
{
port.setPortName(ui->COM->currentText());//选择串口号
if( !port.open(QIODevice::ReadWrite))//打开串口
{
QMessageBox::information(this, tr("提示"),
tr("打开串口失败,请查看串口是否被占用"), QMessageBox::Ok);
return;
}
port.setBaudRate(ui->Baud_rate->currentText().toInt());
port.setDataBits(QSerialPort::Data8);//8位数据位
port.setParity(QSerialPort::NoParity);//无检验
if('1'==bitarg)
port.setStopBits(QSerialPort::OneStop);//1停止位
if('2'==bitarg)
port.setStopBits(QSerialPort::TwoStop);//2停止位
port.setFlowControl(QSerialPort::NoFlowControl);//无硬件控制
ui->Open_addrfix->setText(tr("关闭读取"));
//-------------------------------------------------
ui->COM->setDisabled(true);//串口号下拉列表变灰
ui->Baud_rate->setDisabled(true);//波特率下接列表变灰
ui->stop_bit->setDisabled(true);//停止位下拉列表变灰
ui->COM_search->setDisabled(true);
ui->check_2->setDisabled(true);
//-------------------------------------------------
ui->alarm_value->setDisabled(true);
ui->device_number->setDisabled(true);
ui->error_range->setDisabled(true);
//-------------------------------------------------
ui->addrfix->setDisabled(true);
ui->function_code->setDisabled(true);
ui->register_addrH->setDisabled(true);
ui->register_addrL->setDisabled(true);
ui->register_numberH->setDisabled(true);
ui->register_numberL->setDisabled(true);
ui->CRCH->setDisabled(true);
ui->CRCL->setDisabled(true);
//-------------------------------------------------
th = new MyThread(this, &port);//启动线程,在线程内通讯
Timer_1->start();
put_log = 1;
}else
{
port.close();//关闭串口
ui->Open_addrfix->setText(tr("读取数据"));
ui->COM->setEnabled(true);//串口号下拉列表变亮
ui->Baud_rate->setEnabled(true);//波特率下拉列表变亮
ui->stop_bit->setEnabled(true);//停止位下拉列表变亮
ui->COM_search->setEnabled(true);
ui->check_2->setEnabled(true);
//-------------------------------------------------
ui->alarm_value->setEnabled(true);
ui->device_number->setEnabled(true);
ui->error_range->setEnabled(true);
//-------------------------------------------------
ui->addrfix->setEnabled(true);
ui->function_code->setEnabled(true);
ui->register_addrH->setEnabled(true);
ui->register_addrL->setEnabled(true);
ui->register_numberH->setEnabled(true);
ui->register_numberL->setEnabled(true);
ui->CRCH->setEnabled(true);
ui->CRCL->setEnabled(true);
//-------------------------------------------------
Timer_1->stop();
put_log = 0;
}
while(device_number>ui->tableWidget->rowCount())
{
ui->tableWidget->insertRow(ui->tableWidget->rowCount());//在最后一行的后面插入一行
}
}
float Core=0,clamp=0;
int column=0,row=0;
void TraverCOM::on_check_clicked()
{
unsigned short CRC16;
CRC16=out_CRC16(Message,6);
Message[6] = CRC16%0x100;
Message[7] = CRC16/0x100;
ui->CRCL->setText(to_hexString(Message[6]));
ui->CRCH->setText(to_hexString(Message[7]));
}
uint RECIVENUMBER=1;
void TraverCOM::data_limiter()
{
}
void TraverCOM::Message_analysis()
{
QString hexStringCO=0,hexStringCL=0;
unsigned short CRC16=0;
unsigned char Message1[1024];
for (unsigned int i1 = 0; i1 < comlong; i1++ )
{
Message1[i1] = fbuf[i1];
}
qDebug("报文长度:%d",comlong);
CRC16=out_CRC16(Message1,comlong-2);
if(CRC16 == (Message1[comlong-1]*0x100+Message1[comlong-2]))
{
char ADDR[1]={0};
char CORE[4]={0};
char CLAMP[4]={0};
ADDR[0] = Message1[0];
int addr = Char_int(to_hexString(ADDR[0]));
if(addr != 0)
{
row = addr;
}
for(int numbercore=0;numbercore<4;numbercore++)
{
CORE[numbercore] = Message1[numbercore+3];
int a = Char_int(to_hexString(CORE[numbercore]));
if(a==0)
{
hexStringCO += QString("%1").arg(a,0,16);
hexStringCO += QString("%1").arg(a,0,16);
}
else
{
hexStringCO += QString("%1").arg(a,0,16);
}
}
for(int numberclamp=0;numberclamp<4;numberclamp++)
{
CLAMP[numberclamp] = Message1[numberclamp+7];
int a = Char_int(to_hexString(CLAMP[numberclamp]));
if(a==0)
{
hexStringCL += QString("%1").arg(a,0,16);
hexStringCL += QString("%1").arg(a,0,16);
}
else
{
hexStringCL += QString("%1").arg(a,0,16);
}
}
Core = hexToFloat(hexStringCO);
clamp = hexToFloat(hexStringCL);
for (unsigned int i1 = 0; i1 < comlong; i1++ )
{
fbuf[i1]=0;
}
}else
{
for (unsigned int i1 = 0; i1 < comlong; i1++ )
{
fbuf[i1]=0;
}
}
}
void TraverCOM::dispaly_date(QString hexString,QString state_local)
{
if(row != 0)
{
ui->tableWidget->setItem(row-1,column,new QTableWidgetItem(hexString));
// ui->tableWidget->setItem(row-1,column+1,new QTableWidgetItem(state_local));
ui->tableWidget->setItem(row-1,column+1,new QTableWidgetItem(QString::number(Core)));
ui->tableWidget->setItem(row-1,column+2,new QTableWidgetItem(QString::number(clamp)));
}
else
{
ui->tableWidget->setItem(row,column,new QTableWidgetItem(hexString));
// ui->tableWidget->setItem(row,column+1,new QTableWidgetItem(state_local));
ui->tableWidget->setItem(row,column+1,new QTableWidgetItem(QString::number(Core)));
ui->tableWidget->setItem(row,column+2,new QTableWidgetItem(QString::number(clamp)));
}
}
int alarm_row_red,alarm_row_green;
void TraverCOM::input_message_change(QString hexString)
{
float alarm_value_local = alarm_value;
float limiter_number_H=0;
float limiter_number_L=0;
float error_range_local = error_range;
limiter_number_H =alarm_value_local+(alarm_value_local*(error_range_local/100.00));
limiter_number_L =alarm_value_local-(alarm_value_local*(error_range_local/100.00));
qDebug("limiter_number_H:%f",limiter_number_H);
qDebug("limiter_number_L:%f",limiter_number_L);
if(Core>limiter_number_H || clamp>limiter_number_H || Core<limiter_number_L || clamp<limiter_number_L)
{
alarm_row_red = row;
dispaly_date(hexString,"告警");
QTableWidgetItem *item = new QTableWidgetItem();
item->setBackground(QColor(Qt::red));
ui->tableWidget->setItem(alarm_row_red-1, 3, item);
}
else
{
alarm_row_green= row;
dispaly_date(hexString,"正常");
QTableWidgetItem *item = new QTableWidgetItem();
item->setBackground(QColor(Qt::green));
ui->tableWidget->setItem(alarm_row_green-1, 3, item);
}

}

void TraverCOM::Display_inmessage()
{
if(comlong !=0)
{
QString hexString,hexString1;
unsigned char Message1[1024];
QFont font;
font.setPointSize(22); // 设置字体大小为22
font.setBold(true); // 设置字体加粗
for (unsigned int i1 = 0; i1 < comlong; i1++ )
{
Message1[i1] = fbuf[i1];
}
hexString = QString("%1 ").arg(static_cast<unsigned>(Message1[0]), 2, 16, QChar('0')).toUpper();
Message_analysis();
data_limiter();
input_message_change(hexString);
comlong = 0;
}
}

void TraverCOM::timer1_event()
{
on_check_clicked();
ui->addrfix->setText(to_hexString(Message[0]));
th->start();
Display_inmessage();
th->exit();
th->wait();
}

void TraverCOM::on_check_2_clicked()
{
ui->tableWidget->clearContents();
row = 0;
}

tarvercom.ui

图片

UI界面结构

UI界面的上下关系

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
TraverCOM
|-frame_2
|-Baud_rate
|-COM
|-COM_search
|-CRCH
|-CRCL
|-Open_addrfix
|-addrfix
|-alarm_value
|-check
|-check_2
|-device_number
|-error_range
|-function_code
|-label
|-label_13
|-label_14
|-label_15
|-label_16
|-label_2
|-label_3
|-label_4
|-label_5
|-label_6
|-label_7
|-register_addrH
|-register_addrL
|-register_numberH
|-register_numberL
|-stop_bit
|-tableWidget

控件位置

每个控件在UI界面的位置

TraverCOM

图片

frame_2

图片

Baud_rate

图片

COM

图片

图片

CRCH

图片

CRCL

图片

Open_addrfix

图片

addrfix

图片

alarm_value

图片

check

图片

device_number

图片

error_range

图片

function_code

图片

register_addrH

图片

register_addrL

图片

register_numberH

图片

register_numberL

图片

stop_bit

图片

tableWidget

图片

总结

小程序是在原来的设备地址遍历小程序的基础上修改而来,添加了数据解析以及展示部分,删除原有的报文展示部分。
是地址遍历小程序在铁心数据解析方向的特化。