-
Notifications
You must be signed in to change notification settings - Fork 3
/
converter.cpp
116 lines (96 loc) · 3.95 KB
/
converter.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
#include "converter.h"
uint16_t Converter::getDecData(const QString &hexData, uint8_t indBeg, uint8_t len){
QString binData = hexToBin(hexData);
QString binFragment = binData.mid(indBeg, len);
bool ok = false;
uint16_t res = binFragment.toUShort(&ok, 2);
return res;
}
QString Converter::floatCutOff(const float &value, int afterDot){
std::stringstream stream;
stream << std::fixed << std::setprecision(afterDot) << value;
return QString::fromStdString(stream.str());
}
void Converter::getZmqFromCanFd(zmq::message_t &message, const canfd_frame &frame, const MessageId &id){
message.rebuild(sizeof(ZmqCanMessage));
message.data<ZmqCanMessage>()->_msg_type = MsgType::CANMsg;
message.data<ZmqCanMessage>()->_id = id;
message.data<ZmqCanMessage>()->_frame.can_id = frame.can_id;
message.data<ZmqCanMessage>()->_frame.len = frame.len;
message.data<ZmqCanMessage>()->_frame.flags = frame.flags;
message.data<ZmqCanMessage>()->_frame.__res0 = frame.__res0;
message.data<ZmqCanMessage>()->_frame.__res1 = frame.__res1;
for( __u8 idx = 0; idx < frame.len; ++idx)
message.data<ZmqCanMessage>()->_frame.data[idx] = frame.data[idx];
}
void Converter::getCanFdFromCanLine(canfd_frame &frame, const CanLine &canLine){
const uint8_t fragmSz = 2;
bool ok1 = false;
frame.can_id = canLine.messId.toUInt(&ok1, 16);
frame.len = canLine.messData.length() / fragmSz;
frame.flags = 0;
frame.__res0 = 0;
frame.__res1 = 0;
for(__u8 idx = 0; idx < frame.len; ++idx){
QString hexFragment = canLine.messData.mid(idx * fragmSz, fragmSz);
bool ok2 = false;
frame.data[idx] = hexFragment.toUInt(&ok2, 16);
}
}
void Converter::getCanFdFromZmq(const zmq::message_t &message, canfd_frame &frame, MessageId &id){
if(message.data<ZmqCanMessage>()->_msg_type == MsgType::CANMsg){
id = message.data<ZmqCanMessage>()->_id;
frame.can_id = message.data<ZmqCanMessage>()->_frame.can_id;
frame.len = message.data<ZmqCanMessage>()->_frame.len;
frame.flags = message.data<ZmqCanMessage>()->_frame.flags;
frame.__res0 = message.data<ZmqCanMessage>()->_frame.__res0;
frame.__res1 = message.data<ZmqCanMessage>()->_frame.__res1;
for(__u8 idx = 0; idx < frame.len; ++idx)
frame.data[idx] = message.data<ZmqCanMessage>()->_frame.data[idx];
}
}
CanLine Converter::getCanLineFromCanFd(const std::string &device, const canfd_frame &frame, bool isZmq){
CanLine canLine;
canLine.timeStamp = GET_CUR_TIME_MICRO;
isZmq ? canLine.canNum = "can" + QString::fromStdString(device) : canLine.canNum = QString::fromStdString(device);
canLine.messId = hexToDec(frame.can_id);
QString hexData;
for(uint8_t i = 0; i < frame.len; i++)
hexData.append(hexToDec2(frame.data[i]));
canLine.messData = hexData;
return canLine;
}
QString Converter::binToHex(const QString &binStr){
QString hexStr;
uint8_t points = binStr.length() / 8 - 1;
bool ok = false;
hexStr = QString::number(binStr.toULongLong(&ok, 2), 16);
while (hexStr.length() < binStr.length() / 4)
hexStr.prepend("0");
hexStr = hexStr.toUpper();
for (size_t i = 0; i < points; i++){
uint8_t ind = i * 3 + 2;
hexStr.insert(ind, '.');
}
return hexStr;
}
QString Converter::decToBin(const QString &decStr, uint8_t binLen){
bool ok = false;
QString binData = QString::number(decStr.toULongLong(&ok, 10), 2);
while (binData.length() < binLen)
binData.prepend("0");
return binData;
}
QString Converter::hexToBin(const QString &hexStr){
bool ok = false;
QString binData = QString::number(hexStr.toULongLong(&ok, 16), 2);
while (binData.length() < hexStr.length() * 4)
binData.prepend("0");
return binData;
}
QString Converter::hexToDec2(uint8_t data){
QString hexStr = QString::number(data, 16);
while (hexStr.length() < 2)
hexStr.prepend("0");
return hexStr;
}