提交 | 用户 | age
|
418cb3
|
1 |
#include "pch.h"
|
Q |
2 |
#include "KLink.h"
|
|
3 |
|
|
4 |
KLink::KLink()
|
|
5 |
{
|
|
6 |
|
|
7 |
}
|
|
8 |
|
|
9 |
KLink::~KLink()
|
|
10 |
{
|
|
11 |
|
|
12 |
}
|
|
13 |
|
|
14 |
static const KLink::stKLTypeName KLCoilTypeNames[] =
|
|
15 |
{
|
|
16 |
{KLink::KLCoilTypeX,"X",KLink::KLCoilXCount},
|
|
17 |
{KLink::KLCoilTypeY,"Y",KLink::KLCoilYCount},
|
|
18 |
{KLink::KLCoilTypeR,"R",KLink::KLCoilRCount},
|
|
19 |
{KLink::KLCoilTypeLX,"LX",KLink::KLCoilLXCount},
|
|
20 |
{KLink::KLCoilTypeLY,"LY",KLink::KLCoilLYCount},
|
|
21 |
{KLink::KLCoilTypeT,"T",KLink::KLCoilTCount},
|
|
22 |
{KLink::KLCoilTypeC,"C",KLink::KLCoilCCount}
|
|
23 |
};
|
|
24 |
|
|
25 |
static const KLink::stKLTypeName KLDataTypeNames[] =
|
|
26 |
{
|
|
27 |
|
|
28 |
KLink::KLDataTypeWX,"WX", KLink::KLDataWXCount,
|
|
29 |
KLink::KLDataTypeWY,"WY", KLink::KLDataWYCount,
|
|
30 |
KLink::KLDataTypeWR,"WR",KLink::KLDataWRCount,
|
|
31 |
KLink::KLDataTypeWLX,"WLX",KLink::KLDataWLCount,
|
|
32 |
KLink::KLDataTypeWLY,"WLY",KLink::KLDataWLCount,
|
|
33 |
KLink::KLDataTypeDT,"DT",KLink::KLDataDTCount,
|
|
34 |
KLink::KLDataTypeSDT,"SDT",KLink::KLDataSDTCount,
|
|
35 |
KLink::KLDataTypeSV,"SV",KLink::KLDataSVCount ,
|
|
36 |
KLink::KLDataTypeEV,"EV",KLink::KLDataEVCount ,
|
|
37 |
KLink::KLDataTypeLD,"LD",KLink::KLDataLDCount,
|
|
38 |
KLink::KLDataSysCfg,"Cfg",128,
|
|
39 |
KLink::KLDataTypeFlash,"Flash",256,
|
|
40 |
KLink::KLDataTypeTest,"Test",256
|
|
41 |
};
|
|
42 |
|
|
43 |
const char * KLink::GetErrDescStr(int nErrNo)
|
|
44 |
{
|
|
45 |
switch (nErrNo)
|
|
46 |
{
|
|
47 |
case KL_ERR:
|
|
48 |
return "ERROR";
|
|
49 |
case KL_OK: //=0
|
|
50 |
return "OK";
|
|
51 |
case KL_NG:// = 1,
|
|
52 |
return "NG";
|
|
53 |
case KL_UNKNOWN: // = 2,
|
|
54 |
return "KL_UNKNOWN";
|
|
55 |
case KL_TIMEOUT: // = 3,
|
|
56 |
return "KL_TIMEOUT";
|
|
57 |
case KL_BCC_ERR: // = 4,
|
|
58 |
return "KL_BCC_ERR";
|
|
59 |
case KL_PKG_ERR: // = 5,
|
|
60 |
return "KL_PKG_ERR";
|
|
61 |
case KL_SEQ_ERR: // = 6,
|
|
62 |
return "KL_SEQ_ERR";
|
|
63 |
case KL_LENTH_EXCEED: //,
|
|
64 |
return "KL_LENTH_EXCEED";
|
|
65 |
case KL_NEED_PASS: //,
|
|
66 |
return "KL_NEED_PASS";
|
|
67 |
case KL_NOT_SUPPORT://,
|
|
68 |
return "KL_NOT_SUPPORT";
|
|
69 |
default:
|
|
70 |
return "KL_OTHER_ERR";
|
|
71 |
break;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
int KLink::xtoi(const char * hexstr, int len)
|
|
76 |
{
|
|
77 |
int len1 = len;
|
|
78 |
if (len <= 0) len1 = (int)strlen(hexstr);
|
|
79 |
int j=0;
|
|
80 |
for (int i = 0; i < len1; i++)
|
|
81 |
{
|
|
82 |
unsigned char ch = hexstr[i];
|
|
83 |
if (ch >= '0'&&ch <= '9') { int k = ch - '0'; j = j * 16 + k; continue; }
|
|
84 |
if (ch >= 'A'&&ch <= 'F') { int k = ch - 'A' + 10; j = j * 16 + k; continue; }
|
|
85 |
if (ch >= 'a'&&ch <= 'f') { int k = ch - 'a' + 10; j = j * 16 + k; continue; }
|
|
86 |
if (ch == ' ' || ch == ' ') { continue; }
|
|
87 |
break;
|
|
88 |
}
|
|
89 |
return j;
|
|
90 |
}
|
|
91 |
|
|
92 |
unsigned char KLink::KLBCC(void * pData, int nSize)
|
|
93 |
{
|
|
94 |
unsigned char k = 0;
|
|
95 |
for (int i = 0; i < nSize; i++)
|
|
96 |
{
|
|
97 |
k ^= ((unsigned char *)pData)[i];
|
|
98 |
}
|
|
99 |
return k;
|
|
100 |
}
|
|
101 |
int KLink::CalCRC(void * pBuf, int nSize)
|
|
102 |
{
|
|
103 |
return 0;
|
|
104 |
}
|
|
105 |
|
|
106 |
int KLink::SetCallBackFuncs(pCallBackFuncs pFuncSt)
|
|
107 |
{
|
|
108 |
if (pFuncSt->OpenFunc) { m_CallBackFuncs.OpenFunc = pFuncSt->OpenFunc; m_bOpenCallBackSet = 1; }
|
|
109 |
if (pFuncSt->CloseFunc) { m_CallBackFuncs.CloseFunc = pFuncSt->CloseFunc; m_bCloseCallBackSet = 1; }
|
|
110 |
if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; }
|
|
111 |
if (pFuncSt->RecvPkgFunc) { m_CallBackFuncs.RecvPkgFunc = pFuncSt->RecvPkgFunc; m_bRecvCallBackSet = 1; }
|
|
112 |
if (pFuncSt->ClearBufFunc) { m_CallBackFuncs.ClearBufFunc = pFuncSt->ClearBufFunc; m_bClearBufCallBackSet = 1; }
|
|
113 |
// if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; }
|
|
114 |
|
|
115 |
return KL_OK;
|
|
116 |
}
|
|
117 |
|
|
118 |
int KLink::MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData)
|
|
119 |
{
|
|
120 |
int framelen1 = 0;
|
|
121 |
pKLExtDataFrame pF1 = (pKLExtDataFrame)pBuf;
|
|
122 |
pF1->Dst = nDst;
|
|
123 |
pF1->nType = nType;
|
|
124 |
pF1->Len2 = len2;
|
|
125 |
memcpy(pF1->Data, pData, len2);
|
|
126 |
UCHAR bcc = KLBCC(pBuf, sizeof(tagKLExtDataFrame) + len2 - 1);
|
|
127 |
pF1->Data[len2] = bcc;
|
|
128 |
|
|
129 |
framelen1 = sizeof(tagKLExtDataFrame) + len2;
|
|
130 |
return framelen1;
|
|
131 |
}
|
|
132 |
/*
|
|
133 |
int KLink::MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData)
|
|
134 |
{
|
|
135 |
int PkgLen1 = 0;
|
|
136 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
137 |
pPacket->StartSign = KLSignStart;
|
|
138 |
pPacket->DstAddr = nDst;
|
|
139 |
pPacket->Stat = Stat;
|
|
140 |
pPacket->Cmd = nCMD;
|
|
141 |
pPacket->Type1 = Type;
|
|
142 |
int Datalen = 0;
|
|
143 |
int nWordAddr;
|
|
144 |
int nWordCount;
|
|
145 |
|
|
146 |
switch (nCMD)
|
|
147 |
{
|
|
148 |
case KLCmdNone:
|
|
149 |
break;
|
|
150 |
case KLCmdPing:
|
|
151 |
Datalen = Type;
|
|
152 |
memcpy(pPacket->Params, pData, Datalen);
|
|
153 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
154 |
break;
|
|
155 |
case KLCmdGetInfo:
|
|
156 |
break;
|
|
157 |
case KLCmdRead:
|
|
158 |
Datalen = 0;
|
|
159 |
pPacket->Params[0] = nAddr % 256;
|
|
160 |
pPacket->Params[1] = nAddr / 256;
|
|
161 |
pPacket->Params[2] = nCount % 256;
|
|
162 |
pPacket->Params[3] = nCount / 256;
|
|
163 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
164 |
break;
|
|
165 |
case KLCmdWrite:
|
|
166 |
pPacket->Params[0] = nAddr % 256;
|
|
167 |
pPacket->Params[1] = nAddr / 256;
|
|
168 |
pPacket->Params[2] = nCount % 256;
|
|
169 |
pPacket->Params[3] = nCount / 256;
|
|
170 |
Datalen = nCount;
|
|
171 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
172 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
173 |
|
|
174 |
break;
|
|
175 |
case KLCmdSaveToFlash:
|
|
176 |
break;
|
|
177 |
case KLCmdExChgData:
|
|
178 |
break;
|
|
179 |
case KLCmdGetEventLogCount:
|
|
180 |
pPacket->Params[0] = 0;
|
|
181 |
pPacket->Params[1] = 0;
|
|
182 |
pPacket->Params[2] = 0;
|
|
183 |
pPacket->Params[3] = 0;
|
|
184 |
Datalen = 0;
|
|
185 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
186 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
187 |
default:
|
|
188 |
break;
|
|
189 |
}
|
|
190 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
191 |
{
|
|
192 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
193 |
pPacket->Params[PkgLen1-sizeof(stKLReqPacket)] = BCC;
|
|
194 |
}
|
|
195 |
return PkgLen1;
|
|
196 |
}
|
|
197 |
*/
|
|
198 |
int KLink::MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData, int ExtFrameLen, void * pExtDataFrame)
|
|
199 |
{
|
|
200 |
int PkgLen1 = 0;
|
|
201 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
202 |
pPacket->StartSign = KLSignStart;
|
|
203 |
pPacket->DstAddr = nDst;
|
|
204 |
pPacket->Stat = Stat;
|
|
205 |
pPacket->Cmd = nCMD;
|
|
206 |
pPacket->Type1 = Type;
|
|
207 |
|
|
208 |
pKLStat pStat = (pKLStat)&(pPacket->Stat);
|
|
209 |
int Datalen = 0;
|
|
210 |
int nWordAddr=nAddr;
|
|
211 |
int nWordCount=nCount;
|
|
212 |
pPacket->Params[0] = LOBYTE(nAddr);
|
|
213 |
pPacket->Params[1] = HIBYTE(nAddr);
|
|
214 |
pPacket->Params[2] = nCount % 256;
|
|
215 |
pPacket->Params[3] = nCount / 256;
|
|
216 |
|
|
217 |
switch (nCMD)
|
|
218 |
{
|
|
219 |
case KLCmdNone:
|
|
220 |
break;
|
|
221 |
case KLCmdPing:
|
|
222 |
Datalen = Type;
|
|
223 |
memcpy(pPacket->Params, pData, Datalen);
|
|
224 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
225 |
break;
|
|
226 |
case KLCmdGetInfo:
|
|
227 |
case KLCmdGetSN:
|
|
228 |
Datalen = 0;
|
|
229 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
230 |
break;
|
|
231 |
case KLCmdGetFactoryData:
|
|
232 |
Datalen = 0;
|
|
233 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
234 |
break;
|
|
235 |
case KLCmdWriteFactoryData:
|
|
236 |
Datalen = nCount;
|
|
237 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
238 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
239 |
break;
|
|
240 |
case KLCmdRead:
|
|
241 |
Datalen = 0;
|
|
242 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
243 |
break;
|
|
244 |
case KLCmdWrite:
|
|
245 |
Datalen = nCount;
|
|
246 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
247 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
248 |
break;
|
|
249 |
case KLCmdRead1Bit:
|
|
250 |
Datalen = 0;
|
|
251 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
252 |
break;
|
|
253 |
case KLCmdWrite1Bit:
|
|
254 |
Datalen = 1;
|
|
255 |
memcpy(pPacket->Params + 2, pData, Datalen);
|
|
256 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
257 |
break;
|
|
258 |
|
|
259 |
case KLCmdReadBits:
|
|
260 |
Datalen = 0;
|
|
261 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
262 |
break;
|
|
263 |
case KLCmdWriteBits:
|
|
264 |
Datalen = 0;
|
|
265 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
266 |
break;
|
|
267 |
case KLCmdReadDataByte:
|
|
268 |
case KLCmdReadDataWord:
|
|
269 |
case KLCmdReadData:
|
|
270 |
Datalen = 0;
|
|
271 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
272 |
break;
|
|
273 |
case KLCmdWriteDataByte:
|
|
274 |
case KLCmdWriteDataWord:
|
|
275 |
case KLCmdWriteData:
|
|
276 |
Datalen = nCount;
|
|
277 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
278 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
279 |
break;
|
|
280 |
case KLCmdSaveToFlash:
|
|
281 |
break;
|
|
282 |
case KLCmdExChgData:
|
|
283 |
break;
|
|
284 |
case KLCmdReadProgram:
|
|
285 |
Datalen = 0;
|
|
286 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
287 |
break;
|
|
288 |
case KLCmdStartProgram:
|
|
289 |
Datalen = 0;
|
|
290 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
291 |
break;
|
|
292 |
case KLCmdWriteProgram:
|
|
293 |
Datalen = nCount;
|
|
294 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
295 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
296 |
break;
|
|
297 |
case KLCmdFinishProgram:
|
|
298 |
Datalen = 0;
|
|
299 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
300 |
break;
|
|
301 |
|
|
302 |
case KLCmdReadRunStat:
|
|
303 |
Datalen = 0;
|
|
304 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
305 |
break;
|
|
306 |
|
|
307 |
case KLCmdSaveSysCfg:
|
|
308 |
pPacket->Params[0] = 0;
|
|
309 |
pPacket->Params[1] = 0;
|
|
310 |
pPacket->Params[2] = 0;
|
|
311 |
pPacket->Params[3] = 0;
|
|
312 |
Datalen = 0;
|
|
313 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
314 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
315 |
break;
|
|
316 |
case KLCmdSaveRunStat:
|
|
317 |
pPacket->Params[0] = 0;
|
|
318 |
pPacket->Params[1] = 0;
|
|
319 |
pPacket->Params[2] = 0;
|
|
320 |
pPacket->Params[3] = 0;
|
|
321 |
Datalen = 0;
|
|
322 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
323 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
324 |
break;
|
|
325 |
case KLCmdClearEventLog:
|
|
326 |
pPacket->Params[0] = 0;
|
|
327 |
pPacket->Params[1] = 0;
|
|
328 |
pPacket->Params[2] = 0;
|
|
329 |
pPacket->Params[3] = 0;
|
|
330 |
Datalen = 0;
|
|
331 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
332 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
333 |
|
|
334 |
case KLCmdGetEventLogCount:
|
|
335 |
pPacket->Params[0] = 0;
|
|
336 |
pPacket->Params[1] = 0;
|
|
337 |
pPacket->Params[2] = 0;
|
|
338 |
pPacket->Params[3] = 0;
|
|
339 |
Datalen = 0;
|
|
340 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
341 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
342 |
break;
|
|
343 |
case KLCmdGetEventLog:
|
|
344 |
pPacket->Params[0] = nAddr % 256;
|
|
345 |
pPacket->Params[1] = nAddr / 256;
|
|
346 |
pPacket->Params[2] = nCount % 256;
|
|
347 |
pPacket->Params[3] = nCount / 256;
|
|
348 |
Datalen = 0;
|
|
349 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
350 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
351 |
break;
|
|
352 |
|
|
353 |
default:
|
|
354 |
pPacket->Params[0] = nAddr % 256;
|
|
355 |
pPacket->Params[1] = nAddr / 256;
|
|
356 |
pPacket->Params[2] = nCount % 256;
|
|
357 |
pPacket->Params[3] = nCount / 256;
|
|
358 |
Datalen = 0;
|
|
359 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
360 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
361 |
break;
|
|
362 |
}
|
|
363 |
|
|
364 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
365 |
{
|
|
366 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
367 |
pPacket->Params[PkgLen1 - sizeof(stKLReqPacket)] = BCC;
|
|
368 |
}
|
|
369 |
|
|
370 |
if (pStat->HasExt && ExtFrameLen > 0 && pExtDataFrame != NULL)
|
|
371 |
{
|
|
372 |
|
|
373 |
memcpy(pPacket->Params + PkgLen1 - sizeof(stKLReqPacket) + 1, pExtDataFrame, ExtFrameLen);
|
|
374 |
PkgLen1 += ExtFrameLen;
|
|
375 |
};
|
|
376 |
return PkgLen1;
|
|
377 |
}
|
|
378 |
|
|
379 |
|
|
380 |
int KLink::CheckPackage(void * pBuf, int nSize)
|
|
381 |
{
|
|
382 |
pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
|
|
383 |
if (p1->RplyStSgn != KLSignReply)
|
|
384 |
{
|
|
385 |
return KL_UNKNOWN;
|
|
386 |
}
|
|
387 |
|
|
388 |
int DataLen = 0; //p1->LoadLen;
|
|
389 |
if (DataLen > KLMaxPacketLength)
|
|
390 |
{
|
|
391 |
return KL_PKG_ERR;
|
|
392 |
}
|
|
393 |
if (nSize < DataLen + sizeof(stKLRplyPktHdr))
|
|
394 |
{
|
|
395 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
396 |
//PutStr(str3,len4);
|
|
397 |
return KL_PKG_ERR; //not long enough
|
|
398 |
}
|
|
399 |
// if (p1->data[DataLen+1] != EndSign)
|
|
400 |
// {
|
|
401 |
// ClientStat[nCurPollId].NoEndErr++;
|
|
402 |
// Uart2Stat.LengthErr++;
|
|
403 |
// return -2;
|
|
404 |
// }
|
|
405 |
unsigned char thisBCC = KLBCC(p1, nSize - 1);
|
|
406 |
if (thisBCC != ((UCHAR *)pBuf)[nSize - 1])
|
|
407 |
{//BCC Error;
|
|
408 |
return KL_BCC_ERR;
|
|
409 |
}
|
|
410 |
return KL_OK;
|
|
411 |
|
|
412 |
return true;
|
|
413 |
return false;
|
|
414 |
}
|
|
415 |
|
|
416 |
int KLink::ProcessPacket(void *pBuf, int nLen)
|
|
417 |
{
|
|
418 |
return false;
|
|
419 |
}
|
|
420 |
|
|
421 |
int KLink::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData)
|
|
422 |
{
|
|
423 |
if (nPkgLen < 0) { return KL_ERR; }
|
|
424 |
int res = CheckPackage(pBuf, nPkgLen);
|
|
425 |
if (res != KL_OK) return res;
|
|
426 |
int len1 = 0;
|
|
427 |
pKLRplyPktHdr pPacket = (pKLRplyPktHdr)pBuf;
|
|
428 |
UCHAR Sign = pPacket->RplyStSgn;
|
|
429 |
UCHAR nDst= pPacket->DstAddr;
|
|
430 |
*nCmd = pPacket->nRplyCMD;
|
|
431 |
UCHAR nStatus = pPacket->nStatus;
|
|
432 |
*Status = nStatus;
|
|
433 |
m_nRSeq = ((pKLStat)&nStatus)->nSEQ;
|
|
434 |
int Datalen = pPacket->nSize1;
|
|
435 |
*nCount = Datalen;
|
|
436 |
switch (*nCmd)
|
|
437 |
{
|
|
438 |
case KLCmdNone:
|
|
439 |
break;
|
|
440 |
case KLCmdPing:
|
|
441 |
case KLCmdPingReply:
|
|
442 |
*nCount = Datalen;
|
|
443 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
444 |
break;
|
|
445 |
case KLCmdGetInfo:
|
|
446 |
case KLCmdVerInfo:
|
|
447 |
*nCount = Datalen;
|
|
448 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
449 |
break;
|
|
450 |
case KLCmdGetUid:
|
|
451 |
case KLCmdGetSN:
|
|
452 |
case KLCmdGetFactoryData:
|
|
453 |
*nCount = Datalen;
|
|
454 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
455 |
break;
|
|
456 |
case KLCmdWriteFactoryData:
|
|
457 |
*nCount = Datalen;
|
|
458 |
break;
|
|
459 |
case KLCmdRead:
|
|
460 |
case KLCmdReadReply:
|
|
461 |
*nCount = Datalen;
|
|
462 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
463 |
break;
|
|
464 |
case KLCmdWrite:
|
|
465 |
case KLCmdWriteReply:
|
|
466 |
*nCount = Datalen;
|
|
467 |
break;
|
|
468 |
case KLCmdRead1Bit:
|
|
469 |
case KLCmdReadBits:
|
|
470 |
*nCount = Datalen;
|
|
471 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
472 |
break;
|
|
473 |
case KLCmdReadDataByte:
|
|
474 |
*nCount = Datalen;
|
|
475 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
476 |
break;
|
|
477 |
case KLCmdReadDataWord:
|
|
478 |
case KLCmdReadData:
|
|
479 |
*nCount = Datalen;
|
|
480 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
481 |
break;
|
|
482 |
case KLCmdWriteDataByte:
|
|
483 |
case KLCmdWriteDataWord:
|
|
484 |
case KLCmdWriteData:
|
|
485 |
*nCount = Datalen;
|
|
486 |
break;
|
|
487 |
|
|
488 |
case KLCmdSaveToFlash:
|
|
489 |
break;
|
|
490 |
|
|
491 |
case KLCmdReadProgram:
|
|
492 |
*nCount = Datalen;
|
|
493 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
494 |
break;
|
|
495 |
case KLCmdStartProgram:
|
|
496 |
*nCount =Datalen;
|
|
497 |
break;
|
|
498 |
case KLCmdWriteProgram:
|
|
499 |
*nCount = Datalen;
|
|
500 |
break;
|
|
501 |
case KLCmdFinishProgram:
|
|
502 |
*nCount = Datalen;
|
|
503 |
break;
|
|
504 |
|
|
505 |
case KLCmdReadRunStat:
|
|
506 |
*nCount = Datalen;
|
|
507 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
508 |
break;
|
|
509 |
case KLCmdExChgData:
|
|
510 |
case KLCmdExChgDataReply:
|
|
511 |
break;
|
|
512 |
case KLCmdGetEventLogCount:
|
|
513 |
*nCount = Datalen;
|
|
514 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
515 |
break;
|
|
516 |
case KLCmdGetEventLog:
|
|
517 |
*nCount = Datalen;
|
|
518 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
519 |
break;
|
|
520 |
|
|
521 |
case KLCmdErrRply:
|
|
522 |
*nCount = Datalen;
|
|
523 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
524 |
break;
|
|
525 |
default:
|
|
526 |
*nCount = Datalen;
|
|
527 |
//memcpy(pData, pPacket->Datas, Datalen);
|
|
528 |
break;
|
|
529 |
}
|
|
530 |
return KL_OK;
|
|
531 |
|
|
532 |
return false;
|
|
533 |
}
|
|
534 |
|
|
535 |
int KLink::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value)
|
|
536 |
{
|
|
537 |
m_Dst = nDst;
|
|
538 |
int len1=MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr);
|
|
539 |
SendPacket(m_Packetbuf, len1);
|
|
540 |
int numToRead = sizeof(stKLRplyPktHdr) + 1;
|
|
541 |
|
|
542 |
int len2=RecvPacket(m_RecvBuf, numToRead);
|
|
543 |
unsigned char nCmd;
|
|
544 |
unsigned short nCount=0;
|
|
545 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
546 |
Value[0] = m_DataBuf[0];
|
|
547 |
return KL_OK;
|
|
548 |
}
|
|
549 |
int KLink::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value)
|
|
550 |
{
|
|
551 |
m_Dst = nDst;
|
|
552 |
m_DataBuf[0] = Value;
|
|
553 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr,1,m_DataBuf);
|
|
554 |
SendPacket(m_Packetbuf, len1);
|
|
555 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
556 |
unsigned char nCmd;
|
|
557 |
unsigned short nCount=0;
|
|
558 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
559 |
return KL_OK;
|
|
560 |
}
|
|
561 |
int KLink::ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
|
562 |
{
|
|
563 |
return KL_OK;
|
|
564 |
}
|
|
565 |
int KLink::WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
|
566 |
{
|
|
567 |
return KL_OK;
|
|
568 |
}
|
|
569 |
int KLink::ReadBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
|
570 |
{
|
|
571 |
return KL_OK;
|
|
572 |
}
|
|
573 |
int KLink::WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
|
574 |
{
|
|
575 |
return KL_OK;
|
|
576 |
}
|
|
577 |
int KLink::GetNextSeq()
|
|
578 |
{
|
|
579 |
m_nSeq++;
|
|
580 |
if (m_nSeq >= 8) m_nSeq = 0;
|
|
581 |
m_Stat1.nSEQ = m_nSeq;
|
|
582 |
return m_nSeq;
|
|
583 |
}
|
|
584 |
|
|
585 |
int KLink::ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values)
|
|
586 |
{
|
|
587 |
m_Dst = nDst;
|
|
588 |
m_resultStr.Empty();
|
|
589 |
UCHAR nExpSeq = GetNextSeq();
|
|
590 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr/2, nByteCount);
|
|
591 |
SendPacket(m_Packetbuf, len1);
|
|
592 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
593 |
int len2 = 0;
|
|
594 |
len2= RecvPacket(m_RecvBuf, numToRead);
|
|
595 |
if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"),len2,numToRead); return KL_ERR; }
|
|
596 |
if (len2 < numToRead)
|
|
597 |
{
|
|
598 |
CString s1;
|
|
599 |
for (int i=0;i<len2;i++)
|
|
600 |
{
|
|
601 |
s1.AppendFormat(_T("%02X "),m_RecvBuf[i]);
|
|
602 |
}
|
|
603 |
s1.Append(_T("\r\n"));
|
|
604 |
m_resultStr.Format(_T("ToRead %d R= %d \r\n"),numToRead,len2);
|
|
605 |
m_resultStr += s1;
|
|
606 |
}
|
|
607 |
unsigned char nCmd ;
|
|
608 |
unsigned short nnCount=0;
|
|
609 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
610 |
if (res == KL_OK)
|
|
611 |
{
|
|
612 |
for (int i = 0; i < nnCount; i++)
|
|
613 |
{ Values[i] = m_DataBuf[i]; }
|
|
614 |
}
|
|
615 |
else
|
|
616 |
{
|
|
617 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
618 |
CString s1;
|
|
619 |
s1= GetErrDescStr(res);
|
|
620 |
m_resultStr += s1;
|
|
621 |
return res;
|
|
622 |
}
|
|
623 |
|
|
624 |
return KL_OK;
|
|
625 |
}
|
|
626 |
int KLink::WriteDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values)
|
|
627 |
{
|
|
628 |
m_Dst = nDst;
|
|
629 |
m_resultStr.Empty();
|
|
630 |
UCHAR nExpSeq = GetNextSeq();
|
|
631 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr/2, nByteCount, Values);
|
|
632 |
SendPacket(m_Packetbuf, len1);
|
|
633 |
nByteCount = 0;
|
|
634 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
635 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
636 |
// if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
637 |
if (len2 <= 0) return KL_ERR;
|
|
638 |
unsigned char nCmd;
|
|
639 |
unsigned short nnCount=0;
|
|
640 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
641 |
return res;
|
|
642 |
}
|
|
643 |
|
|
644 |
int KLink::ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
645 |
{
|
|
646 |
m_Dst = nDst;
|
|
647 |
m_resultStr.Empty();
|
|
648 |
UCHAR nExpSeq = GetNextSeq();
|
|
649 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nWordCount*2);
|
|
650 |
SendPacket(m_Packetbuf, len1);
|
|
651 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount*2;
|
|
652 |
|
|
653 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
654 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
655 |
if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"), len2, numToRead); return KL_ERR; }
|
|
656 |
if (len2 < numToRead)
|
|
657 |
{
|
|
658 |
CString s1;
|
|
659 |
for (int i = 0; i < len2; i++)
|
|
660 |
{
|
|
661 |
s1.AppendFormat(_T("%02X "), m_RecvBuf[i]);
|
|
662 |
}
|
|
663 |
s1.Append(_T("\r\n"));
|
|
664 |
m_resultStr.Format(_T("ToRead %d R= %d \r\n"), numToRead, len2);
|
|
665 |
m_resultStr += s1;
|
|
666 |
}
|
|
667 |
unsigned char nCmd;
|
|
668 |
unsigned short nnCount = 0;
|
|
669 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
670 |
if (res == KL_OK)
|
|
671 |
{
|
|
672 |
for (int i = 0; i < nnCount / 2; i++)
|
|
673 |
{
|
|
674 |
// Values[0] = m_DataBuf[0];
|
|
675 |
Values[i] = m_DataBuf[i * 2] + (m_DataBuf[i * 2 + 1] << 8);
|
|
676 |
}
|
|
677 |
}
|
|
678 |
else
|
|
679 |
{
|
|
680 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
681 |
CString s1;
|
|
682 |
s1 = GetErrDescStr(res);
|
|
683 |
m_resultStr += s1;
|
|
684 |
return res;
|
|
685 |
}
|
|
686 |
return KL_OK;
|
|
687 |
}
|
|
688 |
int KLink::WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
689 |
{
|
|
690 |
m_Dst = nDst;
|
|
691 |
m_resultStr.Empty();
|
|
692 |
UCHAR nExpSeq = GetNextSeq();
|
|
693 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nWordCount, Values);
|
|
694 |
SendPacket(m_Packetbuf, len1);
|
|
695 |
nWordCount = 0;
|
|
696 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
697 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
698 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
699 |
if (len2 == 0) return KL_ERR;
|
|
700 |
unsigned char nCmd;
|
|
701 |
unsigned short nnCount=0;
|
|
702 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
703 |
Values[0] = m_DataBuf[0];
|
|
704 |
return res;
|
|
705 |
}
|
|
706 |
|
|
707 |
int KLink::ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
708 |
{
|
|
709 |
m_Dst = nDst;
|
|
710 |
m_resultStr.Empty();
|
|
711 |
UCHAR nExpSeq = GetNextSeq();
|
|
712 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr, nWordCount);
|
|
713 |
SendPacket(m_Packetbuf, len1);
|
|
714 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
715 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
716 |
if (len2 == 0) return KL_ERR;
|
|
717 |
unsigned char nCmd;
|
|
718 |
unsigned short nnCount=0;
|
|
719 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
720 |
for (int i=0;i<nnCount;i++) Values[i] = m_DataBuf[i];
|
|
721 |
return res;
|
|
722 |
|
|
723 |
return KL_OK;
|
|
724 |
}
|
|
725 |
int KLink::WriteData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
726 |
{
|
|
727 |
m_Dst = nDst;
|
|
728 |
m_resultStr.Empty();
|
|
729 |
UCHAR nExpSeq = GetNextSeq();
|
|
730 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nWordCount, Values);
|
|
731 |
SendPacket(m_Packetbuf, len1);
|
|
732 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
733 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
734 |
if (len2 == 0) return KL_ERR;
|
|
735 |
unsigned char nCmd;
|
|
736 |
unsigned short nnCount=0;
|
|
737 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
738 |
// Values[0] = m_DataBuf[0];
|
|
739 |
return res;
|
|
740 |
|
|
741 |
return KL_OK;
|
|
742 |
}
|
|
743 |
int KLink::GetInfo(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
744 |
{
|
|
745 |
m_Dst = nDst;
|
|
746 |
m_resultStr.Empty();
|
|
747 |
UCHAR nExpSeq = GetNextSeq();
|
|
748 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nWordCount, Values);
|
|
749 |
CString s1;
|
|
750 |
for (int i = 0; i < len1; i++) {
|
|
751 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
752 |
}
|
|
753 |
m_resultStr = s1;
|
|
754 |
SendPacket(m_Packetbuf, len1);
|
|
755 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
756 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
757 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
758 |
if (len2 == 0) return KL_ERR;
|
|
759 |
unsigned char nCmd;
|
|
760 |
unsigned short nnCount=0;
|
|
761 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
762 |
// Values[0] = m_DataBuf[0];
|
|
763 |
return res;
|
|
764 |
|
|
765 |
return KL_OK;
|
|
766 |
}
|
|
767 |
int KLink::GetSN(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
768 |
{
|
|
769 |
m_Dst = nDst;
|
|
770 |
m_resultStr.Empty();
|
|
771 |
UCHAR nExpSeq = GetNextSeq();
|
|
772 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetSN, nType, nWordAddr, nWordCount, Values);
|
|
773 |
CString s1;
|
|
774 |
for (int i = 0; i < len1; i++) {
|
|
775 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
776 |
}
|
|
777 |
m_resultStr = s1;
|
|
778 |
SendPacket(m_Packetbuf, len1);
|
|
779 |
nWordCount = 4;
|
|
780 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
781 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
782 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
783 |
if (len2 == 0) return KL_ERR;
|
|
784 |
unsigned char nCmd;
|
|
785 |
unsigned short nnCount = 0;
|
|
786 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
787 |
// Values[0] = m_DataBuf[0];
|
|
788 |
return res;
|
|
789 |
|
|
790 |
return KL_OK;
|
|
791 |
return 0;
|
|
792 |
}
|
|
793 |
int KLink::GetUID(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
794 |
{
|
|
795 |
m_Dst = nDst;
|
|
796 |
m_resultStr.Empty();
|
|
797 |
UCHAR nExpSeq = GetNextSeq();
|
|
798 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetUid, nType, nWordAddr, nWordCount, Values);
|
|
799 |
CString s1;
|
|
800 |
for (int i = 0; i < len1; i++) {
|
|
801 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
802 |
}
|
|
803 |
m_resultStr = s1;
|
|
804 |
SendPacket(m_Packetbuf, len1);
|
|
805 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
806 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
807 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
808 |
if (len2 == 0) return KL_ERR;
|
|
809 |
unsigned char nCmd;
|
|
810 |
unsigned short nnCount = 0;
|
|
811 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
812 |
// Values[0] = m_DataBuf[0];
|
|
813 |
return res;
|
|
814 |
|
|
815 |
return KL_OK;
|
|
816 |
return 0;
|
|
817 |
}
|
|
818 |
|
|
819 |
int KLink::GetFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
820 |
{
|
|
821 |
m_Dst = nDst;
|
|
822 |
m_resultStr.Empty();
|
|
823 |
UCHAR nExpSeq = GetNextSeq();
|
|
824 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetFactoryData, nType, nWordAddr, nWordCount, Values);
|
|
825 |
CString s1;
|
|
826 |
for (int i = 0; i < len1; i++) {
|
|
827 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
828 |
}
|
|
829 |
m_resultStr = s1;
|
|
830 |
SendPacket(m_Packetbuf, len1);
|
|
831 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
832 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
833 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
834 |
if (len2 == 0) return KL_ERR;
|
|
835 |
unsigned char nCmd;
|
|
836 |
unsigned short nnCount = 0;
|
|
837 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
838 |
// Values[0] = m_DataBuf[0];
|
|
839 |
return res;
|
|
840 |
return KL_OK;
|
|
841 |
}
|
|
842 |
int KLink::WriteFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
843 |
{
|
|
844 |
m_Dst = nDst;
|
|
845 |
m_resultStr.Empty();
|
|
846 |
UCHAR nExpSeq = GetNextSeq();
|
|
847 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteFactoryData, nType, nWordAddr, nWordCount, Values);
|
|
848 |
SendPacket(m_Packetbuf, len1);
|
|
849 |
nWordCount = 0;
|
|
850 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
851 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
852 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
853 |
if (len2 == 0) return KL_ERR;
|
|
854 |
unsigned char nCmd;
|
|
855 |
unsigned short nnCount = 0;
|
|
856 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
857 |
// Values[0] = m_DataBuf[0];
|
|
858 |
return res;
|
|
859 |
|
|
860 |
return KL_OK;
|
|
861 |
}
|
|
862 |
|
|
863 |
int KLink::GetEventLogCount(UCHAR nDst, int * nCount)
|
|
864 |
{
|
|
865 |
m_Dst = nDst;
|
|
866 |
m_resultStr.Empty();
|
|
867 |
UCHAR nExpSeq = GetNextSeq();
|
|
868 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLogCount, 0);
|
|
869 |
SendPacket(m_Packetbuf, len1);
|
|
870 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
871 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
872 |
if (len2 == 0) return KL_ERR;
|
|
873 |
unsigned char nCmd;
|
|
874 |
unsigned short nnCount=0;
|
|
875 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
876 |
nCount[0] = *(int *)m_DataBuf;
|
|
877 |
return res;
|
|
878 |
|
|
879 |
return KL_OK;
|
|
880 |
}
|
|
881 |
|
|
882 |
int KLink::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
|
883 |
{
|
|
884 |
m_Dst = nDst;
|
|
885 |
m_resultStr.Empty();
|
|
886 |
UCHAR nExpSeq = GetNextSeq();
|
|
887 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLog, 0, nStartIndex, nCount, 0);
|
|
888 |
SendPacket(m_Packetbuf, len1);
|
|
889 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
890 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
891 |
if (len2 == 0) return KL_ERR;
|
|
892 |
unsigned char nCmd;
|
|
893 |
unsigned short nnCount=0;
|
|
894 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
895 |
|
|
896 |
memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
|
|
897 |
|
|
898 |
return res;
|
|
899 |
|
|
900 |
return KL_OK;
|
|
901 |
}
|
|
902 |
|
|
903 |
int KLink::GetDateTime32(UCHAR nDst, UINT * pValue)
|
|
904 |
{
|
|
905 |
int res = KL_OK;
|
|
906 |
res = ReadDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)pValue);
|
|
907 |
return res;
|
|
908 |
|
|
909 |
}
|
|
910 |
|
|
911 |
int KLink::SetDateTime32(UCHAR nDst, UINT Value)
|
|
912 |
{
|
|
913 |
int res = KL_OK;
|
|
914 |
res = WriteDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)&Value);
|
|
915 |
return res;
|
|
916 |
}
|
|
917 |
|
|
918 |
int KLink::GetMode(UCHAR nDst, UCHAR nType)
|
|
919 |
{
|
|
920 |
int res = KL_OK;
|
|
921 |
// res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
|
|
922 |
m_Dst = nDst;
|
|
923 |
UCHAR nExpSeq = GetNextSeq();
|
|
924 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetMode, nType);
|
|
925 |
SendPacket(m_Packetbuf, len1);
|
|
926 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
927 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
928 |
if (len2 == 0) return KL_ERR;
|
|
929 |
unsigned char nCmd;
|
|
930 |
unsigned short nnCount=0;
|
|
931 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
932 |
return res;
|
|
933 |
|
|
934 |
}
|
|
935 |
|
|
936 |
|
|
937 |
int KLink::ChangeMode(UCHAR nDst, UCHAR nType)
|
|
938 |
{
|
|
939 |
int res = KL_OK;
|
|
940 |
// res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
|
941 |
m_Dst = nDst;
|
|
942 |
UCHAR nExpSeq = GetNextSeq();
|
|
943 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdChgMode, nType);
|
|
944 |
SendPacket(m_Packetbuf, len1);
|
|
945 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
946 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
947 |
if (len2 == 0) return KL_ERR;
|
|
948 |
unsigned char nCmd;
|
|
949 |
unsigned short nnCount=0;
|
|
950 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
951 |
return res;
|
|
952 |
|
|
953 |
}
|
|
954 |
|
|
955 |
|
|
956 |
|
|
957 |
int KLink::ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
958 |
{
|
|
959 |
int res = KL_OK;
|
|
960 |
m_Dst = nDst;
|
|
961 |
UCHAR nExpSeq = GetNextSeq();
|
|
962 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadProgram, nType, nWordAddr, nWordCount);
|
|
963 |
SendPacket(m_Packetbuf, len1);
|
|
964 |
int nByteCount = nWordCount;
|
|
965 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
966 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
967 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
968 |
if (len2 <= 0) return KL_ERR;
|
|
969 |
unsigned char nCmd;
|
|
970 |
unsigned short nnCount=0;
|
|
971 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
972 |
unsigned char * values2 = (unsigned char *)Values;
|
|
973 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
974 |
return res;
|
|
975 |
|
|
976 |
}
|
|
977 |
|
|
978 |
int KLink::StartProgram(UCHAR nDst, UCHAR nType)
|
|
979 |
{
|
|
980 |
int res = KL_OK;
|
|
981 |
m_Dst = nDst;
|
|
982 |
UCHAR nExpSeq = GetNextSeq();
|
|
983 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType);
|
|
984 |
SendPacket(m_Packetbuf, len1);
|
|
985 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
986 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); }
|
|
987 |
if (len2 <= 0) return KL_ERR;
|
|
988 |
unsigned char nCmd;
|
|
989 |
unsigned short nnCount=0;
|
|
990 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
991 |
return res;
|
|
992 |
|
|
993 |
}
|
|
994 |
int KLink::WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
995 |
{
|
|
996 |
int res = KL_OK;
|
|
997 |
m_Dst = nDst;
|
|
998 |
UCHAR nExpSeq = GetNextSeq();
|
|
999 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteProgram, nType, nWordAddr, nWordCount, Values);
|
|
1000 |
|
|
1001 |
SendPacket(m_Packetbuf, len1);
|
|
1002 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
1003 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
1004 |
if (len2 <= 0) return KL_ERR;
|
|
1005 |
unsigned char nCmd;
|
|
1006 |
unsigned short nnCount=0;
|
|
1007 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1008 |
// Values[0] = m_DataBuf[0];
|
|
1009 |
return res;
|
|
1010 |
|
|
1011 |
}
|
|
1012 |
int KLink::FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize)
|
|
1013 |
{
|
|
1014 |
int res = KL_OK;
|
|
1015 |
|
|
1016 |
m_Dst = nDst;
|
|
1017 |
UCHAR nExpSeq = GetNextSeq();
|
|
1018 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nStepSize);
|
|
1019 |
SendPacket(m_Packetbuf, len1);
|
|
1020 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
1021 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
1022 |
if (len2 <= 0) return KL_ERR;
|
|
1023 |
unsigned char nCmd;
|
|
1024 |
unsigned short nnCount=0;
|
|
1025 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1026 |
return res;
|
|
1027 |
}
|
|
1028 |
|
|
1029 |
int KLink::ReadRunStat(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
1030 |
{
|
|
1031 |
int res = KL_OK;
|
|
1032 |
m_Dst = nDst;
|
|
1033 |
UCHAR nExpSeq = GetNextSeq();
|
|
1034 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadRunStat, nType, nWordAddr, nWordCount);
|
|
1035 |
SendPacket(m_Packetbuf, len1);
|
|
1036 |
int nByteCount = nWordCount;
|
|
1037 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1038 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1039 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1040 |
if (len2 <= 0) return KL_ERR;
|
|
1041 |
unsigned char nCmd;
|
|
1042 |
unsigned short nnCount=0;
|
|
1043 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1044 |
unsigned char * values2 = (unsigned char *)Values;
|
|
1045 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
1046 |
return res;
|
|
1047 |
|
|
1048 |
}
|