Repository navigation
Expand file tree
/
Copy pathserialworker.cpp
More file actions
197 lines (181 loc) · 6.57 KB
/
Copy pathserialworker.cpp
File metadata and controls
197 lines (181 loc) · 6.57 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
#include "serialworker.h"
#include <QDebug>
#include "util.h"
#include "serial/serial.h"
#include <QDebug>
#include <QByteArray>
void SerialWorker::run()
{
//Util::list_ports();
//serial::Serial my_serial(COM6, 115200, serial::Timeout::simpleTimeout(2000));
serial::Serial my_serial("/dev/ttyS0", 115200, serial::Timeout::simpleTimeout(2000));
if(my_serial.isOpen())
qDebug()<<"has open";
else
qDebug()<<"not open";
QByteArray array;
errorFlag = 0;
char send[10];
send[0] = 0x6e;
send[1] = 0x5a;
send[2] = 8;
send[3] = 0;
send[6] = 0;
send[7] = 0;
updateFirmWareFlag =false;
updateFirmWareState =0;
firmWare = NULL;
while(true){
if(updateFirmWareFlag)
{
if(updateFirmWareState == 0)
{
my_serial.flushOutput();
sleep(1);
if(firmWare!=NULL){
delete firmWare;
firmWare = NULL;
}
firmWare = new FirmWare(path,path_);
qDebug()<<path<<path_;
firmWare->setStart();
qDebug()<<"setStart"<<firmWare->getSendSize()<<" "<<firmWare->getFileSize();
my_serial.write((uint8_t *)firmWare->getSendTemp(),firmWare->getSendSize());
updateFirmWareState = 1;
}else
{
char temp[16];
int bytes_wrote = my_serial.read((uint8_t *)temp,16);
QByteArray val((char *)temp,bytes_wrote);
array.append(val);
unsigned int state = 0;
unsigned int package = 0;
unsigned int version = 0;
while(updateDataParse(&array,&package,&state,&version)){
}
if(state == 1){
if(firmWare != NULL){
firmWare->setPackage(package,version);
qDebug()<<"updateDataParse "<<package<<" size "<<firmWare->getSendSize()<<" version"<<version;
my_serial.write((uint8_t *)firmWare->getSendTemp(),firmWare->getSendSize());
}
}else if(state == 2){
qDebug()<<"update done";
updateFirmWareFlag = false;
updateFirmWareState =0;
cusdialog->changeStyle(UPDATE_END,1);
}
}
}else
{
char temp[20];
int bytes_wrote = my_serial.read((uint8_t *)temp,20);
QByteArray val((char *)temp,bytes_wrote);
array.append(val);
while(dataParse(&array,dpuStatus)){
}
send[4] = pcStatus->powerlevel &0xff;
send[5] = (pcStatus->powerlevel>>8)&0xff;
if(pcStatus->warningControl){
send[6] = 1;
}else{
send[6] = 0;
}
if(!pcStatus->state){
send[7] = 2;
}else{
if(pcStatus->toppleControl)
{
send[7] = 1;
}else{
send[7] = 0;
}
}
// qDebug()<<"pcStatus->toppleControl"<<send[7];
my_serial.write((uint8_t *)send,8);
}
}
my_serial.close();
}
bool SerialWorker::dataParse(QByteArray *array,DpuStatus *dpuStatus)
{
bool flag = false;
for(int i=0;i<array->length()-3;i++)
{
if(array->at(i) == 0x6c && array->at(i+1) == 0x5a)
{
int len = (array->at(i+3)<<8) + array->at(i+2) + 4;
if((len + i) > array->length())
{
break;
}else{
{
dpuStatus->temp_a = (((uint8_t)array->at(i+5)<<8) + (uint8_t)array->at(i+4))/10;
dpuStatus->temp_b = (((uint8_t)array->at(i+7)<<8) + (uint8_t)array->at(i+6))/10;
dpuStatus->ua = (((uint8_t)array->at(i+9)<<8) + (uint8_t)array->at(i+8))/100;
dpuStatus->ia = (((uint8_t)array->at(i+11)<<8) + (uint8_t)array->at(i+10))/100;
dpuStatus->power = (((uint8_t)array->at(i+15)<<24)+ ((uint8_t)array->at(i+14)<<16) + ((uint8_t)array->at(i+13)<<8) + (uint8_t)array->at(i+12))/100;
dpuStatus->errorcode = ((uint8_t)array->at(i+19)<<24)+ ((uint8_t)array->at(i+18)<<16) + ((uint8_t)array->at(i+17)<<8) + (uint8_t)array->at(i+16);
if(dpuStatus ->errorcode != errorFlag){
int errortemp = dpuStatus ->errorcode ^ errorFlag;
int dismisscode = errortemp & errorFlag;
int dispatchcode = dpuStatus ->errorcode & errortemp;
qDebug()<<"errorcode "<<dpuStatus ->errorcode <<" dispatchcode"<<dispatchcode<<" dismisscode"<<dismisscode;
if(dismisscode != 0){
emit(errorDismiss(dismisscode));
}
if(dispatchcode!=0){
emit(errorDispatch(dispatchcode));
}
errorFlag = dpuStatus ->errorcode;
}
}
array->remove(0,i+len);
flag = true;
break;
}
}
}
return flag;
}
bool SerialWorker::updateDataParse(QByteArray *array,unsigned int *package,unsigned int *state,unsigned int *version)
{
bool flag = false;
for(int i=0;i<array->length()-4;i++)
{
if(array->at(i) == 0x6a && array->at(i+1) == 0x5a)
{
int datelen = (array->at(i+3)<<8) + array->at(i+2);
int len = datelen + 8;
if((len + i) > array->length())
{
break;
}else{
unsigned int checksum = ((uint8_t)array->at(datelen+7)<<24)+ ((uint8_t)array->at(datelen+6)<<16) + ((uint8_t)array->at(datelen+5)<<8) + (uint8_t)array->at(datelen+4);
if(checksumValidate(array,0,datelen+4,checksum))
{
*state = ((uint8_t)array->at(i+5)<<8) + (uint8_t)array->at(i+4);
*version = ((uint8_t)array->at(i+7)<<8) + (uint8_t)array->at(i+6);
*package = ((uint8_t)array->at(i+11)<<24)+ ((uint8_t)array->at(i+10)<<16) + ((uint8_t)array->at(i+9)<<8) + (uint8_t)array->at(i+8);
array->remove(0,i+len);
flag = true;
break;
}
array->remove(0,i+len);
}
}
}
return flag;
}
bool SerialWorker::checksumValidate(QByteArray *data, int offset,int len, unsigned int checksum)
{
bool flag = false;
unsigned int sum = 0;
for(int i=0;i<len;i++){
sum = sum + (uint8_t(data->at(i+offset)));
}
//qDebug()<<"sum"<<sum<<" checksum"<<checksum;
if(sum == checksum)
flag = true;
return flag;
}