-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserialtest.cpp
1777 lines (1479 loc) · 55.6 KB
/
serialtest.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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "serialtest.h"
#include<iostream>
#include <QSerialPortInfo>
#include <QQmlComponent>
#include <QDebug>
#include <string>
#include <QDateTime>
#include <QQmlProperty>
#include <stdio.h>
#include <QPointF>
#include <QTimer>
#include <QtCore>
#include<stdlib.h>
#include <math.h>
SerialTest::Settings currentsetting;//定义设定值结构体的结构体变量
QSerialPort *serialtest;
QString m_serialdataall("");
qint64 mag_corner(0);
qint64 mag_x(0);
qint64 mag_y(0);
qint64 mag_z(0);
qint64 mag_user1(0);
qint64 mag_user2(0);
qint64 mag_user3(0);
qint64 mag_user4(0);
qint64 mag_user5(0);
qint64 mag_user6(0);
QString mag_cornerStr("0\xc2\xb0 \xe5\x8c\x97");
QString m_serialSaveAndApp("");
static qint64 c_sendnumber,c_receivenumber;
static bool settingAddrFlag = false;
static int addr1 = 0;
static int addr2 = 0;
static int addr3 = 0;
static int addrch = 0;
static bool serialOpenFlag = false;
static bool serialDrawClearFlagMag = false;
static bool serialDrawClearFlagIMU = false;
static bool serialDrawClearFlagDrone = false;
static bool serialDrawClearFlagBaro = false;
//static QTimer timerSend;
static bool SerialTestInited = false;
static double currentLon = 0, currentLat = 0;
static int currentNorthSpeed = 0, currentEastSpeed = 0, currentSatelliteNum = 0, currentHdop = 0;
//static QThread * threadSerialPort;
static int StepMotorAngle=0, StepMotorSpeed=0;//fcg
static int BDCMotorAngle=0, BDCMotorSpeed=0;//fcg
//==========================================ace for esc
static int ReadESC_Thro=0, ReadESC_Speed=0,Read_ESCHall=0;//ace
//===========================================ace for esc
static int ACCX = 0, ACCY = 0, ACCZ = 0, GYROX = 0, GYROY = 0, GYROZ = 0;
static double Angle_Acc_X = 0, Angle_Acc_Y;
static int Heigth_Ultrasonic_lowpass = 0, Heigth_Ultrasonic_nofliter = 0;
static double Heigth_Ultrasonic_time = 0;
static double Heigth_Ultrasonic_alpha = 0;
static double anglepitch = 0, angleroll = 0, angleyaw = 0;
static double altitude = 0;
static int TIMEcnt = 0;
static int TIMEINT = 0;
static int pressureRaw = 0, pressureFilter = 0;
static int slideDepth = 8;
static int pressureHeight = 0;
static int opticalflowspeedx = 0, opticalflowspeedy = 0;
static int opticalflowsumx = 0, opticalflowsumy = 0;
static bool serialSendRequest = true;
static int flag_currentPage = 0;//标识符,指示当前页面序号
//1:加速度计模组界面
//2:陀螺仪模组界面
//3:GPS模组界面
//4:气压计模组界面
//5:超声波模组界面
//6:电子罗盘模组界面
//7:无人机整机界面
//8:人脸识别界面
//9:人形跟随界面
//10:姿态识别界面
//11:光流
//12: 轴臂电机
//13: 直流有刷电机
//14: 无刷电调/机器车电机
//15: 演示页面
SerialTest::SerialTest(QSerialPort *parent):
QSerialPort (parent),
m_receivedata("Receive Label"),
m_receivenumber("0"),
m_sendnumber("0")
{
if(false == SerialTestInited)
{
SerialTestInited = true;
serialtest = new QSerialPort;
// QTimer *timerSend = new QTimer(this);
// timerSend.setInterval(50);
// timerSend.setTimerType(Qt::TimerType::PreciseTimer);
// QObject::connect(&timerSend, SIGNAL(timeout()), this, SLOT(timersendtimeout()));
QObject::connect(serialtest, SIGNAL(readyRead()),this, SLOT(receivefrom()));//将端口收到数据产生的信号绑定receivefrom()函数;
// threadSerialPort = new QThread(this);
// serialtest->moveToThread(threadSerialPort);
// threadSerialPort->start();
}
}
//打开端口并设置:函数的参数(……Index由qml中combobox的currentIndex决定),由按钮触发
void SerialTest::openAndSetPort(QString PortName,
int BaudRateIndex,
int DatabitsIndex,
int ParityIndex,
int StopbitsIndex,
int FlowcontrolIndex)
{
////////////////////1.得到当前选择的各项设置//////////////////////////////
//得到当前端口名
// QString allname[6]={"COM1","COM2","COM3","COM4","COM5","COM6"};//列举所有的端口名
// currentsetting.name=allname[PortNameIndex];//由qml里表示name的combobox的currentIndex来确定当前的name
currentsetting.name=PortName;
std::cout<<" ok setPortName to "+ currentsetting.name.toStdString()<< std::endl;//通过输出来验证设定成功
//得到当前波特率
qint32 allbauRate[4]={9600,19200,38400,115200};
currentsetting.baudRate=allbauRate[BaudRateIndex];
//得到当前发送位数
QSerialPort::DataBits allDatabits[4]={QSerialPort::Data5,
QSerialPort::Data6,
QSerialPort::Data7,
QSerialPort::Data8};
currentsetting.dataBits=allDatabits[DatabitsIndex];
//得到当前Parity
QSerialPort::Parity allparity[5]={QSerialPort::NoParity,
QSerialPort::EvenParity,
QSerialPort::OddParity,
QSerialPort::MarkParity,
QSerialPort::SpaceParity};
currentsetting.parity=allparity[ParityIndex];
//得到当前停止位
QSerialPort::StopBits allstopBits[3]={QSerialPort::OneStop,
QSerialPort::OneAndHalfStop,
QSerialPort::TwoStop};
currentsetting.stopBits=allstopBits[StopbitsIndex];
//得到当前FlowControl
QSerialPort::FlowControl allflowControl[3]={QSerialPort::NoFlowControl,
QSerialPort::HardwareControl,
QSerialPort::SoftwareControl};
currentsetting.flowControl=allflowControl[FlowcontrolIndex];
////////////////////2.设定当前端口名//////////////////////////////
serialtest->setPortName(currentsetting.name);
////////////////////3.打开这一端口并按照当前设置信息进行设置//////////////////////////////
if (serialtest->open(QIODevice::ReadWrite))//打开这一端口
{
std::cout<<"open port sucess"<<std::endl;
if(serialtest->setBaudRate(currentsetting.baudRate)//设置各项信息
&& serialtest->setDataBits(currentsetting.dataBits)
&& serialtest->setParity(currentsetting.parity)
&& serialtest->setStopBits(currentsetting.stopBits)
&& serialtest->setFlowControl(currentsetting.flowControl))
{
std::cout<<"set sucess"<<std::endl;
}
serialDrawClearFlagMag = true;
serialDrawClearFlagIMU = true;
serialDrawClearFlagDrone = true;
serialDrawClearFlagBaro = true;
serialOpenFlag = true;
clearSerialDataAll();
QString pagenumber = QString("%1").arg(flag_currentPage, 2, 16, QLatin1Char( '0' ));
sendCMD("2c", "808080800800", pagenumber+"00000000000000");
// timerSend.start();
}
}
void SerialTest::getPortInfo()
{
m_portInfo.clear();
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos){
std::cout<<" port name" + info.portName().toStdString()<<std::endl;
m_portInfo.append(info.portName());
}
}
//构建和发送无人机串口通信发送帧
void SerialTest::DroneFrame_MakeAndSerialSend(quint8 Realdata[4],quint8 Realstatus[2],quint8 Command,quint8 CommandData[8],quint8 Heartbeat)
{
quint8 temp_uint8;
//常整形转换为Qstring形
// long a =63;
// QString str=QString::number(a,16); //str="3f";
// QString str=QString::number(a,16).toUpper(); //str="3F";
QString StrDroneFrame;//无人机串口通信发送帧
QString StrFramehead = "ff55";//帧头
QString StrRealdata;
for(temp_uint8=0;temp_uint8<4;temp_uint8++)
{
StrRealdata+=QString::number(Realdata[temp_uint8],16);//实时数据
}
QString StrRealstatus;
for(temp_uint8=0;temp_uint8<2;temp_uint8++)
{
StrRealstatus+=QString::number(Realstatus[temp_uint8],16);//实时状态
}
QString StrCommand=QString::number(Command,16); //功能字
QString StrCommandData;
for(temp_uint8=0;temp_uint8<8;temp_uint8++)
{
StrCommandData+=QString::number(CommandData[temp_uint8],16);//功能字数据
}
QString StrHeartbeat=QString::number(Heartbeat,16); //心跳包
StrDroneFrame=StrFramehead+StrRealdata+StrRealstatus+StrCommand+StrCommandData+StrHeartbeat;
std::cout<<" StrDroneFrame:" + StrDroneFrame.toStdString()<<std::endl;
// sendto(StrDroneFrame);
}
void SerialTest::setRFaddr(QString addr1,QString addr2,QString addr3,QString channel)
{
// QString straddr1 = QString("%1").arg(addr1, 2, 16, QLatin1Char( '0' ));
// QString straddr2 = QString("%1").arg(addr2, 2, 16, QLatin1Char( '0' ));
// QString straddr3 = QString("%1").arg(addr3, 2, 16, QLatin1Char( '0' ));
// QString strchannel = QString("%1").arg(channel, 2, 16, QLatin1Char( '0' ));
// command += straddr1;
// command += straddr2;
// command += straddr3;
// command += straddr1;
// command += straddr2;
// command += straddr3;
// command += strchannel;
// command += "0000";
// QString command = "ff5580808080080029";
QString command = "";
QString straddr1 ;
QString straddr2 ;
QString straddr3 ;
QString strchannel;
bool ok;
quint8 dec;
dec=addr1.toInt(&ok,10); //dec=255 ; ok=rue
straddr1 = QString("%1").arg(dec, 2, 16, QLatin1Char( '0' ));
dec=addr2.toInt(&ok,10); //dec=255 ; ok=rue
straddr2 = QString("%1").arg(dec, 2, 16, QLatin1Char( '0' ));
dec=addr3.toInt(&ok,10); //dec=255 ; ok=rue
straddr3 = QString("%1").arg(dec, 2, 16, QLatin1Char( '0' ));
dec=channel.toInt(&ok,10); //dec=255 ; ok=rue
strchannel = QString("%1").arg(dec, 2, 16, QLatin1Char( '0' ));
command += straddr1;
command += straddr2;
command += straddr3;
command += straddr3;
command += straddr2;
command += straddr1;
command += strchannel;
command += "00";
// std::cout<<" value:" + command.toStdString()<<std::endl;
// sendto(command);
sendCMD("29", "808080800800", command);
}
void SerialTest::setCurrentPage(int current_page)
{
flag_currentPage=current_page;
}
int SerialTest::getCurrentPage(void)
{
return flag_currentPage;
}
void SerialTest::setUltrasonicLowpassfliter(QString value_freq,QString value_confficent)
{
QString command = "";
QString strvalue_freq;
QString stvalue_confficent;
bool ok;
int dec;
float temp_float;
dec=value_freq.toInt(&ok,10);
if(0!=dec)
{
dec=1000/dec;//转换为周期,单位是ms
if(dec>1000)
{
dec=1000;
}
else if(dec<10)
{
dec=10;
}
}
else {
dec=50;
}
strvalue_freq = QString("%1").arg(dec, 4, 16, QLatin1Char( '0' ));
temp_float=value_confficent.toFloat();
// temp_float=value_confficent.toDouble();
temp_float=temp_float*10000;
dec=temp_float;
if(dec>9999)
{
dec=9999;
}
else if(dec<1)
{
dec=1;
}
stvalue_confficent = QString("%1").arg(dec, 4, 16, QLatin1Char( '0' ));
command += strvalue_freq;
command += stvalue_confficent;
command += "0000000000";
// std::cout<<" value" + command.toStdString()<<std::endl;
sendCMD("2e", "808080800800", command);
}
void SerialTest::setLabAngle(QString angle_Roll,QString angle_Yaw)
{
// QString command = "ff55000000000000280000";
QString command = "0000";
QString strangle_Roll;
QString strangle_Yaw;
bool ok;
int dec;
dec=angle_Roll.toInt(&ok,10); //dec=255 ; ok=rue
strangle_Roll = QString("%1").arg(dec, 4, 16, QLatin1Char( '0' ));
dec=angle_Yaw.toInt(&ok,10); //dec=255 ; ok=rue
strangle_Yaw = QString("%1").arg(dec, 4, 16, QLatin1Char( '0' ));
if(strangle_Roll.length() >4){
command += strangle_Roll.mid(12,4);
}else{
command += strangle_Roll;
}
command += strangle_Yaw;
command += "000000";
// std::cout<<" value" + command.toStdString()<<std::endl;
sendCMD("28", "808080800800", command);
}
void SerialTest::setMagCorner(int yawValue,int rollValue)
{
QString yaw = QString("%1").arg(yawValue, 4, 16, QLatin1Char( '0' ));
QString roll = QString("%1").arg(rollValue, 4, 16, QLatin1Char( '0' ));
// QString command = "ff55000020000000280000";
QString command = "0000";
if(roll.length() >4){
command += roll.mid(12,4);
}else{
command += roll;
}
command += yaw;
command += "000000";
// std::cout<<" value" + command.toStdString()<<std::endl;
// sendto(command);
sendCMD("28", "808080800800", command);
}
void SerialTest::setPlatformYawAngle(int yawValue,int yawRateValue)
{
QString yaw = QString("%1").arg(yawValue, 4, 16, QLatin1Char( '0' ));
QString yawrate = QString("%1").arg(yawRateValue, 2, 16, QLatin1Char( '0' ));
QString command1 = "0000";
command1 += yawrate;
command1 += "000000";
QString command2 = "00000000";
command2 += yaw;
command2 += "0000";
// std::cout<<" value" + command.toStdString()<<std::endl;
// sendto(command);
sendCMD("28", command1, command2);
}
void SerialTest::setPlatformRollAngle(int rollValue,int rollRateValue)
{
QString roll = QString("%1").arg(rollValue, 4, 16, QLatin1Char( '0' ));
QString rollrate = QString("%1").arg(rollRateValue, 2, 16, QLatin1Char( '0' ));
QString command1 = "00";
command1 += rollrate;
command1 += "00000000";
QString command2 = "0000";
if(roll.length() >4){
command2 += roll.mid(12,4);
}else{
command2 += roll;
}
command2 += "00000000";
// std::cout<<" value" + command2.toStdString()<<std::endl;
// sendto(command);
sendCMD("28", command1, command2);
}
void SerialTest:: setBaroSlideDepth(int slidedepth)
{
QString yawrateStr = QString("%1").arg(slidedepth, 2, 16, QLatin1Char( '0' ));
QString command1 = "808080800800";
QString command2 = "";
command2 += yawrateStr;
command2 += "00000000000000";
// std::cout<<" value" + command.toStdString()<<std::endl;
// sendto(command);
sendCMD("31", command1, command2);
}
void SerialTest:: setOptDistance(int distance)
{
QString distanceStr = QString("%1").arg(distance, 2, 16, QLatin1Char( '0' ));
QString command1 = "808080800800";
QString command2 = "";
command2 += distanceStr;
command2 += "00000000000000";
// std::cout<<" value" + command.toStdString()<<std::endl;
// sendto(command);
sendCMD("30", command1, command2);
}
//stepmotor:fcg=====================================================================================
void SerialTest::setPlatformInfo(int number,int motorHz,int motorSpeed,int motorAngle,int accSpeed)
{
QString number1 = QString("%1").arg(number, 2, 16, QLatin1Char( '0' ));
QString motorHz1 = QString("%1").arg(motorHz, 4, 16, QLatin1Char( '0' ));
QString motorSpeed1 = QString("%1").arg(motorSpeed, 2, 16, QLatin1Char( '0' ));
QString motorAngle1 = QString("%1").arg(motorAngle, 4, 16, QLatin1Char( '0' ));
QString accSpeed1 = QString("%1").arg(accSpeed, 2, 16, QLatin1Char( '0' ));
QString command1 = "000000000000";
// command1 += motorAngle1;
// command1 += motorSpeed1;
QString command2 = "";
command2 += motorAngle1;
command2 += motorSpeed1;
command2 += accSpeed1;
command2 += number1;
command2 += motorHz1;
command2 +="00";
//command2 += "0000";
sendCMD("32", command1, command2);
}
//BDCmotor:fcg=====================================================================================
void SerialTest::setBDCPlatformInfo(int number,int motorHz,int motorSpeed,int motorAngle,int accSpeed)
{
QString number1 = QString("%1").arg(number, 2, 16, QLatin1Char( '0' ));
QString motorHz1 = QString("%1").arg(motorHz, 4, 16, QLatin1Char( '0' ));
QString motorSpeed1 = QString("%1").arg(motorSpeed, 2, 16, QLatin1Char( '0' ));
QString motorAngle1 = QString("%1").arg(motorAngle, 4, 16, QLatin1Char( '0' ));
QString accSpeed1 = QString("%1").arg(accSpeed, 2, 16, QLatin1Char( '0' ));
QString command1 = "000000000000";
// command1 += motorAngle1;
// command1 += motorSpeed1;
QString command2 = "";
command2 += motorAngle1;
command2 += motorSpeed1;
command2 += accSpeed1;
command2 += number1;
command2 += motorHz1;
command2 +="00";
//command2 += "0000";
sendCMD("34", command1, command2);
}
//=======================================================================
//ace for ESC
void SerialTest::setCarMotorInfo(int RPM,int KP,int KI ,int KD,int Meter,int ExperimentNum)
{
QString RPM_Vaule = QString("%1").arg(RPM, 4, 16, QLatin1Char( '0' ));
QString KP_Vaule = QString("%1").arg(KP, 2, 16, QLatin1Char( '0' ));
QString KI_Vaule = QString("%1").arg(KI, 2, 16, QLatin1Char( '0' ));
QString KD_Vaule = QString("%1").arg(KD, 2, 16, QLatin1Char( '0' ));
QString Dis_Vaule = QString("%1").arg(Meter, 2, 16, QLatin1Char( '0' ));
QString Num = QString("%1").arg(ExperimentNum, 2, 16, QLatin1Char( '0' ));
//QString command2 = "0000000000000000";
QString command2 = "";
command2+=Num;
command2+="00000000000000";
// command1 += motorAngle1;
// command1 += motorSpeed1;
QString command1 = "";
command1 += RPM_Vaule;
command1 += KP_Vaule;
command1 += KI_Vaule;
command1 += KD_Vaule;
command1 +=Dis_Vaule;
sendCMD("33", command1, command2);
}
//ace for ESC
void SerialTest::setESCMotorInfo(int PPM,int INC,int Dec,int Mode)
{
QString PPM_Vaule = QString("%1").arg(PPM, 4, 16, QLatin1Char( '0' ));
QString INC_ACC = QString("%1").arg(INC, 2, 16, QLatin1Char( '0' ));
QString Dec_ACC = QString("%1").arg(Dec, 2, 16, QLatin1Char( '0' ));
QString Mode_Vaule = QString("%1").arg(Mode, 2, 16, QLatin1Char( '0' ));
QString command2 = "0000000000000000";
// command1 += motorAngle1;
// command1 += motorSpeed1;
QString command1 = "";
command1 += PPM_Vaule;
command1 += INC_ACC;
command1 += Dec_ACC;
command1 += Mode_Vaule;
command1 +="00";
sendCMD("2d", command1, command2);
}
//===============================================================================================ace
//ace for ESC
//void SerialTest::setBLDCSpeed(int Speed)
//{
// QString PPM_Vaule = QString("%1").arg(PPM, 4, 16, QLatin1Char( '0' ));
// QString INC_ACC = QString("%1").arg(INC, 2, 16, QLatin1Char( '0' ));
// QString Dec_ACC = QString("%1").arg(Dec, 2, 16, QLatin1Char( '0' ));
// QString Mode_Vaule = QString("%1").arg(Mode, 2, 16, QLatin1Char( '0' ));
// QString command2 = "0000000000000000";
// command1 += motorAngle1;
// command1 += motorSpeed1;
// QString command1 = "";
// command1 += PPM_Vaule;
// command1 += INC_ACC;
// command1 += Dec_ACC;
// command1 += Mode_Vaule;
// command1 +="00";
// sendCMD("2d", command1, command2);
//}
//
QStringList SerialTest:: receivePort()
{
return m_portInfo;
}
static quint8 heartbeat = 0;
////////////////////4.发送数据//////////////////////////////
void SerialTest::sendto(QString sendmessage)//此函数由qml里的send按钮触发,sendmessage来源于qml文本框的当前文本,
{
if(true == serialOpenFlag){
heartbeat++;
QString heartbeatStr = QString("%1").arg(heartbeat, 2, 16, QLatin1Char( '0' ));
QString value = sendmessage.mid(0, 34) + heartbeatStr; // "FFFF" <- just the hex values!
// std::cout<<" value" + value.toStdString()<<std::endl;
QByteArray data = QByteArray::fromHex(value.toLatin1());
// QByteArray data = sendmessage.toLocal8Bit()+'\r';//将QString转为QByteArray,并加上'\r'(回车符),因为芯片要求在回车符之后再返回数据
qint64 testwritenumber=serialtest->write(data);//写入数据
m_receivedata=m_receivedata+"\n";//加上换行符便于显示
c_sendnumber=c_sendnumber+testwritenumber-1;//发送数据字节数统计(减去回车符)
setsendnumber(QString ::number(c_sendnumber));//更新发送的数据字节总数
// addSerialDataAll("Tx:" + value);
}
}
//static int CMDsendtime = 0;
static QString CMDFormat;
void SerialTest::sendCMD(QString cmd, QString data1, QString data2){
CMDFormat = "ff55" + data1.mid(0,12) + cmd.mid(0,2) + data2.mid(0,16);
qDebug() << CMDFormat;
addSerialDataAll("Tx:" + CMDFormat);
// CMDFormat = QByteArray::fromHex(value.toLatin1());
// CMDsendtime = 1;
sendto(CMDFormat);
}
void SerialTest::timersendtimeout(void){
// QTime currentTime = QTime::currentTime();
// qDebug() << currentTime;
// if(CMDsendtime <= 0){
// if(true == serialSendRequest){
// sendto("ff5580808080080000000000000000000000");
// }
// }
// else{
// CMDsendtime--;
// sendto(CMDFormat);
// }
}
void SerialTest::setsendnumber(QString sendnumber)//更新发送的数据字节总数,触发sendnumberChanged()的消息响应函数sendnumber()来更新显示
{
m_sendnumber=sendnumber;
emit sendnumberChanged();
}
QString SerialTest::sendnumber()//响应sendnumberChanged()消息
{
return m_sendnumber;
}
////////////////////4.接收数据//////////////////////////////
void SerialTest::receivefrom()//由readyRead()消息出发(在前边进行绑定),当串口收到数据此消息被激活(对于串口,每发送出去一个字节,都会将此字节返回,触发readyread消息,当芯片有特殊指令时,收到的信息更多,比如对sim900,发送0000,芯片就会受到0000,但是发送AT,会受到 AT OK)
{
QByteArray data = serialtest->readAll();//读取所有收到的数据
QString receivedata=data.toHex();//将QByteArray转为QString来显示
// qDebug() << receivedata;
QString subString = receivedata.mid(0,4);
QDateTime currentTime = QDateTime::currentDateTime();
QString qs_currenttime = currentTime.toString("ss.zzz");
// qDebug() << qs_currenttime;
bool ok = false;
if(subString == "ff55")
{
if(receivedata.mid(28,2) == "01"){
// bool ok = false;
qint64 corner = (receivedata.mid(4,4).toInt(&ok, 16))/10;
int x = (receivedata.mid(8,4).toInt(&ok, 16));
if(x >= 32768){
x -= 65536;
}
int y = (receivedata.mid(12,4).toInt(&ok, 16));
if(y >= 32768){
y -= 65536;
}
int z = (receivedata.mid(16,4).toInt(&ok, 16));
if(z >= 32768){
z -= 65536;
}
mag_x = x;
mag_y= y;
mag_z = z;
if(6==flag_currentPage)
{
addserialSaveAndApp( "MxMyMz: " +
QString::number(corner,'f',1) + " " +
QString::number(x) + " " +
QString::number(y) + " " +
QString::number(z) + " ");
// qDebug() << corner << " " << x << " " << y << " " << z << " ";
}
if(ok && corner != mag_corner){
mag_corner = corner;
if(mag_corner <= 21 || mag_corner >= 338 ){
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe5\x8c\x97");
}else if (mag_corner >= 22 && mag_corner <= 66) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe4\xb8\x9c\xe5\x8c\x97");
}else if (mag_corner >= 67 && mag_corner <= 112) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe4\xb8\x9c");
}else if (mag_corner >= 113 && mag_corner <= 156) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe4\xb8\x9c\xe5\x8d\x97");
}else if (mag_corner >= 157 && mag_corner <= 201) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe5\x8d\x97");
}else if (mag_corner >= 202 && mag_corner <= 247) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe8\xa5\xbf\xe5\x8d\x97");
}else if (mag_corner >= 248 && mag_corner <= 291) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe8\xa5\xbf");
}else if (mag_corner >= 292 && mag_corner <= 337) {
setMagCornerStr(QString::number(mag_corner) + "\xc2\xb0 \xe8\xa5\xbf\xe5\x8c\x97");
}
// std::cout<<" mag_cornerStr:" + mag_cornerStr.toStdString()<<std::endl;
emit receiveMagCornerChanged();
}
}else if(receivedata.mid(28,2) == "06"){
if(settingAddrFlag == true){
settingAddrFlag = false;
// bool ok = false;
addr1 = receivedata.mid(4,2).toInt(&ok, 16);
addr2 = receivedata.mid(6,2).toInt(&ok, 16);
addr3 = receivedata.mid(8,2).toInt(&ok, 16);
addrch = receivedata.mid(16,2).toInt(&ok, 16);
}
}
else if(receivedata.mid(28,2) == "05"){
//gps信息
// bool ok = false;
int u16half = 1 << 15;
int u16 = 1 << 16;
long u32half = 1 << 31;
long u32 = 1 << 32;
int currentLonInt = receivedata.mid(4,8).toInt(&ok, 16);
if(currentLonInt >= (u32half)){
currentLonInt -= u32;
}
currentLon = ((double)currentLonInt / 1e7);
int currentLatInt = receivedata.mid(12,8).toInt(&ok, 16);
if(currentLatInt >= (u32half)){
currentLatInt -= (u32);
}
currentLat = ((double)currentLatInt / 1e7);
currentEastSpeed = receivedata.mid(20,4).toInt(&ok, 16);
if(currentEastSpeed >= u16half){
currentEastSpeed -= u16;
}
currentNorthSpeed = receivedata.mid(24,4).toInt(&ok, 16);
if(currentNorthSpeed >= u16half){
currentNorthSpeed -= u16;
}
currentSatelliteNum = receivedata.mid(30,2).toInt(&ok, 16);
currentHdop = receivedata.mid(32,4).toInt(&ok, 16);
// QDateTime currentTime = QDateTime::currentDateTime();
// QString qs_currenttime = currentTime.toString("hh:mm:ss.zzz");
// if(3==flag_currentPage)
// {
// addserialSaveAndApp("GPS_Data: " +
// getCurrentLon() + " " +
// getCurrentLat() + " ");
// // qDebug() << (QString::number(currentLon) + "," + QString::number(currentLat) + "," +QString::number(currentEastSpeed)
//// +"," +QString::number(currentNorthSpeed) + "," +QString::number(currentSatelliteNum) + "," +QString::number(currentHdop));
//// qDebug() << (1<<15);
// }
}else if(receivedata.mid(28,2) == "02")
{
//接收到IMU模组数据模块1
int u16half = 1 << 15;
int u16 = 1 << 16;
ACCX = receivedata.mid(4,4).toInt(&ok, 16);
if(ACCX >= u16half){
ACCX -= u16;
}
ACCY = receivedata.mid(8,4).toInt(&ok, 16);
if(ACCY >= u16half){
ACCY -= u16;
}
ACCZ = receivedata.mid(12,4).toInt(&ok, 16);
if(ACCZ >= u16half){
ACCZ -= u16;
}
float temp;
temp=atan((double)ACCX/sqrtf(ACCY*ACCY+ACCZ*ACCZ))*180/3.1415926;
temp-=Angle_Acc_X;
if(temp>0.2||temp<-0.2)
{
Angle_Acc_X+=temp;
}
temp=atan((double)ACCY/sqrtf(ACCX*ACCX+ACCZ*ACCZ))*180/3.1415926;
temp-=Angle_Acc_Y;
if(temp>0.2||temp<-0.2)
{
Angle_Acc_Y+=temp;
}
GYROX = receivedata.mid(16,4).toInt(&ok, 16);
if(GYROX >= u16half){
GYROX -= u16;
}
GYROY = receivedata.mid(20,4).toInt(&ok, 16);
if(GYROY >= u16half){
GYROY -= u16;
}
GYROZ = receivedata.mid(24,4).toInt(&ok, 16);
if(GYROZ >= u16half){
GYROZ -= u16;
}
if(0 == TIMEINT)
{
TIMEINT = receivedata.mid(32,4).toInt(&ok, 16);
}
TIMEcnt = receivedata.mid(32,4).toInt(&ok, 16) - TIMEINT;
if(1==flag_currentPage)
{
addserialSaveAndApp("AxAyAz; " +
QString::number((double)ACCX/512,'f',4) + " " +
QString::number((double)ACCY/512,'f',4) + " " +
QString::number((double)ACCZ/512,'f',4) + " " );
// qDebug() << (QString::number(currentLon) + "," + QString::number(currentLat) + "," +QString::number(currentEastSpeed)
}
else if(2==flag_currentPage){
addserialSaveAndApp(QString::number(TIMEcnt*0.05,'f',2)/*qs_currenttime*/ + "; " +
QString::number(GYROZ* 0.061035,'f',6) + "; ");
// qDebug() << (QString::number(currentLon) + "," + QString::number(currentLat) + "," +QString::number(currentEastSpeed)
}
}
else if(receivedata.mid(28,2) == "08")
{
//接收到超声波模组数据
int u16half = 1 << 15;
int u16 = 1 << 16;
Heigth_Ultrasonic_nofliter = receivedata.mid(4,4).toInt(&ok, 16);
if(Heigth_Ultrasonic_nofliter >= u16half){
Heigth_Ultrasonic_nofliter = 1;
}
Heigth_Ultrasonic_lowpass = receivedata.mid(8,4).toInt(&ok, 16);
if(Heigth_Ultrasonic_lowpass >= u16half){
Heigth_Ultrasonic_lowpass = 1;
}
Heigth_Ultrasonic_time = Heigth_Ultrasonic_lowpass/170.0;// / 340 * 2
Heigth_Ultrasonic_alpha = receivedata.mid(16,4).toInt(&ok, 16) / 10000.0;
if(5==flag_currentPage)
{
addserialSaveAndApp("Ultrasonic_Data: " +
getHeigth_Ultrasonic_nofliter() + " " +
getHeigth_Ultrasonic_lowpass());
// qDebug() << Heigth_Ultrasonic_nofliter << Heigth_Ultrasonic_lowpass;
}
}
else if(receivedata.mid(28,2) == "03")
{
//接收到IMU模组数据模块2
int u16half = 1 << 15;
int u16 = 1 << 16;
anglepitch = receivedata.mid(4,4).toInt(&ok, 16);
if(anglepitch >= u16half){
anglepitch -= u16;
}
anglepitch = anglepitch / 10.0;
angleroll = receivedata.mid(8,4).toInt(&ok, 16);
if(angleroll >= u16half){
angleroll -= u16;
}
angleroll = angleroll / 10.0;
angleyaw = receivedata.mid(12,4).toInt(&ok, 16);
if(angleyaw >= u16half){
angleyaw -= u16;
}
angleyaw = (angleyaw/10.0 - 180);
// addserialSaveAndApp(qs_currenttime + "; " +
// getAnglePitch() + "; " +
// getAngleRoll() + "; " +
// getAngleYaw() + "; ");// qDebug() << (QString::number(currentLon) + "," + QString::number(currentLat) + "," +QString::number(currentEastSpeed)
// qDebug() << anglepitch << angleroll << angleyaw;
}
else if(receivedata.mid(28,2) == "00")
{
//接收到无人机整机数据帧
int u16half = 1 << 15;
int u16 = 1 << 16;
int temp;
altitude = receivedata.mid(20,4).toInt(&ok, 16);
angleyaw = receivedata.mid(12,4).toInt(&ok, 16);
if(angleyaw >= u16half){
angleyaw -= u16;
}
angleyaw = angleyaw/10.0;
if(angleyaw<0)
{
angleyaw+=360;
}
temp=receivedata.mid(30,2).toInt(&ok, 16);
if(temp==1)
{
anglepitch = receivedata.mid(32,4).toInt(&ok, 16);
if(anglepitch >= u16half){
anglepitch -= u16;
}
anglepitch = anglepitch / 10.0;
}
else if(temp==2)
{
angleroll = receivedata.mid(32,4).toInt(&ok, 16);
if(angleroll >= u16half){
angleroll -= u16;
}
angleroll = angleroll / 10.0;
}
// else if(temp==3)
// {
// altitude = receivedata.mid(32,4).toInt(&ok, 16);
// if(altitude >= u16half){
// altitude -= u16;
// }
// }