提交 | 用户 | age
|
4b03ae
|
1 |
#include "pch.h"
|
Q |
2 |
#include "KLinkProtocol.h"
|
|
3 |
|
|
4 |
KLinkProtocol::KLinkProtocol()
|
|
5 |
{
|
|
6 |
|
|
7 |
}
|
|
8 |
|
|
9 |
KLinkProtocol::~KLinkProtocol()
|
|
10 |
{
|
|
11 |
|
|
12 |
}
|
|
13 |
const char * KLinkProtocol::GetErrDescStr(int nErrNo)
|
|
14 |
{
|
|
15 |
switch (nErrNo)
|
|
16 |
{
|
|
17 |
case KL_ERR:
|
|
18 |
return "ERROR";
|
|
19 |
case KL_OK: //=0
|
|
20 |
return "OK";
|
|
21 |
case KL_NG:// = 1,
|
|
22 |
return "NG";
|
|
23 |
case KL_UNKNOWN: // = 2,
|
|
24 |
return "KL_UNKNOWN";
|
|
25 |
case KL_TIMEOUT: // = 3,
|
|
26 |
return "KL_TIMEOUT";
|
|
27 |
case KL_BCC_ERR: // = 4,
|
|
28 |
return "KL_BCC_ERR";
|
|
29 |
case KL_PKG_ERR: // = 5,
|
|
30 |
return "KL_PKG_ERR";
|
|
31 |
case KL_SEQ_ERR: // = 6,
|
|
32 |
return "KL_SEQ_ERR";
|
|
33 |
case KL_LENTH_EXCEED: //,
|
|
34 |
return "KL_LENTH_EXCEED";
|
|
35 |
case KL_NEED_PASS: //,
|
|
36 |
return "KL_NEED_PASS";
|
|
37 |
case KL_NOT_SUPPORT://,
|
|
38 |
return "KL_NOT_SUPPORT";
|
|
39 |
default:
|
|
40 |
return "KL_OTHER_ERR";
|
|
41 |
break;
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
|
45 |
int KLinkProtocol::xtoi(const char * hexstr, int len)
|
|
46 |
{
|
|
47 |
int len1 = len;
|
|
48 |
if (len <= 0) len1 = (int)strlen(hexstr);
|
|
49 |
int j=0;
|
|
50 |
for (int i = 0; i < len1; i++)
|
|
51 |
{
|
|
52 |
unsigned char ch = hexstr[i];
|
|
53 |
if (ch >= '0'&&ch <= '9') { int k = ch - '0'; j = j * 16 + k; continue; }
|
|
54 |
if (ch >= 'A'&&ch <= 'F') { int k = ch - 'A' + 10; j = j * 16 + k; continue; }
|
|
55 |
if (ch >= 'a'&&ch <= 'f') { int k = ch - 'a' + 10; j = j * 16 + k; continue; }
|
|
56 |
if (ch == ' ' || ch == ' ') { continue; }
|
|
57 |
break;
|
|
58 |
}
|
|
59 |
return j;
|
|
60 |
}
|
|
61 |
|
|
62 |
unsigned char KLinkProtocol::KLBCC(void * pData, int nSize)
|
|
63 |
{
|
|
64 |
unsigned char k = 0;
|
|
65 |
for (int i = 0; i < nSize; i++)
|
|
66 |
{
|
|
67 |
k ^= ((unsigned char *)pData)[i];
|
|
68 |
}
|
|
69 |
return k;
|
|
70 |
}
|
|
71 |
int KLinkProtocol::CalCRC(void * pBuf, int nSize)
|
|
72 |
{
|
|
73 |
return 0;
|
|
74 |
}
|
|
75 |
int KLinkProtocol::SetSendCallBackFunc(pSend pSendFunc)
|
|
76 |
{
|
|
77 |
SendPkgFunc = pSendFunc;
|
|
78 |
m_bSendCallBackSet = 1;
|
|
79 |
return KL_OK;
|
|
80 |
}
|
|
81 |
int KLinkProtocol::SetRecvCallBackFunc(pRecv pRecvFunc)
|
|
82 |
{
|
|
83 |
RecvPkgFunc = pRecvFunc;
|
|
84 |
m_bRecvCallBackSet = 1;
|
|
85 |
return KL_OK;
|
|
86 |
}
|
|
87 |
|
|
88 |
int KLinkProtocol::MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData)
|
|
89 |
{
|
|
90 |
int framelen1 = 0;
|
|
91 |
pKLExtDataFrame pF1 = (pKLExtDataFrame)pBuf;
|
|
92 |
pF1->Dst = nDst;
|
|
93 |
pF1->nType = nType;
|
|
94 |
pF1->Len2 = len2;
|
|
95 |
memcpy(pF1->Data, pData, len2);
|
|
96 |
UCHAR bcc = KLBCC(pBuf, sizeof(tagKLExtDataFrame) + len2 - 1);
|
|
97 |
pF1->Data[len2] = bcc;
|
|
98 |
|
|
99 |
framelen1 = sizeof(tagKLExtDataFrame) + len2;
|
|
100 |
return framelen1;
|
|
101 |
}
|
|
102 |
|
|
103 |
int KLinkProtocol::MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData)
|
|
104 |
{
|
|
105 |
int PkgLen1 = 0;
|
|
106 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
107 |
pPacket->StartSign = KLSignStart;
|
|
108 |
pPacket->DstAddr = nDst;
|
|
109 |
pPacket->Stat = Stat;
|
|
110 |
pPacket->Cmd = nCMD;
|
|
111 |
pPacket->Type1 = Type;
|
|
112 |
int Datalen = 0;
|
|
113 |
switch (nCMD)
|
|
114 |
{
|
|
115 |
case KLCmdNone:
|
|
116 |
break;
|
|
117 |
case KLCmdPing:
|
|
118 |
Datalen = Type;
|
|
119 |
memcpy(pPacket->Params, pData, Datalen);
|
|
120 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
121 |
break;
|
|
122 |
case KLCmdGetInfo:
|
|
123 |
break;
|
|
124 |
case KLCmdRead:
|
|
125 |
Datalen = 0;
|
|
126 |
pPacket->Params[0] = nAddr % 256;
|
|
127 |
pPacket->Params[1] = nAddr / 256;
|
|
128 |
pPacket->Params[2] = nCount % 256;
|
|
129 |
pPacket->Params[3] = nCount / 256;
|
|
130 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
131 |
break;
|
|
132 |
case KLCmdWrite:
|
|
133 |
pPacket->Params[0] = nAddr % 256;
|
|
134 |
pPacket->Params[1] = nAddr / 256;
|
|
135 |
pPacket->Params[2] = nCount % 256;
|
|
136 |
pPacket->Params[3] = nCount / 256;
|
|
137 |
Datalen = nCount;
|
|
138 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
139 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
140 |
|
|
141 |
break;
|
|
142 |
case KLCmdSaveToFlash:
|
|
143 |
break;
|
|
144 |
case KLCmdExChgData:
|
|
145 |
break;
|
|
146 |
case KLCmdGetEventLogCount:
|
|
147 |
pPacket->Params[0] = 0;
|
|
148 |
pPacket->Params[1] = 0;
|
|
149 |
pPacket->Params[2] = 0;
|
|
150 |
pPacket->Params[3] = 0;
|
|
151 |
Datalen = 0;
|
|
152 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
153 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
154 |
default:
|
|
155 |
break;
|
|
156 |
}
|
|
157 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
158 |
{
|
|
159 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
160 |
pPacket->Params[PkgLen1-sizeof(stKLReqPacket)] = BCC;
|
|
161 |
}
|
|
162 |
return PkgLen1;
|
|
163 |
}
|
|
164 |
int KLinkProtocol::MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData, int ExtFrameLen, void * pExtDataFrame)
|
|
165 |
{
|
|
166 |
int PkgLen1 = 0;
|
|
167 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
168 |
pPacket->StartSign = KLSignStart;
|
|
169 |
pPacket->DstAddr = nDst;
|
|
170 |
pPacket->Stat = Stat;
|
|
171 |
pPacket->Cmd = nCMD;
|
|
172 |
pPacket->Type1 = Type;
|
|
173 |
|
|
174 |
pKLStat pStat = (pKLStat)&(pPacket->Stat);
|
|
175 |
int Datalen = 0;
|
|
176 |
switch (nCMD)
|
|
177 |
{
|
|
178 |
case KLCmdNone:
|
|
179 |
break;
|
|
180 |
case KLCmdPing:
|
|
181 |
Datalen = Type;
|
|
182 |
memcpy(pPacket->Params, pData, Datalen);
|
|
183 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
184 |
break;
|
|
185 |
case KLCmdGetInfo:
|
|
186 |
break;
|
|
187 |
case KLCmdRead:
|
|
188 |
Datalen = 0;
|
|
189 |
pPacket->Params[0] = nAddr % 256;
|
|
190 |
pPacket->Params[1] = nAddr / 256;
|
|
191 |
pPacket->Params[2] = nCount % 256;
|
|
192 |
pPacket->Params[3] = nCount / 256;
|
|
193 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
194 |
break;
|
|
195 |
case KLCmdWrite:
|
|
196 |
pPacket->Params[0] = nAddr % 256;
|
|
197 |
pPacket->Params[1] = nAddr / 256;
|
|
198 |
pPacket->Params[2] = nCount % 256;
|
|
199 |
pPacket->Params[3] = nCount / 256;
|
|
200 |
Datalen = nCount;
|
|
201 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
202 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
203 |
break;
|
|
204 |
case KLCmdRead1Bit:
|
|
205 |
Datalen = 0;
|
|
206 |
pPacket->Params[0] = nAddr % 256;
|
|
207 |
pPacket->Params[1] = nAddr / 256;
|
|
208 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
209 |
break;
|
|
210 |
case KLCmdWrite1Bit:
|
|
211 |
pPacket->Params[0] = nAddr % 256;
|
|
212 |
pPacket->Params[1] = nAddr / 256;
|
|
213 |
Datalen = 1;
|
|
214 |
memcpy(pPacket->Params + 2, pData, Datalen);
|
|
215 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
216 |
break;
|
|
217 |
|
|
218 |
case KLCmdReadBits:
|
|
219 |
Datalen = 0;
|
|
220 |
pPacket->Params[0] = nAddr % 256;
|
|
221 |
pPacket->Params[1] = nAddr / 256;
|
|
222 |
pPacket->Params[2] = nCount % 256;
|
|
223 |
pPacket->Params[3] = nCount / 256;
|
|
224 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
225 |
break;
|
|
226 |
case KLCmdWriteBits:
|
|
227 |
Datalen = 0;
|
|
228 |
pPacket->Params[0] = nAddr % 256;
|
|
229 |
pPacket->Params[1] = nAddr / 256;
|
|
230 |
pPacket->Params[2] = nCount % 256;
|
|
231 |
pPacket->Params[3] = nCount / 256;
|
|
232 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
233 |
break;
|
|
234 |
case KLCmdReadDataByte:
|
|
235 |
case KLCmdReadDataWord:
|
|
236 |
case KLCmdReadData:
|
|
237 |
|
|
238 |
Datalen = 0;
|
|
239 |
pPacket->Params[0] = nAddr % 256;
|
|
240 |
pPacket->Params[1] = nAddr / 256;
|
|
241 |
pPacket->Params[2] = nCount % 256;
|
|
242 |
pPacket->Params[3] = nCount / 256;
|
|
243 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
244 |
break;
|
|
245 |
case KLCmdWriteDataByte:
|
|
246 |
case KLCmdWriteDataWord:
|
|
247 |
case KLCmdWriteData:
|
|
248 |
pPacket->Params[0] = nAddr % 256;
|
|
249 |
pPacket->Params[1] = nAddr / 256;
|
|
250 |
pPacket->Params[2] = nCount % 256;
|
|
251 |
pPacket->Params[3] = nCount / 256;
|
|
252 |
Datalen = nCount;
|
|
253 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
254 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
255 |
break;
|
|
256 |
case KLCmdSaveToFlash:
|
|
257 |
break;
|
|
258 |
case KLCmdExChgData:
|
|
259 |
break;
|
|
260 |
case KLCmdSaveSysCfg:
|
|
261 |
pPacket->Params[0] = 0;
|
|
262 |
pPacket->Params[1] = 0;
|
|
263 |
pPacket->Params[2] = 0;
|
|
264 |
pPacket->Params[3] = 0;
|
|
265 |
Datalen = 0;
|
|
266 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
267 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
268 |
break;
|
|
269 |
case KLCmdSaveRunStat:
|
|
270 |
pPacket->Params[0] = 0;
|
|
271 |
pPacket->Params[1] = 0;
|
|
272 |
pPacket->Params[2] = 0;
|
|
273 |
pPacket->Params[3] = 0;
|
|
274 |
Datalen = 0;
|
|
275 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
276 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
277 |
break;
|
|
278 |
case KLCmdClearEventLog:
|
|
279 |
pPacket->Params[0] = 0;
|
|
280 |
pPacket->Params[1] = 0;
|
|
281 |
pPacket->Params[2] = 0;
|
|
282 |
pPacket->Params[3] = 0;
|
|
283 |
Datalen = 0;
|
|
284 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
285 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
286 |
|
|
287 |
case KLCmdGetEventLogCount:
|
|
288 |
pPacket->Params[0] = 0;
|
|
289 |
pPacket->Params[1] = 0;
|
|
290 |
pPacket->Params[2] = 0;
|
|
291 |
pPacket->Params[3] = 0;
|
|
292 |
Datalen = 0;
|
|
293 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
294 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
295 |
break;
|
|
296 |
case KLCmdGetEventLog:
|
|
297 |
pPacket->Params[0] = nAddr % 256;
|
|
298 |
pPacket->Params[1] = nAddr / 256;
|
|
299 |
pPacket->Params[2] = nCount % 256;
|
|
300 |
pPacket->Params[3] = nCount / 256;
|
|
301 |
Datalen = 0;
|
|
302 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
303 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
304 |
break;
|
|
305 |
|
|
306 |
default:
|
|
307 |
break;
|
|
308 |
}
|
|
309 |
|
|
310 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
311 |
{
|
|
312 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
313 |
pPacket->Params[PkgLen1 - sizeof(stKLReqPacket)] = BCC;
|
|
314 |
}
|
|
315 |
|
|
316 |
if (pStat->HasExt && ExtFrameLen > 0 && pExtDataFrame != NULL)
|
|
317 |
{
|
|
318 |
|
|
319 |
memcpy(pPacket->Params + PkgLen1 - sizeof(stKLReqPacket) + 1, pExtDataFrame, ExtFrameLen);
|
|
320 |
PkgLen1 += ExtFrameLen;
|
|
321 |
};
|
|
322 |
return PkgLen1;
|
|
323 |
}
|
|
324 |
|
|
325 |
|
|
326 |
int KLinkProtocol::CheckPackage(void * pBuf, UINT nSize)
|
|
327 |
{
|
|
328 |
pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
|
|
329 |
if (p1->RplyStSgn != KLSignReply)
|
|
330 |
{
|
|
331 |
return KL_UNKNOWN;
|
|
332 |
}
|
|
333 |
|
|
334 |
int DataLen = 0; //p1->LoadLen;
|
|
335 |
if (DataLen > KLMaxPacketLength)
|
|
336 |
{
|
|
337 |
return KL_PKG_ERR;
|
|
338 |
}
|
|
339 |
if (nSize < DataLen + sizeof(pKLRplyPktHdr))
|
|
340 |
{
|
|
341 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
342 |
//PutStr(str3,len4);
|
|
343 |
return KL_PKG_ERR; //not long enough
|
|
344 |
}
|
|
345 |
// if (p1->data[DataLen+1] != EndSign)
|
|
346 |
// {
|
|
347 |
// ClientStat[nCurPollId].NoEndErr++;
|
|
348 |
// Uart2Stat.LengthErr++;
|
|
349 |
// return -2;
|
|
350 |
// }
|
|
351 |
unsigned char thisBCC = KLBCC(p1, nSize - 1);
|
|
352 |
if (thisBCC != ((UCHAR *)pBuf)[nSize - 1])
|
|
353 |
{//BCC Error;
|
|
354 |
return KL_BCC_ERR;
|
|
355 |
}
|
|
356 |
return KL_OK;
|
|
357 |
|
|
358 |
return true;
|
|
359 |
return false;
|
|
360 |
}
|
|
361 |
|
|
362 |
int KLinkProtocol::ProcessPacket(void *pBuf, int nLen)
|
|
363 |
{
|
|
364 |
return false;
|
|
365 |
}
|
|
366 |
|
|
367 |
int KLinkProtocol::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData)
|
|
368 |
{
|
|
369 |
int res = CheckPackage(pBuf, nPkgLen);
|
|
370 |
if (res != KL_OK) return res;
|
|
371 |
int len1 = 0;
|
|
372 |
pKLRplyPktHdr pPacket = (pKLRplyPktHdr)pBuf;
|
|
373 |
UCHAR Sign = pPacket->RplyStSgn;
|
|
374 |
UCHAR nDst= pPacket->DstAddr;
|
|
375 |
*nCmd = pPacket->nRplyCMD;
|
|
376 |
UCHAR nStatus = pPacket->nStatus;
|
|
377 |
*Status = nStatus;
|
|
378 |
m_nRSeq = ((pKLStat)&nStatus)->nSEQ;
|
|
379 |
int Datalen = pPacket->nSize1;
|
|
380 |
*nCount = Datalen;
|
|
381 |
switch (*nCmd)
|
|
382 |
{
|
|
383 |
case KLCmdNone:
|
|
384 |
break;
|
|
385 |
case KLCmdPing:
|
|
386 |
case KLCmdPingReply:
|
|
387 |
*nCount = Datalen;
|
|
388 |
memcpy(pPacket->Datas, pData, Datalen);
|
|
389 |
break;
|
|
390 |
case KLCmdGetInfo:
|
|
391 |
case KLCmdVerInfo:
|
|
392 |
*nCount = Datalen;
|
|
393 |
memcpy(pPacket->Datas, pData, Datalen);
|
|
394 |
break;
|
|
395 |
case KLCmdRead:
|
|
396 |
case KLCmdReadReply:
|
|
397 |
*nCount = Datalen;
|
|
398 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
399 |
break;
|
|
400 |
case KLCmdWrite:
|
|
401 |
case KLCmdWriteReply:
|
|
402 |
*nCount = Datalen;
|
|
403 |
break;
|
|
404 |
case KLCmdRead1Bit:
|
|
405 |
case KLCmdReadBits:
|
|
406 |
*nCount = Datalen;
|
|
407 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
408 |
break;
|
|
409 |
case KLCmdReadDataByte:
|
|
410 |
*nCount = Datalen;
|
|
411 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
412 |
break;
|
|
413 |
case KLCmdReadDataWord:
|
|
414 |
case KLCmdReadData:
|
|
415 |
*nCount = Datalen;
|
|
416 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
417 |
break;
|
|
418 |
case KLCmdWriteDataByte:
|
|
419 |
case KLCmdWriteDataWord:
|
|
420 |
case KLCmdWriteData:
|
|
421 |
*nCount = Datalen;
|
|
422 |
break;
|
|
423 |
|
|
424 |
case KLCmdSaveToFlash:
|
|
425 |
break;
|
|
426 |
case KLCmdExChgData:
|
|
427 |
case KLCmdExChgDataReply:
|
|
428 |
break;
|
|
429 |
case KLCmdGetEventLogCount:
|
|
430 |
*nCount = Datalen;
|
|
431 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
432 |
break;
|
|
433 |
case KLCmdGetEventLog:
|
|
434 |
*nCount = Datalen;
|
|
435 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
436 |
break;
|
|
437 |
|
|
438 |
case KLCmdErrRply:
|
|
439 |
*nCount = Datalen;
|
|
440 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
441 |
break;
|
|
442 |
default:
|
|
443 |
break;
|
|
444 |
}
|
|
445 |
return KL_OK;
|
|
446 |
|
|
447 |
return false;
|
|
448 |
}
|
|
449 |
|
|
450 |
int KLinkProtocol::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value)
|
|
451 |
{
|
|
452 |
m_Dst = nDst;
|
|
453 |
int len1=MakeReqPacket(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr);
|
|
454 |
SendPacket(m_Packetbuf, len1);
|
|
455 |
int len2=RecvPacket(m_RecvBuf, 64);
|
|
456 |
unsigned char nCmd;
|
|
457 |
unsigned short nCount;
|
|
458 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
459 |
Value[0] = m_DataBuf[0];
|
|
460 |
return KL_OK;
|
|
461 |
}
|
|
462 |
int KLinkProtocol::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value)
|
|
463 |
{
|
|
464 |
m_Dst = nDst;
|
|
465 |
m_DataBuf[0] = Value;
|
|
466 |
int len1 = MakeReqPacket(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr,1,m_DataBuf);
|
|
467 |
SendPacket(m_Packetbuf, len1);
|
|
468 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
469 |
unsigned char nCmd;
|
|
470 |
unsigned short nCount;
|
|
471 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
472 |
return KL_OK;
|
|
473 |
}
|
|
474 |
int KLinkProtocol::ReadBits(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
|
475 |
{
|
|
476 |
return KL_OK;
|
|
477 |
}
|
|
478 |
int KLinkProtocol::WriteBits(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
|
479 |
{
|
|
480 |
return KL_OK;
|
|
481 |
}
|
|
482 |
int KLinkProtocol::ReadBitsByWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
|
483 |
{
|
|
484 |
return KL_OK;
|
|
485 |
}
|
|
486 |
int KLinkProtocol::WriteBitsByWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
|
487 |
{
|
|
488 |
return KL_OK;
|
|
489 |
}
|
|
490 |
int KLinkProtocol::GetNextSeq()
|
|
491 |
{
|
|
492 |
m_nSeq++;
|
|
493 |
if (m_nSeq >= 8) m_nSeq = 0;
|
|
494 |
m_Stat1.nSEQ = m_nSeq;
|
|
495 |
return m_nSeq;
|
|
496 |
}
|
|
497 |
|
|
498 |
int KLinkProtocol::ReadDataByte(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
|
499 |
{
|
|
500 |
m_Dst = nDst;
|
|
501 |
m_resultStr.Empty();
|
|
502 |
UCHAR nExpSeq = GetNextSeq();
|
|
503 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr/2, nCount);
|
|
504 |
SendPacket(m_Packetbuf, len1);
|
|
505 |
int numToRead = sizeof(stKLRplyPktHdr) + nCount;
|
|
506 |
int len2 = 0;
|
|
507 |
int nTryCount = 0;
|
|
508 |
for (int i = 0; i < 2; i++)
|
|
509 |
{
|
|
510 |
nTryCount++;
|
|
511 |
len2 += RecvPacket(m_RecvBuf + len2, numToRead - len2);
|
|
512 |
if (len2 >= numToRead) break;
|
|
513 |
}
|
|
514 |
if (len2 <= 0) return KL_ERR;
|
|
515 |
if (len2 < numToRead)
|
|
516 |
{
|
|
517 |
CString s1;
|
|
518 |
for (int i=0;i<len2;i++)
|
|
519 |
{
|
|
520 |
s1.AppendFormat(_T("%02X "),m_RecvBuf[i]);
|
|
521 |
}
|
|
522 |
s1.Append(_T("\r\n"));
|
|
523 |
m_resultStr.Format(_T("ToRead %d R= %d Try %d \r\n"),numToRead,len2,nTryCount);
|
|
524 |
m_resultStr += s1;
|
|
525 |
}
|
|
526 |
unsigned char nCmd ;
|
|
527 |
unsigned short nnCount;
|
|
528 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
529 |
if (res == KL_OK)
|
|
530 |
{
|
|
531 |
for (int i = 0; i < nnCount; i++)
|
|
532 |
{ Values[i] = m_DataBuf[i]; }
|
|
533 |
}
|
|
534 |
else
|
|
535 |
{
|
|
536 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
537 |
return res;
|
|
538 |
}
|
|
539 |
|
|
540 |
return KL_OK;
|
|
541 |
}
|
|
542 |
int KLinkProtocol::WriteDataByte(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
|
543 |
{
|
|
544 |
m_Dst = nDst;
|
|
545 |
UCHAR nExpSeq = GetNextSeq();
|
|
546 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr/2, nCount, Values);
|
|
547 |
SendPacket(m_Packetbuf, len1);
|
|
548 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
549 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
550 |
if (len2 == 0) return KL_ERR;
|
|
551 |
unsigned char nCmd;
|
|
552 |
unsigned short nnCount;
|
|
553 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
554 |
return res;
|
|
555 |
}
|
|
556 |
|
|
557 |
int KLinkProtocol::ReadDataWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
558 |
{
|
|
559 |
m_Dst = nDst;
|
|
560 |
UCHAR nExpSeq = GetNextSeq();
|
|
561 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nCount);
|
|
562 |
SendPacket(m_Packetbuf, len1);
|
|
563 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
564 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
565 |
if (len2 == 0) return KL_ERR;
|
|
566 |
unsigned char nCmd;
|
|
567 |
unsigned short nnCount;
|
|
568 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
569 |
Values[0] = m_DataBuf[0];
|
|
570 |
return res;
|
|
571 |
}
|
|
572 |
int KLinkProtocol::WriteDataWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
573 |
{
|
|
574 |
m_Dst = nDst;
|
|
575 |
UCHAR nExpSeq = GetNextSeq();
|
|
576 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nCount, Values);
|
|
577 |
SendPacket(m_Packetbuf, len1);
|
|
578 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
579 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
580 |
if (len2 == 0) return KL_ERR;
|
|
581 |
unsigned char nCmd;
|
|
582 |
unsigned short nnCount;
|
|
583 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
584 |
Values[0] = m_DataBuf[0];
|
|
585 |
return res;
|
|
586 |
}
|
|
587 |
|
|
588 |
int KLinkProtocol::ReadData(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
589 |
{
|
|
590 |
m_Dst = nDst;
|
|
591 |
UCHAR nExpSeq = GetNextSeq();
|
|
592 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr,nCount);
|
|
593 |
SendPacket(m_Packetbuf, len1);
|
|
594 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
595 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
596 |
if (len2 == 0) return KL_ERR;
|
|
597 |
unsigned char nCmd;
|
|
598 |
unsigned short nnCount;
|
|
599 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
600 |
for (int i=0;i<nnCount;i++) Values[i] = m_DataBuf[i];
|
|
601 |
return res;
|
|
602 |
|
|
603 |
return KL_OK;
|
|
604 |
}
|
|
605 |
int KLinkProtocol::WriteData(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
606 |
{
|
|
607 |
m_Dst = nDst;
|
|
608 |
UCHAR nExpSeq = GetNextSeq();
|
|
609 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nCount, Values);
|
|
610 |
SendPacket(m_Packetbuf, len1);
|
|
611 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
612 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
613 |
if (len2 == 0) return KL_ERR;
|
|
614 |
unsigned char nCmd;
|
|
615 |
unsigned short nnCount;
|
|
616 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
617 |
// Values[0] = m_DataBuf[0];
|
|
618 |
return res;
|
|
619 |
|
|
620 |
return KL_OK;
|
|
621 |
}
|
|
622 |
int KLinkProtocol::GetInfo(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
623 |
{
|
|
624 |
m_Dst = nDst;
|
|
625 |
UCHAR nExpSeq = GetNextSeq();
|
|
626 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nCount, Values);
|
|
627 |
SendPacket(m_Packetbuf, len1);
|
|
628 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
629 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
630 |
if (len2 == 0) return KL_ERR;
|
|
631 |
unsigned char nCmd;
|
|
632 |
unsigned short nnCount;
|
|
633 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
634 |
// Values[0] = m_DataBuf[0];
|
|
635 |
return res;
|
|
636 |
|
|
637 |
return KL_OK;
|
|
638 |
}
|
|
639 |
int KLinkProtocol::GetEventLogCount(UCHAR nDst, int * nCount)
|
|
640 |
{
|
|
641 |
m_Dst = nDst;
|
|
642 |
UCHAR nExpSeq = GetNextSeq();
|
|
643 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLogCount, 0);
|
|
644 |
SendPacket(m_Packetbuf, len1);
|
|
645 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
646 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
647 |
if (len2 == 0) return KL_ERR;
|
|
648 |
unsigned char nCmd;
|
|
649 |
unsigned short nnCount;
|
|
650 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
651 |
nCount[0] = *(int *)m_DataBuf;
|
|
652 |
return res;
|
|
653 |
|
|
654 |
return KL_OK;
|
|
655 |
}
|
|
656 |
|
|
657 |
int KLinkProtocol::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
|
658 |
{
|
|
659 |
m_Dst = nDst;
|
|
660 |
UCHAR nExpSeq = GetNextSeq();
|
|
661 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLog, 0, nStartIndex, nCount, 0);
|
|
662 |
SendPacket(m_Packetbuf, len1);
|
|
663 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
664 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
665 |
if (len2 == 0) return KL_ERR;
|
|
666 |
unsigned char nCmd;
|
|
667 |
unsigned short nnCount;
|
|
668 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
669 |
|
|
670 |
memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
|
|
671 |
|
|
672 |
return res;
|
|
673 |
|
|
674 |
return KL_OK;
|
|
675 |
}
|
|
676 |
|