-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoderbin.cpp
More file actions
66 lines (59 loc) · 2.13 KB
/
decoderbin.cpp
File metadata and controls
66 lines (59 loc) · 2.13 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
#include "decoderbin.h"
DecoderBin::DecoderBin(QObject *parent,Configuration *config) :
DecoderBase(parent,config)
{
}
void DecoderBin::newData(const QByteArray& newBytes){
//qDebug(newBytes.toHex());
packetBytes.append(newBytes);
};
void DecoderBin::packetSeparator(){
packetStarted = true;
if(!packetBytes.isEmpty()){
packetParts.clear();
packetValues.clear();
QByteArray part;
QVariant value;
int j = 0;
for(int i=0; i < config->fields.length(); i++){
QString field = config->fields[i];
QString type = config->get(field,"type","byte");
bool msbf = (config->get(field,"endian","msbf") == "msbf");
int len = 1;
value = 0;
part = QByteArray().append('\0');
if("byte" == type){
part = packetBytes.mid(j,len);
value = (unsigned char)part[0];
}else if("sbyte" == type){
part = packetBytes.mid(j,len);
value = (char)part[0];
}else if("word" == type){
len = 2;
part = packetBytes.mid(j,len);
if(msbf){
value = (unsigned short)((unsigned char)part[0]*256 + (unsigned char)part[1]);
}else{
value = (unsigned short)((unsigned char)part[0] + (unsigned char)part[1]*256);
}
}else if("sword" == type){
len = 2;
part = packetBytes.mid(j,len);
if(msbf){
value = (short)((char)part[0]*256 + (unsigned char)part[1]);
}else{
value = (short)((unsigned char)part[0] + (char)part[1]*256);
}
}else if("float" == type){
len = 4;
part = packetBytes.mid(j,len);
value = *(float*)(part.data());
}
packetValues.append(value);
packetParts.append(part);
j += len;
}
emit newPacket(this);
}
packetBytes.clear();
};