-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyparser.cpp
179 lines (162 loc) · 3.95 KB
/
proxyparser.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
#include "proxyparser.h"
#include "innfosproxy.h"
#include "mediator.h"
#include <QDebug>
ProxyParser * ProxyParser::m_pParser = nullptr;
ProxyParser *ProxyParser::getInstance()
{
if(!m_pParser)
{
m_pParser = new ProxyParser();
}
return m_pParser;
}
void ProxyParser::autoDestroy()
{
if(m_pParser)
delete m_pParser;
m_pParser = nullptr;
}
void ProxyParser::parse(const QByteArray &buf)
{
m_remainData.append(buf);
while (m_remainData.size() > 5) {
if(!headCheck(m_remainData))
handleError();
if(m_remainData.size() > 5)
{
QByteArray byteData = m_remainData.left(5);
m_remainData = m_remainData.right(m_remainData.size()-5);
IData tmp(byteData);
tmp.Skip(3);
quint16 nLen = tmp.ReadShort();
int nRemainCnt = 0;// data bytes remain to read
if (nLen == 0)
{
nRemainCnt = 1;
}
else
{
if (nLen == 1 && byteData.at(2) != '\2' && byteData.at(2) != D_CAN_CONNECT)//暂时处理
{
nRemainCnt = 2;//
}
else
{
nRemainCnt = nLen + 3;
}
}
if (m_remainData.size() < nRemainCnt)// data not enough
{
m_remainData.prepend(byteData);
return;
}
byteData.append(m_remainData.left(nRemainCnt));
m_remainData = m_remainData.right(m_remainData.size()-nRemainCnt);
if(!dataCheck(byteData))
{
handleError();
}
else
{
dispatchData(byteData);
}
}
else
{
return;
}
}
}
ProxyParser::ProxyParser(QObject *parent) : QObject(parent)
{
}
void ProxyParser::handleError()
{
QByteArray tag;
tag.resize(1);
tag[0] = 0xEE;
int nIdx = m_remainData.indexOf(tag, 1);
if (nIdx >= 0)
{
qDebug() << "ParseError" << m_remainData.toHex();
m_remainData = m_remainData.right(m_remainData.size()-nIdx);
}
else
{
qDebug() << "ParseError" << m_remainData;
m_remainData.clear();
}
}
bool ProxyParser::headCheck(const QByteArray &data)
{
if (data.size() == 0)
return false;
QByteArray tag;
tag.resize(1);
tag[0] = 0xEE;
int nIdx = data.indexOf(tag, 0);
if(nIdx != 0)
return false;
return true;
}
bool ProxyParser::dataCheck(const QByteArray &data)
{
if(data.size() < 7)
return false;
QByteArray tag;
tag.resize(1);
tag[0] = 0xED;
int nIdx = data.indexOf(tag, data.size()-1);
if(nIdx != data.size()-1)
return false;
IData tmp(data);
tmp.Skip(3);
quint16 nLen = tmp.ReadShort();
if(nLen+6 < data.size())// has crc check code
{
return tmp.CheckData();
}
return true;
}
void ProxyParser::dispatchData(QByteArray &buf)
{
#ifdef TEST_DEBUG
ProxyWatcher::getInstance()->reciveItem(buf);
#endif
quint8 nTag = buf.at(2);
switch (nTag)
{
case D_HANDSHAKE:
NoCrcProxy::decode(buf);
break;
case D_SET_POSITION_P:
case D_SET_POSITION_I:
case D_SET_POSITION_D:
case D_SET_CURRENT_P:
case D_SET_CURRENT_I:
case D_SET_VELOCITY_P:
case D_SET_VELOCITY_I:
case D_CLEAR_ERROR:
case D_CLEAR_HOMING:
case D_SET_PAIRS:
case D_SET_CURRENT:
case D_SET_POSITION:
case D_SET_VELOCITY:
case D_CHART_DATA_STATR:
case D_CAN_CONNECT:
case D_SAVE_PARAM:
case D_SET_DEVICE_ID:
case D_CHART_OPEN:
case D_CHART_CLOSE:
case D_READ_MOTORS_SWITCH://暂时处理
case D_READ_MOTOR_MODE://暂时处理
case D_READ_LAST_STATE:
case D_SET_SWITCH_MOTORS:
NoCrcProxy::decode(buf);
break;
default:
InnfosProxy::decode(buf);
break;
}
}