提交 | 用户 | age
|
0ed438
|
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::SetSendCallBackFunc(std::function<int(void *, int)> pSendFunc)
|
|
107 |
{
|
|
108 |
SendPkgFunc = pSendFunc;
|
|
109 |
m_bSendCallBackSet = 1;
|
|
110 |
return KL_OK;
|
|
111 |
}
|
|
112 |
|
|
113 |
int KLink::SetRecvCallBackFunc(std::function<int(void *, int)> pRecvFunc)
|
|
114 |
{
|
|
115 |
RecvPkgFunc = pRecvFunc;
|
|
116 |
m_bRecvCallBackSet = 1;
|
|
117 |
return KL_OK;
|
|
118 |
}
|
|
119 |
|
|
120 |
int KLink::SetOpenCallBackFunc(std::function<int(int)> pOpenFunc)
|
|
121 |
{
|
|
122 |
OpenFunc = pOpenFunc;
|
|
123 |
m_bOpenCallBackSet = 1;
|
|
124 |
return KL_OK;
|
|
125 |
}
|
|
126 |
int KLink::SetCloseCallBackFunc(std::function<int(int)> pCloseFunc)
|
|
127 |
{
|
|
128 |
CloseFunc = pCloseFunc;
|
|
129 |
m_bCloseCallBackSet = 1;
|
|
130 |
return KL_OK;
|
|
131 |
}
|
|
132 |
int KLink::MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData)
|
|
133 |
{
|
|
134 |
int framelen1 = 0;
|
|
135 |
pKLExtDataFrame pF1 = (pKLExtDataFrame)pBuf;
|
|
136 |
pF1->Dst = nDst;
|
|
137 |
pF1->nType = nType;
|
|
138 |
pF1->Len2 = len2;
|
|
139 |
memcpy(pF1->Data, pData, len2);
|
|
140 |
UCHAR bcc = KLBCC(pBuf, sizeof(tagKLExtDataFrame) + len2 - 1);
|
|
141 |
pF1->Data[len2] = bcc;
|
|
142 |
|
|
143 |
framelen1 = sizeof(tagKLExtDataFrame) + len2;
|
|
144 |
return framelen1;
|
|
145 |
}
|
|
146 |
/*
|
|
147 |
int KLink::MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData)
|
|
148 |
{
|
|
149 |
int PkgLen1 = 0;
|
|
150 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
151 |
pPacket->StartSign = KLSignStart;
|
|
152 |
pPacket->DstAddr = nDst;
|
|
153 |
pPacket->Stat = Stat;
|
|
154 |
pPacket->Cmd = nCMD;
|
|
155 |
pPacket->Type1 = Type;
|
|
156 |
int Datalen = 0;
|
|
157 |
int nWordAddr;
|
|
158 |
int nWordCount;
|
|
159 |
|
|
160 |
switch (nCMD)
|
|
161 |
{
|
|
162 |
case KLCmdNone:
|
|
163 |
break;
|
|
164 |
case KLCmdPing:
|
|
165 |
Datalen = Type;
|
|
166 |
memcpy(pPacket->Params, pData, Datalen);
|
|
167 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
168 |
break;
|
|
169 |
case KLCmdGetInfo:
|
|
170 |
break;
|
|
171 |
case KLCmdRead:
|
|
172 |
Datalen = 0;
|
|
173 |
pPacket->Params[0] = nAddr % 256;
|
|
174 |
pPacket->Params[1] = nAddr / 256;
|
|
175 |
pPacket->Params[2] = nCount % 256;
|
|
176 |
pPacket->Params[3] = nCount / 256;
|
|
177 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
178 |
break;
|
|
179 |
case KLCmdWrite:
|
|
180 |
pPacket->Params[0] = nAddr % 256;
|
|
181 |
pPacket->Params[1] = nAddr / 256;
|
|
182 |
pPacket->Params[2] = nCount % 256;
|
|
183 |
pPacket->Params[3] = nCount / 256;
|
|
184 |
Datalen = nCount;
|
|
185 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
186 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
187 |
|
|
188 |
break;
|
|
189 |
case KLCmdSaveToFlash:
|
|
190 |
break;
|
|
191 |
case KLCmdExChgData:
|
|
192 |
break;
|
|
193 |
case KLCmdGetEventLogCount:
|
|
194 |
pPacket->Params[0] = 0;
|
|
195 |
pPacket->Params[1] = 0;
|
|
196 |
pPacket->Params[2] = 0;
|
|
197 |
pPacket->Params[3] = 0;
|
|
198 |
Datalen = 0;
|
|
199 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
200 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
201 |
default:
|
|
202 |
break;
|
|
203 |
}
|
|
204 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
205 |
{
|
|
206 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
207 |
pPacket->Params[PkgLen1-sizeof(stKLReqPacket)] = BCC;
|
|
208 |
}
|
|
209 |
return PkgLen1;
|
|
210 |
}
|
|
211 |
*/
|
|
212 |
int KLink::MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData, int ExtFrameLen, void * pExtDataFrame)
|
|
213 |
{
|
|
214 |
int PkgLen1 = 0;
|
|
215 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
216 |
pPacket->StartSign = KLSignStart;
|
|
217 |
pPacket->DstAddr = nDst;
|
|
218 |
pPacket->Stat = Stat;
|
|
219 |
pPacket->Cmd = nCMD;
|
|
220 |
pPacket->Type1 = Type;
|
|
221 |
|
|
222 |
pKLStat pStat = (pKLStat)&(pPacket->Stat);
|
|
223 |
int Datalen = 0;
|
|
224 |
int nWordAddr=nAddr;
|
|
225 |
int nWordCount=nCount;
|
|
226 |
pPacket->Params[0] = LOBYTE(nAddr);
|
|
227 |
pPacket->Params[1] = HIBYTE(nAddr);
|
|
228 |
pPacket->Params[2] = nCount % 256;
|
|
229 |
pPacket->Params[3] = nCount / 256;
|
|
230 |
|
|
231 |
switch (nCMD)
|
|
232 |
{
|
|
233 |
case KLCmdNone:
|
|
234 |
break;
|
|
235 |
case KLCmdPing:
|
|
236 |
Datalen = Type;
|
|
237 |
memcpy(pPacket->Params, pData, Datalen);
|
|
238 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
239 |
break;
|
|
240 |
case KLCmdGetInfo:
|
|
241 |
break;
|
|
242 |
case KLCmdRead:
|
|
243 |
Datalen = 0;
|
|
244 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
245 |
break;
|
|
246 |
case KLCmdWrite:
|
|
247 |
Datalen = nCount;
|
|
248 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
249 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
250 |
break;
|
|
251 |
case KLCmdRead1Bit:
|
|
252 |
Datalen = 0;
|
|
253 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
254 |
break;
|
|
255 |
case KLCmdWrite1Bit:
|
|
256 |
Datalen = 1;
|
|
257 |
memcpy(pPacket->Params + 2, pData, Datalen);
|
|
258 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
259 |
break;
|
|
260 |
|
|
261 |
case KLCmdReadBits:
|
|
262 |
Datalen = 0;
|
|
263 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
264 |
break;
|
|
265 |
case KLCmdWriteBits:
|
|
266 |
Datalen = 0;
|
|
267 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
268 |
break;
|
|
269 |
case KLCmdReadDataByte:
|
|
270 |
case KLCmdReadDataWord:
|
|
271 |
case KLCmdReadData:
|
|
272 |
Datalen = 0;
|
|
273 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
274 |
break;
|
|
275 |
case KLCmdWriteDataByte:
|
|
276 |
case KLCmdWriteDataWord:
|
|
277 |
case KLCmdWriteData:
|
|
278 |
Datalen = nCount;
|
|
279 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
280 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
281 |
break;
|
|
282 |
case KLCmdSaveToFlash:
|
|
283 |
break;
|
|
284 |
case KLCmdExChgData:
|
|
285 |
break;
|
|
286 |
case KLCmdReadProgram:
|
|
287 |
Datalen = 0;
|
|
288 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
289 |
break;
|
|
290 |
case KLCmdStartProgram:
|
|
291 |
Datalen = 0;
|
|
292 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
293 |
break;
|
|
294 |
case KLCmdWriteProgram:
|
|
295 |
Datalen = nCount;
|
|
296 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
297 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
298 |
break;
|
|
299 |
case KLCmdFinishProgram:
|
|
300 |
Datalen = 0;
|
|
301 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
302 |
break;
|
|
303 |
|
|
304 |
case KLCmdReadRunStat:
|
|
305 |
Datalen = 0;
|
|
306 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
307 |
break;
|
|
308 |
|
|
309 |
case KLCmdSaveSysCfg:
|
|
310 |
pPacket->Params[0] = 0;
|
|
311 |
pPacket->Params[1] = 0;
|
|
312 |
pPacket->Params[2] = 0;
|
|
313 |
pPacket->Params[3] = 0;
|
|
314 |
Datalen = 0;
|
|
315 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
316 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
317 |
break;
|
|
318 |
case KLCmdSaveRunStat:
|
|
319 |
pPacket->Params[0] = 0;
|
|
320 |
pPacket->Params[1] = 0;
|
|
321 |
pPacket->Params[2] = 0;
|
|
322 |
pPacket->Params[3] = 0;
|
|
323 |
Datalen = 0;
|
|
324 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
325 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
326 |
break;
|
|
327 |
case KLCmdClearEventLog:
|
|
328 |
pPacket->Params[0] = 0;
|
|
329 |
pPacket->Params[1] = 0;
|
|
330 |
pPacket->Params[2] = 0;
|
|
331 |
pPacket->Params[3] = 0;
|
|
332 |
Datalen = 0;
|
|
333 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
334 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
335 |
|
|
336 |
case KLCmdGetEventLogCount:
|
|
337 |
pPacket->Params[0] = 0;
|
|
338 |
pPacket->Params[1] = 0;
|
|
339 |
pPacket->Params[2] = 0;
|
|
340 |
pPacket->Params[3] = 0;
|
|
341 |
Datalen = 0;
|
|
342 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
343 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
344 |
break;
|
|
345 |
case KLCmdGetEventLog:
|
|
346 |
pPacket->Params[0] = nAddr % 256;
|
|
347 |
pPacket->Params[1] = nAddr / 256;
|
|
348 |
pPacket->Params[2] = nCount % 256;
|
|
349 |
pPacket->Params[3] = nCount / 256;
|
|
350 |
Datalen = 0;
|
|
351 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
352 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
353 |
break;
|
|
354 |
|
|
355 |
default:
|
|
356 |
pPacket->Params[0] = nAddr % 256;
|
|
357 |
pPacket->Params[1] = nAddr / 256;
|
|
358 |
pPacket->Params[2] = nCount % 256;
|
|
359 |
pPacket->Params[3] = nCount / 256;
|
|
360 |
Datalen = 0;
|
|
361 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
362 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
363 |
break;
|
|
364 |
}
|
|
365 |
|
|
366 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
367 |
{
|
|
368 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
369 |
pPacket->Params[PkgLen1 - sizeof(stKLReqPacket)] = BCC;
|
|
370 |
}
|
|
371 |
|
|
372 |
if (pStat->HasExt && ExtFrameLen > 0 && pExtDataFrame != NULL)
|
|
373 |
{
|
|
374 |
|
|
375 |
memcpy(pPacket->Params + PkgLen1 - sizeof(stKLReqPacket) + 1, pExtDataFrame, ExtFrameLen);
|
|
376 |
PkgLen1 += ExtFrameLen;
|
|
377 |
};
|
|
378 |
return PkgLen1;
|
|
379 |
}
|
|
380 |
|
|
381 |
|
|
382 |
int KLink::CheckPackage(void * pBuf, int nSize)
|
|
383 |
{
|
|
384 |
pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
|
|
385 |
if (p1->RplyStSgn != KLSignReply)
|
|
386 |
{
|
|
387 |
return KL_UNKNOWN;
|
|
388 |
}
|
|
389 |
|
|
390 |
int DataLen = 0; //p1->LoadLen;
|
|
391 |
if (DataLen > KLMaxPacketLength)
|
|
392 |
{
|
|
393 |
return KL_PKG_ERR;
|
|
394 |
}
|
|
395 |
if (nSize < DataLen + sizeof(stKLRplyPktHdr))
|
|
396 |
{
|
|
397 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
398 |
//PutStr(str3,len4);
|
|
399 |
return KL_PKG_ERR; //not long enough
|
|
400 |
}
|
|
401 |
// if (p1->data[DataLen+1] != EndSign)
|
|
402 |
// {
|
|
403 |
// ClientStat[nCurPollId].NoEndErr++;
|
|
404 |
// Uart2Stat.LengthErr++;
|
|
405 |
// return -2;
|
|
406 |
// }
|
|
407 |
unsigned char thisBCC = KLBCC(p1, nSize - 1);
|
|
408 |
if (thisBCC != ((UCHAR *)pBuf)[nSize - 1])
|
|
409 |
{//BCC Error;
|
|
410 |
return KL_BCC_ERR;
|
|
411 |
}
|
|
412 |
return KL_OK;
|
|
413 |
|
|
414 |
return true;
|
|
415 |
return false;
|
|
416 |
}
|
|
417 |
|
|
418 |
int KLink::ProcessPacket(void *pBuf, int nLen)
|
|
419 |
{
|
|
420 |
return false;
|
|
421 |
}
|
|
422 |
|
|
423 |
int KLink::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData)
|
|
424 |
{
|
|
425 |
if (nPkgLen < 0) { return KL_ERR; }
|
|
426 |
int res = CheckPackage(pBuf, nPkgLen);
|
|
427 |
if (res != KL_OK) return res;
|
|
428 |
int len1 = 0;
|
|
429 |
pKLRplyPktHdr pPacket = (pKLRplyPktHdr)pBuf;
|
|
430 |
UCHAR Sign = pPacket->RplyStSgn;
|
|
431 |
UCHAR nDst= pPacket->DstAddr;
|
|
432 |
*nCmd = pPacket->nRplyCMD;
|
|
433 |
UCHAR nStatus = pPacket->nStatus;
|
|
434 |
*Status = nStatus;
|
|
435 |
m_nRSeq = ((pKLStat)&nStatus)->nSEQ;
|
|
436 |
int Datalen = pPacket->nSize1;
|
|
437 |
*nCount = Datalen;
|
|
438 |
switch (*nCmd)
|
|
439 |
{
|
|
440 |
case KLCmdNone:
|
|
441 |
break;
|
|
442 |
case KLCmdPing:
|
|
443 |
case KLCmdPingReply:
|
|
444 |
*nCount = Datalen;
|
|
445 |
memcpy(pPacket->Datas, pData, Datalen);
|
|
446 |
break;
|
|
447 |
case KLCmdGetInfo:
|
|
448 |
case KLCmdVerInfo:
|
|
449 |
*nCount = Datalen;
|
|
450 |
memcpy(pPacket->Datas, pData, Datalen);
|
|
451 |
break;
|
|
452 |
case KLCmdRead:
|
|
453 |
case KLCmdReadReply:
|
|
454 |
*nCount = Datalen;
|
|
455 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
456 |
break;
|
|
457 |
case KLCmdWrite:
|
|
458 |
case KLCmdWriteReply:
|
|
459 |
*nCount = Datalen;
|
|
460 |
break;
|
|
461 |
case KLCmdRead1Bit:
|
|
462 |
case KLCmdReadBits:
|
|
463 |
*nCount = Datalen;
|
|
464 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
465 |
break;
|
|
466 |
case KLCmdReadDataByte:
|
|
467 |
*nCount = Datalen;
|
|
468 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
469 |
break;
|
|
470 |
case KLCmdReadDataWord:
|
|
471 |
case KLCmdReadData:
|
|
472 |
*nCount = Datalen;
|
|
473 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
474 |
break;
|
|
475 |
case KLCmdWriteDataByte:
|
|
476 |
case KLCmdWriteDataWord:
|
|
477 |
case KLCmdWriteData:
|
|
478 |
*nCount = Datalen;
|
|
479 |
break;
|
|
480 |
|
|
481 |
case KLCmdSaveToFlash:
|
|
482 |
break;
|
|
483 |
|
|
484 |
case KLCmdReadProgram:
|
|
485 |
*nCount = Datalen;
|
|
486 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
487 |
break;
|
|
488 |
case KLCmdStartProgram:
|
|
489 |
*nCount =Datalen;
|
|
490 |
break;
|
|
491 |
case KLCmdWriteProgram:
|
|
492 |
*nCount = Datalen;
|
|
493 |
break;
|
|
494 |
case KLCmdFinishProgram:
|
|
495 |
*nCount = Datalen;
|
|
496 |
break;
|
|
497 |
|
|
498 |
case KLCmdReadRunStat:
|
|
499 |
*nCount = Datalen;
|
|
500 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
501 |
break;
|
|
502 |
case KLCmdExChgData:
|
|
503 |
case KLCmdExChgDataReply:
|
|
504 |
break;
|
|
505 |
case KLCmdGetEventLogCount:
|
|
506 |
*nCount = Datalen;
|
|
507 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
508 |
break;
|
|
509 |
case KLCmdGetEventLog:
|
|
510 |
*nCount = Datalen;
|
|
511 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
512 |
break;
|
|
513 |
|
|
514 |
case KLCmdErrRply:
|
|
515 |
*nCount = Datalen;
|
|
516 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
517 |
break;
|
|
518 |
default:
|
|
519 |
*nCount = Datalen;
|
|
520 |
//memcpy(pData, pPacket->Datas, Datalen);
|
|
521 |
break;
|
|
522 |
}
|
|
523 |
return KL_OK;
|
|
524 |
|
|
525 |
return false;
|
|
526 |
}
|
|
527 |
|
|
528 |
int KLink::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value)
|
|
529 |
{
|
|
530 |
m_Dst = nDst;
|
|
531 |
int len1=MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr);
|
|
532 |
SendPacket(m_Packetbuf, len1);
|
|
533 |
int numToRead = sizeof(stKLRplyPktHdr) + 1;
|
|
534 |
|
|
535 |
int len2=RecvPacket(m_RecvBuf, numToRead);
|
|
536 |
unsigned char nCmd;
|
|
537 |
unsigned short nCount;
|
|
538 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
539 |
Value[0] = m_DataBuf[0];
|
|
540 |
return KL_OK;
|
|
541 |
}
|
|
542 |
int KLink::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value)
|
|
543 |
{
|
|
544 |
m_Dst = nDst;
|
|
545 |
m_DataBuf[0] = Value;
|
|
546 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr,1,m_DataBuf);
|
|
547 |
SendPacket(m_Packetbuf, len1);
|
|
548 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
549 |
unsigned char nCmd;
|
|
550 |
unsigned short nCount;
|
|
551 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
552 |
return KL_OK;
|
|
553 |
}
|
|
554 |
int KLink::ReadBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
|
555 |
{
|
|
556 |
return KL_OK;
|
|
557 |
}
|
|
558 |
int KLink::WriteBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
|
559 |
{
|
|
560 |
return KL_OK;
|
|
561 |
}
|
|
562 |
int KLink::ReadBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
|
563 |
{
|
|
564 |
return KL_OK;
|
|
565 |
}
|
|
566 |
int KLink::WriteBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
|
567 |
{
|
|
568 |
return KL_OK;
|
|
569 |
}
|
|
570 |
int KLink::GetNextSeq()
|
|
571 |
{
|
|
572 |
m_nSeq++;
|
|
573 |
if (m_nSeq >= 8) m_nSeq = 0;
|
|
574 |
m_Stat1.nSEQ = m_nSeq;
|
|
575 |
return m_nSeq;
|
|
576 |
}
|
|
577 |
|
|
578 |
int KLink::ReadDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
|
579 |
{
|
|
580 |
m_Dst = nDst;
|
|
581 |
m_resultStr.Empty();
|
|
582 |
UCHAR nExpSeq = GetNextSeq();
|
|
583 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr/2, nByteCount);
|
|
584 |
SendPacket(m_Packetbuf, len1);
|
|
585 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
586 |
int len2 = 0;
|
|
587 |
len2= RecvPacket(m_RecvBuf, numToRead);
|
|
588 |
if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"),len2,numToRead); return KL_ERR; }
|
|
589 |
if (len2 < numToRead)
|
|
590 |
{
|
|
591 |
CString s1;
|
|
592 |
for (int i=0;i<len2;i++)
|
|
593 |
{
|
|
594 |
s1.AppendFormat(_T("%02X "),m_RecvBuf[i]);
|
|
595 |
}
|
|
596 |
s1.Append(_T("\r\n"));
|
|
597 |
m_resultStr.Format(_T("ToRead %d R= %d \r\n"),numToRead,len2);
|
|
598 |
m_resultStr += s1;
|
|
599 |
}
|
|
600 |
unsigned char nCmd ;
|
|
601 |
unsigned short nnCount;
|
|
602 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
603 |
if (res == KL_OK)
|
|
604 |
{
|
|
605 |
for (int i = 0; i < nnCount; i++)
|
|
606 |
{ Values[i] = m_DataBuf[i]; }
|
|
607 |
}
|
|
608 |
else
|
|
609 |
{
|
|
610 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
611 |
return res;
|
|
612 |
}
|
|
613 |
|
|
614 |
return KL_OK;
|
|
615 |
}
|
|
616 |
int KLink::WriteDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
|
617 |
{
|
|
618 |
m_Dst = nDst;
|
|
619 |
UCHAR nExpSeq = GetNextSeq();
|
|
620 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr/2, nByteCount, Values);
|
|
621 |
SendPacket(m_Packetbuf, len1);
|
|
622 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
623 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
624 |
if (len2 == 0) return KL_ERR;
|
|
625 |
unsigned char nCmd;
|
|
626 |
unsigned short nnCount;
|
|
627 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
628 |
return res;
|
|
629 |
}
|
|
630 |
|
|
631 |
int KLink::ReadDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
632 |
{
|
|
633 |
m_Dst = nDst;
|
|
634 |
UCHAR nExpSeq = GetNextSeq();
|
|
635 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nWordCount);
|
|
636 |
SendPacket(m_Packetbuf, len1);
|
|
637 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
638 |
|
|
639 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
640 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
641 |
if (len2 == 0) return KL_ERR;
|
|
642 |
unsigned char nCmd;
|
|
643 |
unsigned short nnCount;
|
|
644 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
645 |
for (int i = 0; i < nnCount/2; i++)
|
|
646 |
{
|
|
647 |
// Values[0] = m_DataBuf[0];
|
|
648 |
Values[i] = m_DataBuf[i*2] + (m_DataBuf[i * 2 +1]<<8);
|
|
649 |
}
|
|
650 |
return res;
|
|
651 |
}
|
|
652 |
int KLink::WriteDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
653 |
{
|
|
654 |
m_Dst = nDst;
|
|
655 |
UCHAR nExpSeq = GetNextSeq();
|
|
656 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nWordCount, Values);
|
|
657 |
SendPacket(m_Packetbuf, len1);
|
|
658 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
659 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
660 |
if (len2 == 0) return KL_ERR;
|
|
661 |
unsigned char nCmd;
|
|
662 |
unsigned short nnCount;
|
|
663 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
664 |
Values[0] = m_DataBuf[0];
|
|
665 |
return res;
|
|
666 |
}
|
|
667 |
|
|
668 |
int KLink::ReadData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
669 |
{
|
|
670 |
m_Dst = nDst;
|
|
671 |
UCHAR nExpSeq = GetNextSeq();
|
|
672 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr, nWordCount);
|
|
673 |
SendPacket(m_Packetbuf, len1);
|
|
674 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
675 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
676 |
if (len2 == 0) return KL_ERR;
|
|
677 |
unsigned char nCmd;
|
|
678 |
unsigned short nnCount;
|
|
679 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
680 |
for (int i=0;i<nnCount;i++) Values[i] = m_DataBuf[i];
|
|
681 |
return res;
|
|
682 |
|
|
683 |
return KL_OK;
|
|
684 |
}
|
|
685 |
int KLink::WriteData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
686 |
{
|
|
687 |
m_Dst = nDst;
|
|
688 |
UCHAR nExpSeq = GetNextSeq();
|
|
689 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nWordCount, Values);
|
|
690 |
SendPacket(m_Packetbuf, len1);
|
|
691 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
692 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
693 |
if (len2 == 0) return KL_ERR;
|
|
694 |
unsigned char nCmd;
|
|
695 |
unsigned short nnCount;
|
|
696 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
697 |
// Values[0] = m_DataBuf[0];
|
|
698 |
return res;
|
|
699 |
|
|
700 |
return KL_OK;
|
|
701 |
}
|
|
702 |
int KLink::GetInfo(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
|
703 |
{
|
|
704 |
m_Dst = nDst;
|
|
705 |
UCHAR nExpSeq = GetNextSeq();
|
|
706 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nWordCount, Values);
|
|
707 |
SendPacket(m_Packetbuf, len1);
|
|
708 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
709 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
710 |
if (len2 == 0) return KL_ERR;
|
|
711 |
unsigned char nCmd;
|
|
712 |
unsigned short nnCount;
|
|
713 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
714 |
// Values[0] = m_DataBuf[0];
|
|
715 |
return res;
|
|
716 |
|
|
717 |
return KL_OK;
|
|
718 |
}
|
|
719 |
int KLink::GetEventLogCount(UCHAR nDst, int * nCount)
|
|
720 |
{
|
|
721 |
m_Dst = nDst;
|
|
722 |
UCHAR nExpSeq = GetNextSeq();
|
|
723 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLogCount, 0);
|
|
724 |
SendPacket(m_Packetbuf, len1);
|
|
725 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
726 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
727 |
if (len2 == 0) return KL_ERR;
|
|
728 |
unsigned char nCmd;
|
|
729 |
unsigned short nnCount;
|
|
730 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
731 |
nCount[0] = *(int *)m_DataBuf;
|
|
732 |
return res;
|
|
733 |
|
|
734 |
return KL_OK;
|
|
735 |
}
|
|
736 |
|
|
737 |
int KLink::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
|
738 |
{
|
|
739 |
m_Dst = nDst;
|
|
740 |
UCHAR nExpSeq = GetNextSeq();
|
|
741 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLog, 0, nStartIndex, nCount, 0);
|
|
742 |
SendPacket(m_Packetbuf, len1);
|
|
743 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
744 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
745 |
if (len2 == 0) return KL_ERR;
|
|
746 |
unsigned char nCmd;
|
|
747 |
unsigned short nnCount;
|
|
748 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
749 |
|
|
750 |
memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
|
|
751 |
|
|
752 |
return res;
|
|
753 |
|
|
754 |
return KL_OK;
|
|
755 |
}
|
|
756 |
|
|
757 |
int KLink::GetDateTime32(UCHAR nDst, UINT * pValue)
|
|
758 |
{
|
|
759 |
int res = KL_OK;
|
|
760 |
res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
|
|
761 |
return res;
|
|
762 |
|
|
763 |
}
|
|
764 |
|
|
765 |
int KLink::SetDateTime32(UCHAR nDst, UINT Value)
|
|
766 |
{
|
|
767 |
int res = KL_OK;
|
|
768 |
res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
|
769 |
return res;
|
|
770 |
}
|
|
771 |
|
|
772 |
int KLink::GetMode(UCHAR nDst, UCHAR nType)
|
|
773 |
{
|
|
774 |
int res = KL_OK;
|
|
775 |
// res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
|
|
776 |
m_Dst = nDst;
|
|
777 |
UCHAR nExpSeq = GetNextSeq();
|
|
778 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetMode, nType);
|
|
779 |
SendPacket(m_Packetbuf, len1);
|
|
780 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
781 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
782 |
if (len2 == 0) return KL_ERR;
|
|
783 |
unsigned char nCmd;
|
|
784 |
unsigned short nnCount;
|
|
785 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
786 |
return res;
|
|
787 |
|
|
788 |
}
|
|
789 |
|
|
790 |
|
|
791 |
int KLink::ChangeMode(UCHAR nDst, UCHAR nType)
|
|
792 |
{
|
|
793 |
int res = KL_OK;
|
|
794 |
// res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
|
795 |
m_Dst = nDst;
|
|
796 |
UCHAR nExpSeq = GetNextSeq();
|
|
797 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdChgMode, nType);
|
|
798 |
SendPacket(m_Packetbuf, len1);
|
|
799 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
800 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
801 |
if (len2 == 0) return KL_ERR;
|
|
802 |
unsigned char nCmd;
|
|
803 |
unsigned short nnCount;
|
|
804 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
805 |
return res;
|
|
806 |
|
|
807 |
}
|
|
808 |
|
|
809 |
|
|
810 |
|
|
811 |
int KLink::ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
812 |
{
|
|
813 |
int res = KL_OK;
|
|
814 |
m_Dst = nDst;
|
|
815 |
UCHAR nExpSeq = GetNextSeq();
|
|
816 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadProgram, nType, nWordAddr, nWordCount);
|
|
817 |
SendPacket(m_Packetbuf, len1);
|
|
818 |
int nByteCount = nWordCount;
|
|
819 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
820 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
821 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
822 |
if (len2 <= 0) return KL_ERR;
|
|
823 |
unsigned char nCmd;
|
|
824 |
unsigned short nnCount;
|
|
825 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
826 |
unsigned char * values2 = (unsigned char *)Values;
|
|
827 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
828 |
return res;
|
|
829 |
|
|
830 |
}
|
|
831 |
|
|
832 |
int KLink::StartProgram(UCHAR nDst, UCHAR nType)
|
|
833 |
{
|
|
834 |
int res = KL_OK;
|
|
835 |
m_Dst = nDst;
|
|
836 |
UCHAR nExpSeq = GetNextSeq();
|
|
837 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType);
|
|
838 |
SendPacket(m_Packetbuf, len1);
|
|
839 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
840 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); }
|
|
841 |
if (len2 <= 0) return KL_ERR;
|
|
842 |
unsigned char nCmd;
|
|
843 |
unsigned short nnCount;
|
|
844 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
845 |
return res;
|
|
846 |
|
|
847 |
}
|
|
848 |
int KLink::WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
849 |
{
|
|
850 |
int res = KL_OK;
|
|
851 |
m_Dst = nDst;
|
|
852 |
UCHAR nExpSeq = GetNextSeq();
|
|
853 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteProgram, nType, nWordAddr, nWordCount, Values);
|
|
854 |
|
|
855 |
SendPacket(m_Packetbuf, len1);
|
|
856 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
857 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
858 |
if (len2 <= 0) return KL_ERR;
|
|
859 |
unsigned char nCmd;
|
|
860 |
unsigned short nnCount;
|
|
861 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
862 |
// Values[0] = m_DataBuf[0];
|
|
863 |
return res;
|
|
864 |
|
|
865 |
}
|
|
866 |
int KLink::FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize)
|
|
867 |
{
|
|
868 |
int res = KL_OK;
|
|
869 |
|
|
870 |
m_Dst = nDst;
|
|
871 |
UCHAR nExpSeq = GetNextSeq();
|
|
872 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nStepSize);
|
|
873 |
SendPacket(m_Packetbuf, len1);
|
|
874 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
875 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
876 |
if (len2 <= 0) return KL_ERR;
|
|
877 |
unsigned char nCmd;
|
|
878 |
unsigned short nnCount;
|
|
879 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
880 |
return res;
|
|
881 |
}
|
|
882 |
|
|
883 |
int KLink::ReadRunStat(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
884 |
{
|
|
885 |
int res = KL_OK;
|
|
886 |
m_Dst = nDst;
|
|
887 |
UCHAR nExpSeq = GetNextSeq();
|
|
888 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadRunStat, nType, nWordAddr, nWordCount);
|
|
889 |
SendPacket(m_Packetbuf, len1);
|
|
890 |
int nByteCount = nWordCount;
|
|
891 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
892 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
893 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
894 |
if (len2 <= 0) return KL_ERR;
|
|
895 |
unsigned char nCmd;
|
|
896 |
unsigned short nnCount;
|
|
897 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
898 |
unsigned char * values2 = (unsigned char *)Values;
|
|
899 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
900 |
return res;
|
|
901 |
|
|
902 |
}
|