-
Notifications
You must be signed in to change notification settings - Fork 0
/
idata.cpp
177 lines (158 loc) · 3.41 KB
/
idata.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
#include "idata.h"
quint8 IData::deviceId = 0;
IData::IData():
m_nIndex(0)
{
}
IData::IData(const QByteArray &buf) :
m_nIndex(0),
m_buffer(buf)
{
}
qint16 IData::ReadShort()
{
if (m_nIndex+2 > m_buffer.size())
{
return 0xffff;
}
qint16 crc2 = ((m_buffer.at(m_nIndex++) << 8) & 0xff00);
crc2 += ((m_buffer.at(m_nIndex++)) & 0xff);
return crc2;
}
quint16 IData::ReadUShort()
{
if (m_nIndex+2 > m_buffer.size())
{
return 0xffff;
}
quint16 crc2 = ((m_buffer.at(m_nIndex++) << 8) & 0xff00);
crc2 += ((m_buffer.at(m_nIndex++)) & 0xff);
return crc2;
}
qint32 IData::ReadInt()
{
if (m_nIndex + 4 > m_buffer.size())
{
return 0xffffffff;
}
uint nTmp = 0;
for (int j = 0; j < 4; ++j)
{
nTmp += (((m_buffer.at(m_nIndex++) << (24 - j * 8))) & (0xff << (24 - j * 8)));
}
qint32 nTmp2 = nTmp;
return nTmp2;
}
qint8 IData::ReadByte()
{
if (m_nIndex + 1 > m_buffer.size())
{
return 0xff;
}
qint8 nTmp = m_buffer.at(m_nIndex++);
return nTmp;
}
quint8 IData::ReadUByte()
{
if (m_nIndex + 1 > m_buffer.size())
{
return 0xff;
}
quint8 nTmp = m_buffer.at(m_nIndex++);
return nTmp;
}
void IData::WriteByte(quint8 c)
{
//quint8 nTmp = c & 0xf;
m_buffer.append(c);
}
void IData::WriteShort(qint16 content)
{
quint8 nTmp = ((content >> 8) & 0xff);
m_buffer.append(nTmp);
nTmp = content & 0xff;
m_buffer.append(nTmp);
}
void IData::WriteInt(qint32 content)
{
for (int j = 0; j < 4; ++j)
{
quint8 nTmp = ((content >> (24 - j * 8)) & 0xff);
m_buffer.append(nTmp);
}
}
bool IData::Skip(qint32 skip)
{
qint32 nTmp = m_nIndex + skip;
if (nTmp < 0 || nTmp > m_buffer.size()-1)
{
return false;
}
m_nIndex = nTmp;
return true;
}
void IData::AddProxyEnd()
{
m_buffer.append(0xED);
//buf.append('\n');
}
void IData::AddProxyBegin(const quint8 nMode, const quint8 nDeviceId)
{
m_buffer.append(0xEE);
m_buffer.append(nDeviceId);
m_buffer.append(nMode);
}
bool IData::CheckData()
{
QByteArray buf = m_buffer;
if (buf.size() < 6)
{
return false;
}
quint8 nBeign = buf.at(0);
quint8 nEnd = buf.at(buf.size() - 1);
if (nBeign != 0xEE || nEnd != 0xED)
{
return false;
}
char * content = const_cast<char *>(buf.data());
content += 5;
quint16 crc = CRC16_1((uchar *)content, buf.size() - 8);
quint16 crc2 = ((buf.at(buf.size()-3) << 8) & 0xff00);
crc2 += ((buf.at(buf.size()-2)) & 0xff);
if (crc != crc2)
{
return false;
}
return true;
}
void IData::AddCRCCode(qint16 nLen)
{
if (m_buffer.size() > 5)
{
char * charContent = m_buffer.data();
charContent += 5;//跳过帧前面部分的固定数据
quint16 crc = CRC16_1((uchar *)charContent, nLen);
quint8 nTmp = ((crc >> 8) & 0xff);
m_buffer.append(nTmp);
nTmp = crc & 0x00ff;
m_buffer.append(nTmp);
}
}
//template<class T>
//void IData::WriteData(IData::T data)
//{
// int nLen = sizeof(data);
// switch (nLen) {
// case 1:
// WriteByte(data);
// break;
// case 2:
// WriteShort(data);
// break;
// case 4:
// WriteInt(data);
// default:
// break;
// }
//}