myhelper.h
21.5 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
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <QtCore>
#include <QtGui>
#include <QtSql>
#include <QtNetwork>
#include <QtXml>
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#define TIMEMS qPrintable (QTime::currentTime().toString("HH:mm:ss zzz"))
#define TIME qPrintable (QTime::currentTime().toString("HH:mm:ss"))
#define QDATE qPrintable (QDate::currentDate().toString("yyyy-MM-dd"))
#define QTIME qPrintable (QTime::currentTime().toString("HH-mm-ss"))
#define DATETIME qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
#define STRDATETIME qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))
#define STRDATETIMEMS qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss-zzz"))
#define AppName "NetTool"
#define AppPath qApp->applicationDirPath()
#define AppDeskWidth qApp->desktop()->availableGeometry().width()
#define AppDeskHeight qApp->desktop()->availableGeometry().height()
#ifndef MYHELPER_H
#define MYHELPER_H
static uint32_t crc32_table[256];
class myHelper: public QObject
{
public:
//设置为开机启动
static void runWithSystem(bool autoRun, QString strName, QString strPath)
{
QSettings *reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
if (autoRun) {
reg->setValue(strName, strPath);
} else {
reg->setValue(strName, "");
}
}
static void setCode()
{
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#endif
}
//设置指定样式
static void setStyle(const QString &qssFile)
{
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
QString qss = QLatin1String(file.readAll());
qApp->setStyleSheet(qss);
QString PaletteColor = qss.mid(20, 7);
qApp->setPalette(QPalette(QColor(PaletteColor)));
file.close();
}
}
//延时
static void sleep(int sec)
{
QTime dieTime = QTime::currentTime().addMSecs(sec);
while (QTime::currentTime() < dieTime) {
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
}
//窗体居中显示
static void formInCenter(QWidget *frm)
{
int frmX = frm->width();
int frmY = frm->height();
QDesktopWidget w;
int deskWidth = w.availableGeometry().width();
int deskHeight = w.availableGeometry().height();
QPoint movePoint(deskWidth / 2 - frmX / 2, deskHeight / 2 - frmY / 2);
frm->move(movePoint);
}
//设置系统日期时间
static void setSystemDateTime(QString year, QString month, QString day, QString hour, QString min, QString sec)
{
#ifdef Q_OS_WIN
QProcess p(0);
p.start("cmd");
p.waitForStarted();
p.write(QString("date %1-%2-%3\n").arg(year).arg(month).arg(day).toLatin1());
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
p.start("cmd");
p.waitForStarted();
p.write(QString("time %1:%2:%3.00\n").arg(hour).arg(min).arg(sec).toLatin1());
p.closeWriteChannel();
p.waitForFinished(1000);
p.close();
#else
QString cmd = QString("date %1%2%3%4%5.%6").arg(month).arg(day).arg(hour).arg(min).arg(year).arg(sec);
system(cmd.toLatin1());
system("hwclock -w");
#endif
}
static QString byteArrayToHexStr(QByteArray data)
{
QString temp = "";
QString hex = data.toHex();
for (int i = 0; i < hex.length(); i = i + 2) {
temp += hex.mid(i, 2) + " ";
}
return temp.trimmed().toUpper();
}
static QString byteArrayToAsciiStr(QByteArray data)
{
QString temp;
int len = data.size();
for (int i = 0; i < len; i++) {
//0x20为空格,空格以下都是不可见字符
char b = data.at(i);
if (0x00 == b) {
temp += QString("\\NUL");
} else if (0x01 == b) {
temp += QString("\\SOH");
} else if (0x02 == b) {
temp += QString("\\STX");
} else if (0x03 == b) {
temp += QString("\\ETX");
} else if (0x04 == b) {
temp += QString("\\EOT");
} else if (0x05 == b) {
temp += QString("\\ENQ");
} else if (0x06 == b) {
temp += QString("\\ACK");
} else if (0x07 == b) {
temp += QString("\\BEL");
} else if (0x08 == b) {
temp += QString("\\BS");
} else if (0x09 == b) {
temp += QString("\\HT");
} else if (0x0A == b) {
temp += QString("\\LF");
} else if (0x0B == b) {
temp += QString("\\VT");
} else if (0x0C == b) {
temp += QString("\\FF");
} else if (0x0D == b) {
temp += QString("\\CR");
} else if (0x0E == b) {
temp += QString("\\SO");
} else if (0x0F == b) {
temp += QString("\\SI");
} else if (0x10 == b) {
temp += QString("\\DLE");
} else if (0x11 == b) {
temp += QString("\\DC1");
} else if (0x12 == b) {
temp += QString("\\DC2");
} else if (0x13 == b) {
temp += QString("\\DC3");
} else if (0x14 == b) {
temp += QString("\\DC4");
} else if (0x15 == b) {
temp += QString("\\NAK");
} else if (0x16 == b) {
temp += QString("\\SYN");
} else if (0x17 == b) {
temp += QString("\\ETB");
} else if (0x18 == b) {
temp += QString("\\CAN");
} else if (0x19 == b) {
temp += QString("\\EM");
} else if (0x1A == b) {
temp += QString("\\SUB");
} else if (0x1B == b) {
temp += QString("\\ESC");
} else if (0x1C == b) {
temp += QString("\\FS");
} else if (0x1D == b) {
temp += QString("\\GS");
} else if (0x1E == b) {
temp += QString("\\RS");
} else if (0x1F == b) {
temp += QString("\\US");
} else if (0x7F == b) {
temp += QString("\\x7F");
} else if (0x5C == b) {
temp += QString("\\x5C");
} else if (0x20 >= b) {
temp += QString("\\x%1").arg(decimalToStrHex((quint8)b));
} else {
temp += QString("%1").arg(b);
}
}
return temp.trimmed();
}
static QByteArray hexStrToByteArray(QString str)
{
QByteArray senddata;
int hexdata, lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len / 2);
char lstr, hstr;
for (int i = 0; i < len;) {
hstr = str.at(i).toLatin1();
if (hstr == ' ') {
i++;
continue;
}
i++;
if (i >= len) {
break;
}
lstr = str.at(i).toLatin1();
hexdata = convertHexChar(hstr);
lowhexdata = convertHexChar(lstr);
if ((hexdata == 16) || (lowhexdata == 16)) {
break;
} else {
hexdata = hexdata * 16 + lowhexdata;
}
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
return senddata;
}
static QByteArray asciiStrToByteArray(QString str)
{
QByteArray buffer;
int len = str.length();
QString letter;
QString hex;
for (int i = 0; i < len; i++) {
letter = str.at(i);
if (letter == "\\") {
i++;
letter = str.mid(i, 1);
if (letter == "x") {
i++;
hex = str.mid(i, 2);
buffer.append(strHexToDecimal(hex));
i++;
continue;
} else if (letter == "N") {
i++;
hex = str.mid(i, 1);
if (hex == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "L") { //NUL=0x00
buffer.append((char)0x00);
continue;
}
} else if (hex == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "K") { //NAK=0x15
buffer.append(0x15);
continue;
}
}
} else if (letter == "S") {
i++;
hex = str.mid(i, 1);
if (hex == "O") {
i++;
hex = str.mid(i, 1);
if (hex == "H") { //SOH=0x01
buffer.append(0x01);
continue;
} else { //SO=0x0E
buffer.append(0x0E);
i--;
continue;
}
} else if (hex == "T") {
i++;
hex = str.mid(i, 1);
if (hex == "X") { //STX=0x02
buffer.append(0x02);
continue;
}
} else if (hex == "I") { //SI=0x0F
buffer.append(0x0F);
continue;
} else if (hex == "Y") {
i++;
hex = str.mid(i, 1);
if (hex == "N") { //SYN=0x16
buffer.append(0x16);
continue;
}
} else if (hex == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "B") { //SUB=0x1A
buffer.append(0x1A);
continue;
}
}
} else if (letter == "E") {
i++;
hex = str.mid(i, 1);
if (hex == "T") {
i++;
hex = str.mid(i, 1);
if (hex == "X") { //ETX=0x03
buffer.append(0x03);
continue;
} else if (hex == "B") { //ETB=0x17
buffer.append(0x17);
continue;
}
} else if (hex == "O") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //EOT=0x04
buffer.append(0x04);
continue;
}
} else if (hex == "N") {
i++;
hex = str.mid(i, 1);
if (hex == "Q") { //ENQ=0x05
buffer.append(0x05);
continue;
}
} else if (hex == "M") { //EM=0x19
buffer.append(0x19);
continue;
} else if (hex == "S") {
i++;
hex = str.mid(i, 1);
if (hex == "C") { //ESC=0x1B
buffer.append(0x1B);
continue;
}
}
} else if (letter == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "K") { //ACK=0x06
buffer.append(0x06);
continue;
}
}
} else if (letter == "B") {
i++;
hex = str.mid(i, 1);
if (hex == "E") {
i++;
hex = str.mid(i, 1);
if (hex == "L") { //BEL=0x07
buffer.append(0x07);
continue;
}
} else if (hex == "S") { //BS=0x08
buffer.append(0x08);
continue;
}
} else if (letter == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "R") { //CR=0x0D
buffer.append(0x0D);
continue;
} else if (hex == "A") {
i++;
hex = str.mid(i, 1);
if (hex == "N") { //CAN=0x18
buffer.append(0x18);
continue;
}
}
} else if (letter == "D") {
i++;
hex = str.mid(i, 1);
if (hex == "L") {
i++;
hex = str.mid(i, 1);
if (hex == "E") { //DLE=0x10
buffer.append(0x10);
continue;
}
} else if (hex == "C") {
i++;
hex = str.mid(i, 1);
if (hex == "1") { //DC1=0x11
buffer.append(0x11);
continue;
} else if (hex == "2") { //DC2=0x12
buffer.append(0x12);
continue;
} else if (hex == "3") { //DC3=0x13
buffer.append(0x13);
continue;
} else if (hex == "4") { //DC2=0x14
buffer.append(0x14);
continue;
}
}
} else if (letter == "F") {
i++;
hex = str.mid(i, 1);
if (hex == "F") { //FF=0x0C
buffer.append(0x0C);
continue;
} else if (hex == "S") { //FS=0x1C
buffer.append(0x1C);
continue;
}
} else if (letter == "H") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //HT=0x09
buffer.append(0x09);
continue;
}
} else if (letter == "L") {
i++;
hex = str.mid(i, 1);
if (hex == "F") { //LF=0x0A
buffer.append(0x0A);
continue;
}
} else if (letter == "G") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //GS=0x1D
buffer.append(0x1D);
continue;
}
} else if (letter == "R") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //RS=0x1E
buffer.append(0x1E);
continue;
}
} else if (letter == "U") {
i++;
hex = str.mid(i, 1);
if (hex == "S") { //US=0x1F
buffer.append(0x1F);
continue;
}
} else if (letter == "V") {
i++;
hex = str.mid(i, 1);
if (hex == "T") { //VT=0x0B
buffer.append(0x0B);
continue;
}
} else if (letter == "\\") {
//如果连着的是多个\\则对应添加\对应的16进制0x5C
buffer.append(0x5C);
continue;
} else {
//将对应的\[前面的\\也要加入
buffer.append(0x5C);
buffer.append(letter.toLatin1());
continue;
}
}
buffer.append(str.mid(i, 1).toLatin1());
}
return buffer;
}
static char convertHexChar(char ch)
{
if ((ch >= '0') && (ch <= '9')) {
return ch - 0x30;
} else if ((ch >= 'A') && (ch <= 'F')) {
return ch - 'A' + 10;
} else if ((ch >= 'a') && (ch <= 'f')) {
return ch - 'a' + 10;
} else {
return (0);
}
}
static QString decimalToStrHex(int decimal)
{
QString temp = QString::number(decimal, 16);
if (temp.length() == 1) {
temp = "0" + temp;
}
return temp.toUpper();
}
static int strHexToDecimal(QString strHex)
{
bool ok;
return strHex.toInt(&ok, 16);
}
static void String2Hex(QString str, QByteArray &senddata)
{
int hexdata,lowhexdata;
int hexdatalen = 0;
int len = str.length();
senddata.resize(len/2);
char lstr,hstr;
for(int i=0; i<len; )
{
//char lstr,
hstr=str[i].toLatin1();
if(hstr == ' ')
{
i++;
continue;
}
i++;
if(i >= len)
break;
lstr = str[i].toLatin1();
hexdata = ConvertHexChar(hstr);
lowhexdata = ConvertHexChar(lstr);
if((hexdata == 16) || (lowhexdata == 16))
break;
else
hexdata = hexdata*16+lowhexdata;
i++;
senddata[hexdatalen] = (char)hexdata;
hexdatalen++;
}
senddata.resize(hexdatalen);
}
static char ConvertHexChar(char ch)
{
if((ch >= '0') && (ch <= '9'))
return ch-0x30;
else if((ch >= 'A') && (ch <= 'F'))
return ch-'A'+10;
else if((ch >= 'a') && (ch <= 'f'))
return ch-'a'+10;
else return (-1);
}
// typedef signed char int8_t;
// typedef unsigned char uint8_t;
// typedef short int16_t;
// typedef unsigned short uint16_t;
// typedef int int32_t;
// typedef unsigned uint32_t;
static uint16_t data_conver_big_small(uint16_t a)
{
union
{
uint16_t i;
uint8_t c[2];
} u, r;
u.i = a;
r.c[0] = u.c[1];
r.c[1] = u.c[0];
return r.i;
}
static uint32_t data_conver_big_small(uint32_t a)
{
union
{
uint32_t i;
uint8_t c[4];
} u, r;
u.i = a;
r.c[0] = u.c[3];
r.c[1] = u.c[2];
r.c[2] = u.c[1];
r.c[3] = u.c[0];
return r.i;
}
// static float data_conver_big_small(float a)
// {
// union
// {
// float i;
// char c[4];
// } u, r;
// u.i = a;
// r.c[0] = u.c[3];
// r.c[1] = u.c[2];
// r.c[2] = u.c[1];
// r.c[3] = u.c[0];
// return r.i;
// }
static void crc32_table_init(uint32_t crc)
{
uint32_t crc_value;
uint16_t table_index;
uint8_t bit_index;
for(table_index = 0; table_index < 256; table_index++)
{
crc_value = (unsigned int)table_index;
for(bit_index = 0; bit_index < 8; bit_index++)
{
if(crc_value&1)
{
crc_value = (crc_value>>1)^crc;
}
else
{
crc_value = crc_value>>1;
}
}
crc32_table[table_index] = crc_value;
}
}
static uint32_t crc32_cal(uint8_t *ptr, uint32_t len)
{
uint32_t index;
uint32_t crc;
crc = 0xffffffff;
myHelper::crc32_table_init(0xEDB88320);
for(index = 0;index < len; index++)
{
crc = (crc>>8)^(crc32_table[(crc^ptr[index])&0xff]);
}
crc = ~crc;
return crc;
}
}; //end myHelper
#endif // MYHELPER_H