提交 | 用户 | age
|
418cb3
|
1 |
#include "pch.h"
|
Q |
2 |
#include "KLink1.h"
|
|
3 |
#include "KLink.h"
|
|
4 |
#include <afxdialogex.h>
|
|
5 |
#include "CDialogCommSet1.h"
|
|
6 |
|
|
7 |
KLink1::KLink1()
|
|
8 |
{
|
|
9 |
|
|
10 |
}
|
|
11 |
|
|
12 |
KLink1::~KLink1()
|
|
13 |
{
|
|
14 |
|
|
15 |
}
|
|
16 |
|
|
17 |
static const KLink1::stKLTypeName KLCoilTypeNames[] =
|
|
18 |
{
|
|
19 |
{KLink1::KLCoilTypeX,"X",KLink1::KLCoilXCount},
|
|
20 |
{KLink1::KLCoilTypeY,"Y",KLink1::KLCoilYCount},
|
|
21 |
{KLink1::KLCoilTypeR,"R",KLink1::KLCoilRCount},
|
|
22 |
{KLink1::KLCoilTypeLX,"LX",KLink1::KLCoilLXCount},
|
|
23 |
{KLink1::KLCoilTypeLY,"LY",KLink1::KLCoilLYCount},
|
|
24 |
{KLink1::KLCoilTypeT,"T",KLink1::KLCoilTCount},
|
|
25 |
{KLink1::KLCoilTypeC,"C",KLink1::KLCoilCCount}
|
|
26 |
};
|
|
27 |
|
|
28 |
static const KLink1::stKLTypeName KLDataTypeNames[] =
|
|
29 |
{
|
|
30 |
|
|
31 |
KLink1::KLDataTypeWX,"WX", KLink1::KLDataWXCount,
|
|
32 |
KLink1::KLDataTypeWY,"WY", KLink1::KLDataWYCount,
|
|
33 |
KLink1::KLDataTypeWR,"WR",KLink1::KLDataWRCount,
|
|
34 |
KLink1::KLDataTypeWLX,"WLX",KLink1::KLDataWLCount,
|
|
35 |
KLink1::KLDataTypeWLY,"WLY",KLink1::KLDataWLCount,
|
|
36 |
KLink1::KLDataTypeDT,"DT",KLink1::KLDataDTCount,
|
|
37 |
KLink1::KLDataTypeSDT,"SDT",KLink1::KLDataSDTCount,
|
|
38 |
KLink1::KLDataTypeSV,"SV",KLink1::KLDataSVCount ,
|
|
39 |
KLink1::KLDataTypeEV,"EV",KLink1::KLDataEVCount ,
|
|
40 |
KLink1::KLDataTypeLD,"LD",KLink1::KLDataLDCount,
|
|
41 |
KLink1::KLDataSysCfg,"Cfg",128,
|
|
42 |
KLink1::KLDataTypeFlash,"Flash",256,
|
|
43 |
KLink1::KLDataTypeTest,"Test",256
|
|
44 |
};
|
|
45 |
|
|
46 |
const char * KLink1::GetErrDescStr(int nErrNo)
|
|
47 |
{
|
|
48 |
switch (nErrNo)
|
|
49 |
{
|
|
50 |
case KL_ERR:
|
|
51 |
return "ERROR";
|
|
52 |
case KL_OK: //=0
|
|
53 |
return "OK";
|
|
54 |
case KL_NG:// = 1,
|
|
55 |
return "NG";
|
|
56 |
case KL_UNKNOWN: // = 2,
|
|
57 |
return "KL_UNKNOWN";
|
|
58 |
case KL_TIMEOUT: // = 3,
|
|
59 |
return "KL_TIMEOUT";
|
|
60 |
case KL_BCC_ERR: // = 4,
|
|
61 |
return "KL_BCC_ERR";
|
|
62 |
case KL_PKG_ERR: // = 5,
|
|
63 |
return "KL_PKG_ERR";
|
|
64 |
case KL_SEQ_ERR: // = 6,
|
|
65 |
return "KL_SEQ_ERR";
|
|
66 |
case KL_LENTH_EXCEED: //,
|
|
67 |
return "KL_LENTH_EXCEED";
|
|
68 |
case KL_NEED_PASS: //,
|
|
69 |
return "KL_NEED_PASS";
|
|
70 |
case KL_NOT_SUPPORT://,
|
|
71 |
return "KL_NOT_SUPPORT";
|
|
72 |
default:
|
|
73 |
return "KL_OTHER_ERR";
|
|
74 |
break;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
int KLink1::xtoi(const char * hexstr, int len)
|
|
79 |
{
|
|
80 |
int len1 = len;
|
|
81 |
if (len <= 0) len1 = (int)strlen(hexstr);
|
|
82 |
int j = 0;
|
|
83 |
for (int i = 0; i < len1; i++)
|
|
84 |
{
|
|
85 |
unsigned char ch = hexstr[i];
|
|
86 |
if (ch >= '0'&&ch <= '9') { int k = ch - '0'; j = j * 16 + k; continue; }
|
|
87 |
if (ch >= 'A'&&ch <= 'F') { int k = ch - 'A' + 10; j = j * 16 + k; continue; }
|
|
88 |
if (ch >= 'a'&&ch <= 'f') { int k = ch - 'a' + 10; j = j * 16 + k; continue; }
|
|
89 |
if (ch == ' ' || ch == ' ') { continue; }
|
|
90 |
break;
|
|
91 |
}
|
|
92 |
return j;
|
|
93 |
}
|
|
94 |
|
|
95 |
unsigned char KLink1::KLBCC(void * pData, int nSize)
|
|
96 |
{
|
|
97 |
unsigned char k = 0;
|
|
98 |
for (int i = 0; i < nSize; i++)
|
|
99 |
{
|
|
100 |
k ^= ((unsigned char *)pData)[i];
|
|
101 |
}
|
|
102 |
return k;
|
|
103 |
}
|
|
104 |
int KLink1::CalCRC(void * pBuf, int nSize)
|
|
105 |
{
|
|
106 |
return 0;
|
|
107 |
}
|
|
108 |
|
|
109 |
int KLink1::SetCallBackFuncs(pCallBackFuncs pFuncSt)
|
|
110 |
{
|
|
111 |
if (pFuncSt->OpenFunc) { m_CallBackFuncs.OpenFunc = pFuncSt->OpenFunc; m_bOpenCallBackSet = 1; }
|
|
112 |
if (pFuncSt->CloseFunc) { m_CallBackFuncs.CloseFunc = pFuncSt->CloseFunc; m_bCloseCallBackSet = 1; }
|
|
113 |
if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; }
|
|
114 |
if (pFuncSt->RecvPkgFunc) { m_CallBackFuncs.RecvPkgFunc = pFuncSt->RecvPkgFunc; m_bRecvCallBackSet = 1; }
|
|
115 |
if (pFuncSt->ClearBufFunc) { m_CallBackFuncs.ClearBufFunc = pFuncSt->ClearBufFunc; m_bClearBufCallBackSet = 1; }
|
|
116 |
if (pFuncSt->OnConnect) { m_CallBackFuncs.OnConnect = pFuncSt->OnConnect; m_bOnConnectCallBackSet = 1; }
|
|
117 |
if (pFuncSt->OnDisConnect) { m_CallBackFuncs.OnDisConnect = pFuncSt->OnDisConnect; m_bOnDisConnectCallBackSet = 1; }
|
|
118 |
if (pFuncSt->OnError) { m_CallBackFuncs.OnError = pFuncSt->OnError; m_bOnErrorCallBackSet = 1; }
|
|
119 |
|
|
120 |
// if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; }
|
|
121 |
|
|
122 |
return KL_OK;
|
|
123 |
}
|
|
124 |
|
|
125 |
|
|
126 |
int KLink1::SetCommParam()
|
|
127 |
{
|
|
128 |
AFX_MANAGE_STATE(AfxGetStaticModuleState());
|
|
129 |
/*
|
|
130 |
//方法2的状态变更
|
|
131 |
HINSTANCE save_hInstance = AfxGetResourceHandle();
|
|
132 |
|
|
133 |
AfxSetResourceHandle(theApp.m_hInstance);
|
|
134 |
|
|
135 |
// CDialog dlg(IDD_DLL_DIALOG);//打开ID为2000的对话框
|
|
136 |
// dlg.DoModal();
|
|
137 |
*/
|
|
138 |
CDialogCommSet1 dialog1;
|
df0321
|
139 |
if (m_bCommParamSet)
|
Q |
140 |
{
|
|
141 |
dialog1.m_nComNum = m_nPort;
|
|
142 |
dialog1.m_nBaudRate = m_nBaudRate;
|
|
143 |
dialog1.m_Settings = m_Settings;
|
|
144 |
dialog1.m_bOpened = m_bOpened;
|
|
145 |
}
|
418cb3
|
146 |
INT_PTR r = dialog1.DoModal();
|
Q |
147 |
//方法2的状态还原
|
|
148 |
|
|
149 |
// AfxSetResourceHandle(save_hInstance);
|
|
150 |
if (r == IDOK)
|
|
151 |
{
|
|
152 |
m_nPort = dialog1.m_nComNum;
|
|
153 |
m_nBaudRate = dialog1.m_nBaudRate;
|
|
154 |
m_Settings = dialog1.m_Settings;
|
|
155 |
m_bCommParamSet = true;
|
|
156 |
}
|
|
157 |
else
|
|
158 |
{
|
|
159 |
return -1;
|
|
160 |
}
|
|
161 |
|
|
162 |
return 0;
|
|
163 |
}
|
|
164 |
int KLink1::fnTest1(int n)
|
|
165 |
{
|
|
166 |
AFX_MANAGE_STATE(AfxGetStaticModuleState());
|
|
167 |
/*
|
|
168 |
//方法2的状态变更
|
|
169 |
HINSTANCE save_hInstance = AfxGetResourceHandle();
|
|
170 |
|
|
171 |
AfxSetResourceHandle(theApp.m_hInstance);
|
|
172 |
|
|
173 |
// CDialog dlg(IDD_DLL_DIALOG);//打开ID为2000的对话框
|
|
174 |
// dlg.DoModal();
|
|
175 |
*/
|
|
176 |
CDialogCommSet1 dialog1;
|
|
177 |
INT_PTR r = dialog1.DoModal();
|
|
178 |
//方法2的状态还原
|
|
179 |
|
|
180 |
// AfxSetResourceHandle(save_hInstance);
|
|
181 |
|
|
182 |
return 0;
|
|
183 |
}
|
|
184 |
|
|
185 |
int KLink1::Open(int OpenParam)
|
|
186 |
{
|
|
187 |
CString s1;
|
|
188 |
if (!m_bCommParamSet)
|
|
189 |
{
|
|
190 |
if (SetCommParam()) return -1;
|
|
191 |
m_bCommParamSet = 1;
|
|
192 |
}
|
|
193 |
MySerPort1.SetParams(m_nPort, m_nBaudRate, _T("8,N,1"));
|
|
194 |
int r = MySerPort1.Open();
|
|
195 |
s1.Format(_T("Open %s = %d"), MySerPort1.m_strResult, r);
|
|
196 |
m_resultStr = s1; // MySerPort1.m_strResult;
|
|
197 |
if (r == MySerPort1.R_OK)
|
|
198 |
{
|
|
199 |
m_bOpened = true;
|
|
200 |
// MyKLink1.Open();
|
|
201 |
// m_static_connect.SetCtlColor(RGB(0, 255, 0));
|
|
202 |
return KL_OK;
|
|
203 |
}
|
|
204 |
else
|
|
205 |
{
|
|
206 |
m_bOpened = false;
|
|
207 |
return r;
|
|
208 |
}
|
|
209 |
/*
|
|
210 |
if (m_bOpenCallBackSet)
|
|
211 |
{
|
|
212 |
m_CallBackFuncs.OpenFunc();
|
|
213 |
}
|
|
214 |
*/
|
|
215 |
m_bOpened = true;
|
|
216 |
return KL_OK;
|
|
217 |
};
|
|
218 |
int KLink1::Close(int CloseParam)
|
|
219 |
{
|
|
220 |
if (m_bCloseCallBackSet)
|
|
221 |
{
|
|
222 |
m_CallBackFuncs.CloseFunc();
|
|
223 |
}
|
|
224 |
m_bOpened = false;
|
|
225 |
return KL_OK;
|
|
226 |
};
|
|
227 |
int KLink1::Connect(int ConnectParam)
|
|
228 |
{
|
|
229 |
int OpenParam = ConnectParam;
|
|
230 |
int res=Open(OpenParam);
|
|
231 |
unsigned short len1;
|
|
232 |
unsigned short buf1[256];
|
|
233 |
//m_resultStr = MySerPort1.m_strResult;
|
|
234 |
return res;
|
|
235 |
if (res == KL_OK)
|
|
236 |
{
|
|
237 |
int res2 = GetInfo(m_Dst, &len1, buf1);
|
|
238 |
|
|
239 |
if (m_bOnConnectCallBackSet) { m_CallBackFuncs.OnConnect(); }
|
|
240 |
return res;
|
|
241 |
}
|
|
242 |
return res;
|
|
243 |
}
|
|
244 |
int KLink1::DisConnect(int DisConnectParam)
|
|
245 |
{
|
|
246 |
if (m_bOnDisConnectCallBackSet) { m_CallBackFuncs.OnDisConnect(DisConnectParam); }
|
|
247 |
return Close();
|
|
248 |
}
|
|
249 |
|
|
250 |
int KLink1::CalSpeed()
|
|
251 |
{
|
|
252 |
double thistime = MySerPort1.GetTimemS();
|
|
253 |
double diftime = thistime - LastTime;
|
|
254 |
if (diftime >= 500)
|
|
255 |
{
|
|
256 |
m_SendSpeed = (DWORD)(m_nSendCount * 1000 / diftime);
|
|
257 |
m_RecvSpeed = static_cast<DWORD> (m_nRecvCount * 1000 / diftime);
|
|
258 |
m_nLastSendCount = m_nSendCount; m_nSendCount = 0;
|
|
259 |
m_nLastRecvCount = m_nRecvCount; m_nRecvCount = 0;
|
|
260 |
LastTime = thistime;
|
|
261 |
}
|
|
262 |
return 0;
|
|
263 |
}
|
|
264 |
|
|
265 |
int KLink1::DoHeartBeat(int nParam)
|
|
266 |
{
|
|
267 |
unsigned short nByteCount;
|
|
268 |
unsigned short buf1[256];
|
|
269 |
int res = GetInfo(m_Dst, &nByteCount, buf1);
|
|
270 |
return KL_OK;
|
|
271 |
}
|
|
272 |
|
|
273 |
void KLink1::OnTimer()
|
|
274 |
{
|
|
275 |
DoHeartBeat();
|
|
276 |
}
|
|
277 |
|
|
278 |
int KLink1::SendPacket(void * pBuf, int Len)
|
|
279 |
{
|
|
280 |
if (m_bOpened && m_bSendCallBackSet)
|
|
281 |
{
|
|
282 |
if (m_bClearBufCallBackSet) m_CallBackFuncs.ClearBufFunc();
|
|
283 |
return m_CallBackFuncs.SendPkgFunc(pBuf, Len);
|
|
284 |
}
|
|
285 |
else return KL_ERR;
|
|
286 |
}
|
|
287 |
|
|
288 |
int KLink1::RecvPacket(void * pBuf, int LenToRead, int ReTryCount)
|
|
289 |
{
|
|
290 |
|
|
291 |
if (m_bOpened && m_bRecvCallBackSet)
|
|
292 |
{
|
|
293 |
int len2 = 0;
|
|
294 |
char * pBufByte = (char *)pBuf;
|
|
295 |
for (int i = 0; i < ReTryCount; i++)
|
|
296 |
{
|
|
297 |
int len1 = m_CallBackFuncs.RecvPkgFunc(pBufByte + len2, LenToRead - len2);
|
|
298 |
if (len1 <= 0 || len1 > 256) {
|
|
299 |
continue;
|
|
300 |
}
|
|
301 |
len2 += len1;
|
|
302 |
if (len2 >= LenToRead) break;
|
|
303 |
}
|
|
304 |
if (len2 >= LenToRead) {
|
|
305 |
m_nContinueErrCount = 0;
|
|
306 |
return len2;
|
|
307 |
}
|
|
308 |
else if (len2 <= 0) {
|
|
309 |
m_nErrCount++;
|
|
310 |
m_nContinueErrCount++;
|
|
311 |
if (m_nContinueErrCount > 10)
|
|
312 |
{
|
|
313 |
// Close(-1);
|
|
314 |
}
|
|
315 |
return KL_ERR;
|
|
316 |
}
|
|
317 |
else {
|
|
318 |
return len2;
|
|
319 |
}
|
|
320 |
}
|
|
321 |
else
|
|
322 |
{
|
|
323 |
return KL_ERR;
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
int KLink1::MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData)
|
|
328 |
{
|
|
329 |
int framelen1 = 0;
|
|
330 |
pKLExtDataFrame pF1 = (pKLExtDataFrame)pBuf;
|
|
331 |
pF1->Dst = nDst;
|
|
332 |
pF1->nType = nType;
|
|
333 |
pF1->Len2 = len2;
|
|
334 |
memcpy(pF1->Data, pData, len2);
|
|
335 |
UCHAR bcc = KLBCC(pBuf, sizeof(tagKLExtDataFrame) + len2 - 1);
|
|
336 |
pF1->Data[len2] = bcc;
|
|
337 |
|
|
338 |
framelen1 = sizeof(tagKLExtDataFrame) + len2;
|
|
339 |
return framelen1;
|
|
340 |
}
|
|
341 |
/*
|
|
342 |
int KLink1::MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData)
|
|
343 |
{
|
|
344 |
int PkgLen1 = 0;
|
|
345 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
346 |
pPacket->StartSign = KLSignStart;
|
|
347 |
pPacket->DstAddr = nDst;
|
|
348 |
pPacket->Stat = Stat;
|
|
349 |
pPacket->Cmd = nCMD;
|
|
350 |
pPacket->Type1 = Type;
|
|
351 |
int Datalen = 0;
|
|
352 |
int nWordAddr;
|
|
353 |
int nWordCount;
|
|
354 |
|
|
355 |
switch (nCMD)
|
|
356 |
{
|
|
357 |
case KLCmdNone:
|
|
358 |
break;
|
|
359 |
case KLCmdPing:
|
|
360 |
Datalen = Type;
|
|
361 |
memcpy(pPacket->Params, pData, Datalen);
|
|
362 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
363 |
break;
|
|
364 |
case KLCmdGetInfo:
|
|
365 |
break;
|
|
366 |
case KLCmdRead:
|
|
367 |
Datalen = 0;
|
|
368 |
pPacket->Params[0] = nAddr % 256;
|
|
369 |
pPacket->Params[1] = nAddr / 256;
|
|
370 |
pPacket->Params[2] = nCount % 256;
|
|
371 |
pPacket->Params[3] = nCount / 256;
|
|
372 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
373 |
break;
|
|
374 |
case KLCmdWrite:
|
|
375 |
pPacket->Params[0] = nAddr % 256;
|
|
376 |
pPacket->Params[1] = nAddr / 256;
|
|
377 |
pPacket->Params[2] = nCount % 256;
|
|
378 |
pPacket->Params[3] = nCount / 256;
|
|
379 |
Datalen = nCount;
|
|
380 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
381 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
382 |
|
|
383 |
break;
|
|
384 |
case KLCmdSaveToFlash:
|
|
385 |
break;
|
|
386 |
case KLCmdExChgData:
|
|
387 |
break;
|
|
388 |
case KLCmdGetEventLogCount:
|
|
389 |
pPacket->Params[0] = 0;
|
|
390 |
pPacket->Params[1] = 0;
|
|
391 |
pPacket->Params[2] = 0;
|
|
392 |
pPacket->Params[3] = 0;
|
|
393 |
Datalen = 0;
|
|
394 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
395 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
396 |
default:
|
|
397 |
break;
|
|
398 |
}
|
|
399 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
400 |
{
|
|
401 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
402 |
pPacket->Params[PkgLen1-sizeof(stKLReqPacket)] = BCC;
|
|
403 |
}
|
|
404 |
return PkgLen1;
|
|
405 |
}
|
|
406 |
*/
|
|
407 |
int KLink1::MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData, int ExtFrameLen, void * pExtDataFrame)
|
|
408 |
{
|
|
409 |
int PkgLen1 = 0;
|
|
410 |
pKLReqPacket pPacket = (pKLReqPacket)pBuf;
|
|
411 |
pPacket->StartSign = KLSignStart;
|
|
412 |
pPacket->DstAddr = nDst;
|
|
413 |
pPacket->Stat = Stat;
|
|
414 |
pPacket->Cmd = nCMD;
|
|
415 |
pPacket->Type1 = Type;
|
|
416 |
|
|
417 |
pKLStat pStat = (pKLStat)&(pPacket->Stat);
|
|
418 |
int Datalen = 0;
|
|
419 |
int nWordAddr = nAddr;
|
|
420 |
int nWordCount = nCount;
|
|
421 |
pPacket->Params[0] = LOBYTE(nAddr);
|
|
422 |
pPacket->Params[1] = HIBYTE(nAddr);
|
|
423 |
pPacket->Params[2] = nCount % 256;
|
|
424 |
pPacket->Params[3] = nCount / 256;
|
|
425 |
|
|
426 |
switch (nCMD)
|
|
427 |
{
|
|
428 |
case KLCmdNone:
|
|
429 |
break;
|
|
430 |
case KLCmdPing:
|
|
431 |
Datalen = Type;
|
|
432 |
memcpy(pPacket->Params, pData, Datalen);
|
|
433 |
PkgLen1 = sizeof(stKLReqPacket) + Datalen;
|
|
434 |
break;
|
|
435 |
case KLCmdGetInfo:
|
|
436 |
case KLCmdGetSN:
|
|
437 |
Datalen = 0;
|
|
438 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
439 |
break;
|
|
440 |
case KLCmdGetFactoryData:
|
|
441 |
Datalen = 0;
|
|
442 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
443 |
break;
|
|
444 |
case KLCmdWriteFactoryData:
|
|
445 |
Datalen = nCount;
|
|
446 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
447 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
448 |
break;
|
|
449 |
case KLCmdRead:
|
|
450 |
Datalen = 0;
|
|
451 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
452 |
break;
|
|
453 |
case KLCmdWrite:
|
|
454 |
Datalen = nCount;
|
|
455 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
456 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
457 |
break;
|
|
458 |
case KLCmdRead1Bit:
|
|
459 |
Datalen = 0;
|
|
460 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
461 |
break;
|
|
462 |
case KLCmdWrite1Bit:
|
|
463 |
Datalen = 1;
|
|
464 |
memcpy(pPacket->Params + 2, pData, Datalen);
|
|
465 |
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
|
466 |
break;
|
|
467 |
|
|
468 |
case KLCmdReadBits:
|
|
469 |
Datalen = 0;
|
|
470 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
471 |
break;
|
|
472 |
case KLCmdWriteBits:
|
|
473 |
Datalen = 0;
|
|
474 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
475 |
break;
|
|
476 |
case KLCmdReadDataByte:
|
|
477 |
case KLCmdReadDataWord:
|
|
478 |
case KLCmdReadData:
|
|
479 |
Datalen = 0;
|
|
480 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
481 |
break;
|
|
482 |
case KLCmdWriteDataByte:
|
|
483 |
case KLCmdWriteDataWord:
|
|
484 |
case KLCmdWriteData:
|
|
485 |
Datalen = nCount;
|
|
486 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
487 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
488 |
break;
|
|
489 |
case KLCmdSaveToFlash:
|
|
490 |
break;
|
|
491 |
case KLCmdExChgData:
|
|
492 |
break;
|
|
493 |
case KLCmdReadProgram:
|
|
494 |
Datalen = 0;
|
|
495 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
496 |
break;
|
|
497 |
case KLCmdStartProgram:
|
|
498 |
Datalen = 0;
|
|
499 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
500 |
break;
|
|
501 |
case KLCmdWriteProgram:
|
|
502 |
Datalen = nCount;
|
|
503 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
504 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
505 |
break;
|
|
506 |
case KLCmdFinishProgram:
|
|
507 |
Datalen = 0;
|
|
508 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
509 |
break;
|
|
510 |
|
|
511 |
case KLCmdReadRunStat:
|
|
512 |
Datalen = 0;
|
|
513 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
514 |
break;
|
|
515 |
|
|
516 |
case KLCmdWriteSysCfg:
|
|
517 |
Datalen = nCount;
|
|
518 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
519 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
520 |
break;
|
|
521 |
case KLCmdSaveSysCfg:
|
|
522 |
pPacket->Params[0] = 0;
|
|
523 |
pPacket->Params[1] = 0;
|
|
524 |
pPacket->Params[2] = 0;
|
|
525 |
pPacket->Params[3] = 0;
|
|
526 |
Datalen = 0;
|
|
527 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
528 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
529 |
break;
|
|
530 |
case KLCmdSaveRunStat:
|
|
531 |
pPacket->Params[0] = 0;
|
|
532 |
pPacket->Params[1] = 0;
|
|
533 |
pPacket->Params[2] = 0;
|
|
534 |
pPacket->Params[3] = 0;
|
|
535 |
Datalen = 0;
|
|
536 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
537 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
538 |
break;
|
|
539 |
case KLCmdClearEventLog:
|
|
540 |
pPacket->Params[0] = 0;
|
|
541 |
pPacket->Params[1] = 0;
|
|
542 |
pPacket->Params[2] = 0;
|
|
543 |
pPacket->Params[3] = 0;
|
|
544 |
Datalen = 0;
|
|
545 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
546 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
547 |
|
|
548 |
case KLCmdGetEventLogCount:
|
|
549 |
pPacket->Params[0] = 0;
|
|
550 |
pPacket->Params[1] = 0;
|
|
551 |
pPacket->Params[2] = 0;
|
|
552 |
pPacket->Params[3] = 0;
|
|
553 |
Datalen = 0;
|
|
554 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
555 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
556 |
break;
|
|
557 |
case KLCmdGetEventLog:
|
|
558 |
pPacket->Params[0] = nAddr % 256;
|
|
559 |
pPacket->Params[1] = nAddr / 256;
|
|
560 |
pPacket->Params[2] = nCount % 256;
|
|
561 |
pPacket->Params[3] = nCount / 256;
|
|
562 |
Datalen = 0;
|
|
563 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
564 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
565 |
break;
|
|
566 |
|
|
567 |
default:
|
|
568 |
pPacket->Params[0] = nAddr % 256;
|
|
569 |
pPacket->Params[1] = nAddr / 256;
|
|
570 |
pPacket->Params[2] = nCount % 256;
|
|
571 |
pPacket->Params[3] = nCount / 256;
|
|
572 |
Datalen = 0;
|
|
573 |
memcpy(pPacket->Params + 4, pData, Datalen);
|
|
574 |
PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen;
|
|
575 |
break;
|
|
576 |
}
|
|
577 |
|
|
578 |
if (PkgLen1 >= sizeof(stKLReqPacket))
|
|
579 |
{
|
|
580 |
UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1);
|
|
581 |
pPacket->Params[PkgLen1 - sizeof(stKLReqPacket)] = BCC;
|
|
582 |
}
|
|
583 |
|
|
584 |
if (pStat->HasExt && ExtFrameLen > 0 && pExtDataFrame != NULL)
|
|
585 |
{
|
|
586 |
|
|
587 |
memcpy(pPacket->Params + PkgLen1 - sizeof(stKLReqPacket) + 1, pExtDataFrame, ExtFrameLen);
|
|
588 |
PkgLen1 += ExtFrameLen;
|
|
589 |
};
|
|
590 |
return PkgLen1;
|
|
591 |
}
|
|
592 |
|
|
593 |
|
|
594 |
int KLink1::CheckPackage(void * pBuf, int nSize)
|
|
595 |
{
|
|
596 |
pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
|
|
597 |
if (p1->RplyStSgn != KLSignReply)
|
|
598 |
{
|
|
599 |
return KL_UNKNOWN;
|
|
600 |
}
|
|
601 |
|
|
602 |
int DataLen = 0; //p1->LoadLen;
|
|
603 |
if (DataLen > KLMaxPacketLength)
|
|
604 |
{
|
|
605 |
return KL_PKG_ERR;
|
|
606 |
}
|
|
607 |
if (nSize < DataLen + sizeof(stKLRplyPktHdr))
|
|
608 |
{
|
|
609 |
//len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket));
|
|
610 |
//PutStr(str3,len4);
|
|
611 |
return KL_PKG_ERR; //not long enough
|
|
612 |
}
|
|
613 |
// if (p1->data[DataLen+1] != EndSign)
|
|
614 |
// {
|
|
615 |
// ClientStat[nCurPollId].NoEndErr++;
|
|
616 |
// Uart2Stat.LengthErr++;
|
|
617 |
// return -2;
|
|
618 |
// }
|
|
619 |
unsigned char thisBCC = KLBCC(p1, nSize - 1);
|
|
620 |
if (thisBCC != ((UCHAR *)pBuf)[nSize - 1])
|
|
621 |
{//BCC Error;
|
|
622 |
return KL_BCC_ERR;
|
|
623 |
}
|
|
624 |
return KL_OK;
|
|
625 |
|
|
626 |
return true;
|
|
627 |
return false;
|
|
628 |
}
|
|
629 |
|
|
630 |
int KLink1::ProcessPacket(void *pBuf, int nLen)
|
|
631 |
{
|
|
632 |
return false;
|
|
633 |
}
|
|
634 |
|
|
635 |
int KLink1::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData)
|
|
636 |
{
|
|
637 |
if (nPkgLen < 0) { return KL_ERR; }
|
|
638 |
int res = CheckPackage(pBuf, nPkgLen);
|
|
639 |
if (res != KL_OK) return res;
|
|
640 |
int len1 = 0;
|
|
641 |
pKLRplyPktHdr pPacket = (pKLRplyPktHdr)pBuf;
|
|
642 |
UCHAR Sign = pPacket->RplyStSgn;
|
|
643 |
UCHAR nDst = pPacket->DstAddr;
|
|
644 |
*nCmd = pPacket->nRplyCMD;
|
|
645 |
UCHAR nStatus = pPacket->nStatus;
|
|
646 |
*Status = nStatus;
|
|
647 |
m_nRSeq = ((pKLStat)&nStatus)->nSEQ;
|
|
648 |
int Datalen = pPacket->nSize1;
|
|
649 |
*nCount = Datalen;
|
|
650 |
switch (*nCmd)
|
|
651 |
{
|
|
652 |
case KLCmdNone:
|
|
653 |
break;
|
|
654 |
case KLCmdPing:
|
|
655 |
case KLCmdPingReply:
|
|
656 |
*nCount = Datalen;
|
|
657 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
658 |
break;
|
|
659 |
case KLCmdGetInfo:
|
|
660 |
case KLCmdVerInfo:
|
|
661 |
*nCount = Datalen;
|
|
662 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
663 |
break;
|
|
664 |
case KLCmdGetUid:
|
|
665 |
case KLCmdGetSN:
|
|
666 |
case KLCmdGetFactoryData:
|
|
667 |
*nCount = Datalen;
|
|
668 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
669 |
break;
|
|
670 |
case KLCmdWriteFactoryData:
|
|
671 |
*nCount = Datalen;
|
|
672 |
break;
|
|
673 |
case KLCmdRead:
|
|
674 |
case KLCmdReadReply:
|
|
675 |
*nCount = Datalen;
|
|
676 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
677 |
break;
|
|
678 |
case KLCmdWrite:
|
|
679 |
case KLCmdWriteReply:
|
|
680 |
*nCount = Datalen;
|
|
681 |
break;
|
|
682 |
case KLCmdRead1Bit:
|
|
683 |
case KLCmdReadBits:
|
|
684 |
*nCount = Datalen;
|
|
685 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
686 |
break;
|
|
687 |
case KLCmdReadDataByte:
|
|
688 |
*nCount = Datalen;
|
|
689 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
690 |
break;
|
|
691 |
case KLCmdReadDataWord:
|
|
692 |
case KLCmdReadData:
|
|
693 |
*nCount = Datalen;
|
|
694 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
695 |
break;
|
|
696 |
case KLCmdWriteDataByte:
|
|
697 |
case KLCmdWriteDataWord:
|
|
698 |
case KLCmdWriteData:
|
|
699 |
*nCount = Datalen;
|
|
700 |
break;
|
|
701 |
|
|
702 |
case KLCmdSaveToFlash:
|
|
703 |
break;
|
|
704 |
|
|
705 |
case KLCmdReadProgram:
|
|
706 |
*nCount = Datalen;
|
|
707 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
708 |
break;
|
|
709 |
case KLCmdStartProgram:
|
|
710 |
*nCount = Datalen;
|
|
711 |
break;
|
|
712 |
case KLCmdWriteProgram:
|
|
713 |
*nCount = Datalen;
|
|
714 |
break;
|
|
715 |
case KLCmdFinishProgram:
|
|
716 |
*nCount = Datalen;
|
|
717 |
break;
|
|
718 |
|
|
719 |
case KLCmdReadRunStat:
|
|
720 |
*nCount = Datalen;
|
|
721 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
722 |
break;
|
|
723 |
case KLCmdReadSysCfg:
|
|
724 |
*nCount = Datalen;
|
|
725 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
726 |
break;
|
|
727 |
case KLCmdExChgData:
|
|
728 |
case KLCmdExChgDataReply:
|
|
729 |
break;
|
|
730 |
case KLCmdGetEventLogCount:
|
|
731 |
*nCount = Datalen;
|
|
732 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
733 |
break;
|
|
734 |
case KLCmdGetEventLog:
|
|
735 |
*nCount = Datalen;
|
|
736 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
737 |
break;
|
|
738 |
|
|
739 |
case KLCmdErrRply:
|
|
740 |
*nCount = Datalen;
|
|
741 |
memcpy(pData, pPacket->Datas, Datalen);
|
|
742 |
break;
|
|
743 |
default:
|
|
744 |
*nCount = Datalen;
|
|
745 |
//memcpy(pData, pPacket->Datas, Datalen);
|
|
746 |
break;
|
|
747 |
}
|
|
748 |
return KL_OK;
|
|
749 |
|
|
750 |
return false;
|
|
751 |
}
|
|
752 |
|
|
753 |
int KLink1::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value)
|
|
754 |
{
|
|
755 |
m_Dst = nDst;
|
|
756 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr);
|
|
757 |
SendPacket(m_Packetbuf, len1);
|
|
758 |
int numToRead = sizeof(stKLRplyPktHdr) + 1;
|
|
759 |
|
|
760 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
761 |
unsigned char nCmd;
|
|
762 |
unsigned short nCount = 0;
|
|
763 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
764 |
Value[0] = m_DataBuf[0];
|
|
765 |
return KL_OK;
|
|
766 |
}
|
|
767 |
int KLink1::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value)
|
|
768 |
{
|
|
769 |
m_Dst = nDst;
|
|
770 |
m_DataBuf[0] = Value;
|
|
771 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr, 1, m_DataBuf);
|
|
772 |
SendPacket(m_Packetbuf, len1);
|
|
773 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
774 |
unsigned char nCmd;
|
|
775 |
unsigned short nCount = 0;
|
|
776 |
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
|
777 |
return KL_OK;
|
|
778 |
}
|
|
779 |
int KLink1::ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
|
780 |
{
|
|
781 |
return KL_OK;
|
|
782 |
}
|
|
783 |
int KLink1::WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
|
784 |
{
|
|
785 |
return KL_OK;
|
|
786 |
}
|
|
787 |
int KLink1::ReadBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
|
788 |
{
|
|
789 |
return KL_OK;
|
|
790 |
}
|
|
791 |
int KLink1::WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
|
792 |
{
|
|
793 |
return KL_OK;
|
|
794 |
}
|
|
795 |
int KLink1::GetNextSeq()
|
|
796 |
{
|
|
797 |
m_nSeq++;
|
|
798 |
if (m_nSeq >= 8) m_nSeq = 0;
|
|
799 |
m_Stat1.nSEQ = m_nSeq;
|
|
800 |
return m_nSeq;
|
|
801 |
}
|
|
802 |
|
|
803 |
int KLink1::ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, USHORT * nByteRead, UCHAR * Values)
|
|
804 |
{
|
|
805 |
m_Dst = nDst;
|
|
806 |
m_resultStr.Empty();
|
|
807 |
UCHAR nExpSeq = GetNextSeq();
|
|
808 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr / 2, nByteCount);
|
|
809 |
SendPacket(m_Packetbuf, len1);
|
|
810 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
811 |
int len2 = 0;
|
|
812 |
len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
813 |
if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"), len2, numToRead); return KL_ERR; }
|
|
814 |
if (len2 < numToRead)
|
|
815 |
{
|
|
816 |
CString s1;
|
|
817 |
for (int i = 0; i < len2; i++)
|
|
818 |
{
|
|
819 |
s1.AppendFormat(_T("%02X "), m_RecvBuf[i]);
|
|
820 |
}
|
|
821 |
s1.Append(_T("\r\n"));
|
|
822 |
m_resultStr.Format(_T("ToRead %d R= %d \r\n"), numToRead, len2);
|
|
823 |
m_resultStr += s1;
|
|
824 |
}
|
|
825 |
unsigned char nCmd;
|
|
826 |
unsigned short nnCount = 0;
|
|
827 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
828 |
if (res == KL_OK)
|
|
829 |
{
|
|
830 |
for (int i = 0; i < nnCount; i++)
|
|
831 |
{
|
|
832 |
Values[i] = m_DataBuf[i];
|
|
833 |
}
|
|
834 |
}
|
|
835 |
else
|
|
836 |
{
|
|
837 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
838 |
CString s1;
|
|
839 |
s1 = GetErrDescStr(res);
|
|
840 |
m_resultStr += s1;
|
|
841 |
return res;
|
|
842 |
}
|
|
843 |
*nByteRead = nnCount;
|
|
844 |
return KL_OK;
|
|
845 |
}
|
|
846 |
|
|
847 |
int KLink1::WriteDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values)
|
|
848 |
{
|
|
849 |
m_Dst = nDst;
|
|
850 |
m_resultStr.Empty();
|
|
851 |
UCHAR nExpSeq = GetNextSeq();
|
|
852 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr / 2, nByteCount, Values);
|
|
853 |
SendPacket(m_Packetbuf, len1);
|
|
854 |
nByteCount = 0;
|
|
855 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
856 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
857 |
// if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
858 |
if (len2 <= 0) return KL_ERR;
|
|
859 |
unsigned char nCmd;
|
|
860 |
unsigned short nnCount = 0;
|
|
861 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
862 |
return res;
|
|
863 |
}
|
|
864 |
|
|
865 |
int KLink1::ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * nByteRead, USHORT * Values)
|
|
866 |
{
|
|
867 |
m_Dst = nDst;
|
|
868 |
m_resultStr.Empty();
|
|
869 |
UCHAR nExpSeq = GetNextSeq();
|
|
870 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nWordCount * 2);
|
|
871 |
SendPacket(m_Packetbuf, len1);
|
|
872 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount * 2;
|
|
873 |
|
|
874 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
875 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
876 |
if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"), len2, numToRead); return KL_ERR; }
|
|
877 |
if (len2 < numToRead)
|
|
878 |
{
|
|
879 |
CString s1;
|
|
880 |
for (int i = 0; i < len2; i++)
|
|
881 |
{
|
|
882 |
s1.AppendFormat(_T("%02X "), m_RecvBuf[i]);
|
|
883 |
}
|
|
884 |
s1.Append(_T("\r\n"));
|
|
885 |
m_resultStr.Format(_T("ToRead %d R= %d \r\n"), numToRead, len2);
|
|
886 |
m_resultStr += s1;
|
|
887 |
}
|
|
888 |
unsigned char nCmd;
|
|
889 |
unsigned short nnCount = 0;
|
|
890 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
891 |
if (res == KL_OK)
|
|
892 |
{
|
|
893 |
for (int i = 0; i < nnCount / 2; i++)
|
|
894 |
{
|
|
895 |
// Values[0] = m_DataBuf[0];
|
|
896 |
Values[i] = m_DataBuf[i * 2] + (m_DataBuf[i * 2 + 1] << 8);
|
|
897 |
}
|
|
898 |
}
|
|
899 |
else
|
|
900 |
{
|
|
901 |
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
|
902 |
CString s1;
|
|
903 |
s1 = GetErrDescStr(res);
|
|
904 |
m_resultStr += s1;
|
|
905 |
return res;
|
|
906 |
}
|
|
907 |
*nByteRead = nnCount;
|
|
908 |
return KL_OK;
|
|
909 |
}
|
|
910 |
|
|
911 |
int KLink1::WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
912 |
{
|
|
913 |
m_Dst = nDst;
|
|
914 |
m_resultStr.Empty();
|
|
915 |
UCHAR nExpSeq = GetNextSeq();
|
|
916 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nWordCount, Values);
|
|
917 |
SendPacket(m_Packetbuf, len1);
|
|
918 |
nWordCount = 0;
|
|
919 |
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
|
920 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
921 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
922 |
if (len2 == 0) return KL_ERR;
|
|
923 |
unsigned char nCmd;
|
|
924 |
unsigned short nnCount = 0;
|
|
925 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
926 |
Values[0] = m_DataBuf[0];
|
|
927 |
return res;
|
|
928 |
}
|
|
929 |
|
|
930 |
int KLink1::ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * nByteRead, USHORT * Values)
|
|
931 |
{
|
|
932 |
m_Dst = nDst;
|
|
933 |
m_resultStr.Empty();
|
|
934 |
UCHAR nExpSeq = GetNextSeq();
|
|
935 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr, nWordCount);
|
|
936 |
SendPacket(m_Packetbuf, len1);
|
|
937 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
938 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
939 |
if (len2 == 0) return KL_ERR;
|
|
940 |
unsigned char nCmd;
|
|
941 |
unsigned short nnCount = 0;
|
|
942 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
943 |
for (int i = 0; i < nnCount; i++) Values[i] = m_DataBuf[i];
|
|
944 |
*nByteRead = nnCount;
|
|
945 |
return res;
|
|
946 |
|
|
947 |
return KL_OK;
|
|
948 |
}
|
|
949 |
int KLink1::WriteData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
950 |
{
|
|
951 |
m_Dst = nDst;
|
|
952 |
m_resultStr.Empty();
|
|
953 |
UCHAR nExpSeq = GetNextSeq();
|
|
954 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nWordCount, Values);
|
|
955 |
SendPacket(m_Packetbuf, len1);
|
|
956 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
957 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
958 |
if (len2 == 0) return KL_ERR;
|
|
959 |
unsigned char nCmd;
|
|
960 |
unsigned short nnCount = 0;
|
|
961 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
962 |
// Values[0] = m_DataBuf[0];
|
|
963 |
return res;
|
|
964 |
|
|
965 |
return KL_OK;
|
|
966 |
}
|
|
967 |
|
|
968 |
int KLink1::GetInfo(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
|
969 |
{
|
|
970 |
m_Dst = nDst;
|
|
971 |
m_resultStr.Empty();
|
|
972 |
UCHAR nExpSeq = GetNextSeq();
|
|
973 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, 0, 0, 0, Values);
|
|
974 |
CString s1;
|
|
975 |
for (int i = 0; i < len1; i++) {
|
|
976 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
977 |
}
|
|
978 |
//m_resultStr = s1;
|
|
979 |
SendPacket(m_Packetbuf, len1);
|
|
980 |
int numToRead = sizeof(stKLRplyPktHdr) + 32;
|
|
981 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
982 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
983 |
if (len2 == 0) return KL_ERR;
|
|
984 |
unsigned char nCmd;
|
|
985 |
unsigned short nnCount = 0;
|
|
986 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
987 |
// Values[0] = m_DataBuf[0];
|
|
988 |
*nReadBytes = nnCount;
|
|
989 |
return res;
|
|
990 |
|
|
991 |
return KL_OK;
|
|
992 |
}
|
|
993 |
|
|
994 |
int KLink1::GetSN(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
|
995 |
{
|
|
996 |
m_Dst = nDst;
|
|
997 |
m_resultStr.Empty();
|
|
998 |
UCHAR nExpSeq = GetNextSeq();
|
|
999 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetSN, 0, 0, 4, Values);
|
|
1000 |
CString s1;
|
|
1001 |
for (int i = 0; i < len1; i++) {
|
|
1002 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
1003 |
}
|
|
1004 |
m_resultStr = s1;
|
|
1005 |
SendPacket(m_Packetbuf, len1);
|
|
1006 |
int ByteCount = 4;
|
|
1007 |
int numToRead = sizeof(stKLRplyPktHdr) + ByteCount;
|
|
1008 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1009 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1010 |
if (len2 == 0) return KL_ERR;
|
|
1011 |
unsigned char nCmd;
|
|
1012 |
unsigned short nnCount = 0;
|
|
1013 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
1014 |
// Values[0] = m_DataBuf[0];
|
|
1015 |
*nReadBytes = nnCount;
|
|
1016 |
return res;
|
|
1017 |
|
|
1018 |
return KL_OK;
|
|
1019 |
return 0;
|
|
1020 |
}
|
|
1021 |
int KLink1::GetUID(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
|
1022 |
{
|
|
1023 |
m_Dst = nDst;
|
|
1024 |
m_resultStr.Empty();
|
|
1025 |
UCHAR nExpSeq = GetNextSeq();
|
|
1026 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetUid, 0, 0, 12, Values);
|
|
1027 |
CString s1;
|
|
1028 |
for (int i = 0; i < len1; i++) {
|
|
1029 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
1030 |
}
|
|
1031 |
m_resultStr = s1;
|
|
1032 |
SendPacket(m_Packetbuf, len1);
|
|
1033 |
|
|
1034 |
int numToRead = sizeof(stKLRplyPktHdr) + 12;
|
|
1035 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1036 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1037 |
if (len2 == 0) return KL_ERR;
|
|
1038 |
unsigned char nCmd;
|
|
1039 |
unsigned short nnCount = 0;
|
|
1040 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
1041 |
// Values[0] = m_DataBuf[0];
|
|
1042 |
*nReadBytes = nnCount;
|
|
1043 |
return res;
|
|
1044 |
|
|
1045 |
return KL_OK;
|
|
1046 |
return 0;
|
|
1047 |
}
|
|
1048 |
|
|
1049 |
int KLink1::ReadFactoryData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * nReadBytes, USHORT * Values)
|
|
1050 |
{
|
|
1051 |
m_Dst = nDst;
|
|
1052 |
m_resultStr.Empty();
|
|
1053 |
UCHAR nExpSeq = GetNextSeq();
|
|
1054 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetFactoryData, 0, nStartAddr, nByteCount, Values);
|
|
1055 |
CString s1;
|
|
1056 |
for (int i = 0; i < len1; i++) {
|
|
1057 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
1058 |
}
|
|
1059 |
m_resultStr = s1;
|
|
1060 |
SendPacket(m_Packetbuf, len1);
|
|
1061 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1062 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1063 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1064 |
if (len2 == 0) return KL_ERR;
|
|
1065 |
unsigned char nCmd;
|
|
1066 |
unsigned short nnCount = 0;
|
|
1067 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
1068 |
// Values[0] = m_DataBuf[0];
|
|
1069 |
*nReadBytes = nnCount;
|
|
1070 |
return res;
|
|
1071 |
return KL_OK;
|
|
1072 |
}
|
|
1073 |
int KLink1::WriteFactoryData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * Values)
|
|
1074 |
{
|
|
1075 |
|
|
1076 |
m_Dst = nDst;
|
|
1077 |
m_resultStr.Empty();
|
|
1078 |
UCHAR nExpSeq = GetNextSeq();
|
|
1079 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteFactoryData, 0, nStartAddr, nByteCount, Values);
|
|
1080 |
SendPacket(m_Packetbuf, len1);
|
|
1081 |
int nReadBytes = 0;
|
|
1082 |
int numToRead = sizeof(stKLRplyPktHdr) + nReadBytes;
|
|
1083 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1084 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1085 |
if (len2 == 0) return KL_ERR;
|
|
1086 |
unsigned char nCmd;
|
|
1087 |
unsigned short nnCount = 0;
|
|
1088 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1089 |
// Values[0] = m_DataBuf[0];
|
|
1090 |
return res;
|
|
1091 |
|
|
1092 |
return KL_OK;
|
|
1093 |
}
|
|
1094 |
|
|
1095 |
|
|
1096 |
int KLink1::ReadSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * nReadBytes, USHORT * Values)
|
|
1097 |
{
|
|
1098 |
m_Dst = nDst;
|
|
1099 |
m_resultStr.Empty();
|
|
1100 |
UCHAR nExpSeq = GetNextSeq();
|
|
1101 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadSysCfg, 0, nStartAddr, nByteCount, Values);
|
|
1102 |
CString s1;
|
|
1103 |
for (int i = 0; i < len1; i++) {
|
|
1104 |
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
|
1105 |
}
|
|
1106 |
m_resultStr = s1;
|
|
1107 |
SendPacket(m_Packetbuf, len1);
|
|
1108 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1109 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1110 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1111 |
if (len2 == 0) return KL_ERR;
|
|
1112 |
unsigned char nCmd;
|
|
1113 |
unsigned short nnCount = 0;
|
|
1114 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
|
1115 |
// Values[0] = m_DataBuf[0];
|
|
1116 |
*nReadBytes = nnCount;
|
|
1117 |
return res;
|
|
1118 |
return KL_OK;
|
|
1119 |
}
|
|
1120 |
int KLink1::WriteSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * Values)
|
|
1121 |
{
|
|
1122 |
|
|
1123 |
m_Dst = nDst;
|
|
1124 |
m_resultStr.Empty();
|
|
1125 |
UCHAR nExpSeq = GetNextSeq();
|
|
1126 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteSysCfg, 0, nStartAddr, nByteCount, Values);
|
|
1127 |
SendPacket(m_Packetbuf, len1);
|
|
1128 |
int nReadBytes = 0;
|
|
1129 |
int numToRead = sizeof(stKLRplyPktHdr) + nReadBytes;
|
|
1130 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1131 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1132 |
if (len2 == 0) return KL_ERR;
|
|
1133 |
unsigned char nCmd;
|
|
1134 |
unsigned short nnCount = 0;
|
|
1135 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1136 |
// Values[0] = m_DataBuf[0];
|
|
1137 |
return res;
|
|
1138 |
|
|
1139 |
return KL_OK;
|
|
1140 |
}
|
|
1141 |
int KLink1::GetEventLogCount(UCHAR nDst, int * nCount)
|
|
1142 |
{
|
|
1143 |
m_Dst = nDst;
|
|
1144 |
m_resultStr.Empty();
|
|
1145 |
UCHAR nExpSeq = GetNextSeq();
|
|
1146 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLogCount, 0);
|
|
1147 |
SendPacket(m_Packetbuf, len1);
|
|
1148 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
1149 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
1150 |
if (len2 == 0) return KL_ERR;
|
|
1151 |
unsigned char nCmd;
|
|
1152 |
unsigned short nnCount = 0;
|
|
1153 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1154 |
nCount[0] = *(int *)m_DataBuf;
|
|
1155 |
return res;
|
|
1156 |
|
|
1157 |
return KL_OK;
|
|
1158 |
}
|
|
1159 |
|
|
1160 |
int KLink1::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
|
1161 |
{
|
|
1162 |
m_Dst = nDst;
|
|
1163 |
m_resultStr.Empty();
|
|
1164 |
UCHAR nExpSeq = GetNextSeq();
|
|
1165 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLog, 0, nStartIndex, nCount, 0);
|
|
1166 |
SendPacket(m_Packetbuf, len1);
|
|
1167 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
1168 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
1169 |
if (len2 == 0) return KL_ERR;
|
|
1170 |
unsigned char nCmd;
|
|
1171 |
unsigned short nnCount = 0;
|
|
1172 |
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1173 |
|
|
1174 |
memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
|
|
1175 |
|
|
1176 |
return res;
|
|
1177 |
|
|
1178 |
return KL_OK;
|
|
1179 |
}
|
|
1180 |
|
|
1181 |
int KLink1::GetDateTime32(UCHAR nDst, UINT * pValue)
|
|
1182 |
{
|
|
1183 |
int res = KL_OK;
|
|
1184 |
unsigned short nCount;
|
|
1185 |
res = ReadDataByte(nDst, KLDataTypeSDT, 36, 4, &nCount, (UCHAR *)pValue);
|
|
1186 |
return res;
|
|
1187 |
|
|
1188 |
}
|
|
1189 |
|
|
1190 |
int KLink1::SetDateTime32(UCHAR nDst, UINT Value)
|
|
1191 |
{
|
|
1192 |
int res = KL_OK;
|
|
1193 |
res = WriteDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)&Value);
|
|
1194 |
return res;
|
|
1195 |
}
|
|
1196 |
|
df0321
|
1197 |
int KLink1::GetMode(UCHAR nDst, UCHAR nType, UCHAR * value)
|
418cb3
|
1198 |
{
|
Q |
1199 |
int res = KL_OK;
|
|
1200 |
// res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
|
|
1201 |
m_Dst = nDst;
|
|
1202 |
UCHAR nExpSeq = GetNextSeq();
|
|
1203 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetMode, nType);
|
|
1204 |
SendPacket(m_Packetbuf, len1);
|
|
1205 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
1206 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
1207 |
if (len2 == 0) return KL_ERR;
|
|
1208 |
unsigned char nCmd;
|
|
1209 |
unsigned short nnCount = 0;
|
|
1210 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
df0321
|
1211 |
if (nnCount > 0) { *value = m_DataBuf[0]; }
|
418cb3
|
1212 |
return res;
|
Q |
1213 |
|
|
1214 |
}
|
|
1215 |
|
|
1216 |
|
|
1217 |
int KLink1::ChangeMode(UCHAR nDst, UCHAR nType)
|
|
1218 |
{
|
|
1219 |
int res = KL_OK;
|
|
1220 |
// res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
|
1221 |
m_Dst = nDst;
|
|
1222 |
UCHAR nExpSeq = GetNextSeq();
|
|
1223 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdChgMode, nType);
|
|
1224 |
SendPacket(m_Packetbuf, len1);
|
|
1225 |
int len2 = RecvPacket(m_RecvBuf, 64);
|
|
1226 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
|
1227 |
if (len2 == 0) return KL_ERR;
|
|
1228 |
unsigned char nCmd;
|
|
1229 |
unsigned short nnCount = 0;
|
|
1230 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1231 |
return res;
|
|
1232 |
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
int KLink1::BlinkLED(UCHAR nDst, UCHAR nSecond)
|
|
1236 |
{
|
|
1237 |
int res = KL_OK;
|
|
1238 |
// res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
|
1239 |
m_Dst = nDst;
|
|
1240 |
UCHAR nExpSeq = GetNextSeq();
|
|
1241 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdBlinkLED, nSecond);
|
|
1242 |
SendPacket(m_Packetbuf, len1);
|
|
1243 |
int nByteCount = 0;
|
|
1244 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1245 |
|
|
1246 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1247 |
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1248 |
if (len2 == 0) return KL_ERR;
|
|
1249 |
unsigned char nCmd;
|
|
1250 |
unsigned short nnCount = 0;
|
|
1251 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1252 |
return res;
|
|
1253 |
|
|
1254 |
}
|
|
1255 |
|
|
1256 |
int KLink1::ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
1257 |
{
|
|
1258 |
int res = KL_OK;
|
|
1259 |
m_Dst = nDst;
|
|
1260 |
UCHAR nExpSeq = GetNextSeq();
|
|
1261 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadProgram, nType, nWordAddr, nWordCount);
|
|
1262 |
SendPacket(m_Packetbuf, len1);
|
|
1263 |
int nByteCount = nWordCount;
|
|
1264 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1265 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1266 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1267 |
if (len2 <= 0) return KL_ERR;
|
|
1268 |
unsigned char nCmd;
|
|
1269 |
unsigned short nnCount = 0;
|
|
1270 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1271 |
unsigned char * values2 = (unsigned char *)Values;
|
|
1272 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
1273 |
return res;
|
|
1274 |
|
|
1275 |
}
|
|
1276 |
|
|
1277 |
int KLink1::StartProgram(UCHAR nDst, UCHAR nType)
|
|
1278 |
{
|
|
1279 |
int res = KL_OK;
|
|
1280 |
m_Dst = nDst;
|
|
1281 |
UCHAR nExpSeq = GetNextSeq();
|
|
1282 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType);
|
|
1283 |
SendPacket(m_Packetbuf, len1);
|
|
1284 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
1285 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); }
|
|
1286 |
if (len2 <= 0) return KL_ERR;
|
|
1287 |
unsigned char nCmd;
|
|
1288 |
unsigned short nnCount = 0;
|
|
1289 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1290 |
return res;
|
|
1291 |
|
|
1292 |
}
|
|
1293 |
|
|
1294 |
int KLink1::WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
1295 |
{
|
|
1296 |
int res = KL_OK;
|
|
1297 |
m_Dst = nDst;
|
|
1298 |
UCHAR nExpSeq = GetNextSeq();
|
|
1299 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteProgram, nType, nWordAddr, nWordCount, Values);
|
|
1300 |
|
|
1301 |
SendPacket(m_Packetbuf, len1);
|
|
1302 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
1303 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
1304 |
if (len2 <= 0) return KL_ERR;
|
|
1305 |
unsigned char nCmd;
|
|
1306 |
unsigned short nnCount = 0;
|
|
1307 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1308 |
// Values[0] = m_DataBuf[0];
|
|
1309 |
return res;
|
|
1310 |
|
|
1311 |
}
|
|
1312 |
|
|
1313 |
int KLink1::FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize)
|
|
1314 |
{
|
|
1315 |
int res = KL_OK;
|
|
1316 |
|
|
1317 |
m_Dst = nDst;
|
|
1318 |
UCHAR nExpSeq = GetNextSeq();
|
|
1319 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nStepSize);
|
|
1320 |
SendPacket(m_Packetbuf, len1);
|
|
1321 |
int len2 = RecvPacket(m_RecvBuf, 6);
|
|
1322 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
|
1323 |
if (len2 <= 0) return KL_ERR;
|
|
1324 |
unsigned char nCmd;
|
|
1325 |
unsigned short nnCount = 0;
|
|
1326 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1327 |
return res;
|
|
1328 |
}
|
|
1329 |
|
|
1330 |
|
|
1331 |
int KLink1::ReadRunStat(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
|
1332 |
{
|
|
1333 |
int res = KL_OK;
|
|
1334 |
m_Dst = nDst;
|
|
1335 |
UCHAR nExpSeq = GetNextSeq();
|
|
1336 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadRunStat, nType, nWordAddr, nWordCount);
|
|
1337 |
SendPacket(m_Packetbuf, len1);
|
|
1338 |
int nByteCount = nWordCount;
|
|
1339 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1340 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1341 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1342 |
if (len2 <= 0) return KL_ERR;
|
|
1343 |
unsigned char nCmd;
|
|
1344 |
unsigned short nnCount = 0;
|
|
1345 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1346 |
unsigned char * values2 = (unsigned char *)Values;
|
|
1347 |
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
|
1348 |
return res;
|
|
1349 |
|
|
1350 |
}
|
|
1351 |
|
|
1352 |
int KLink1::ClearStatistics(UCHAR nDst, UCHAR nType)
|
|
1353 |
{
|
|
1354 |
int res = KL_OK;
|
|
1355 |
m_Dst = nDst;
|
|
1356 |
UCHAR nExpSeq = GetNextSeq();
|
|
1357 |
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdClearRunStat, nType);
|
|
1358 |
SendPacket(m_Packetbuf, len1);
|
|
1359 |
int nByteCount = 0;
|
|
1360 |
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
|
1361 |
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
|
1362 |
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
|
1363 |
if (len2 <= 0) return KL_ERR;
|
|
1364 |
unsigned char nCmd;
|
|
1365 |
unsigned short nnCount = 0;
|
|
1366 |
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
1367 |
return res;
|
|
1368 |
|
|
1369 |
}
|