QuakeGod
2021-11-30 0ed43835e6bf40ba4d31fb6b8dc0d8400162b90a
提交 | 用户 | age
4b03ae 1 #pragma once
Q 2
3 class KLinkProtocol
4 {
5     typedef unsigned char UCHAR;
6     typedef unsigned short USHORT;
7     typedef unsigned int UINT;
8
9 public:
10     KLinkProtocol();
11     virtual ~KLinkProtocol();
0ed438 12
Q 13     enum enKLCoilTypes
4b03ae 14     {
0ed438 15         KLCoilTypeX = 0,        //X Input
Q 16         KLCoilTypeY = 1,        //Y Output
17         KLCoilTypeR = 2,        //R register
18         KLCoilTypeLX = 3,        //Link register
19         KLCoilTypeLY = 4,        //Link register
20         KLCoilTypeT = 5,        //Timer
21         KLCoilTypeC = 5,        //Counter
22         KLCoilTypeLR = 6,        //Link register
4b03ae 23
Q 24     };
0ed438 25     enum enKLDataTypes
4b03ae 26     {
0ed438 27         KLDataTypeWX = 0,
Q 28         KLDataTypeWY = 1,
29         KLDataTypeWR = 2,
30         KLDataTypeWLX = 3,
31         KLDataTypeWLY = 4,
32         KLDataTypeDT = 5,
33         KLDataTypeSDT = 6,
34         KLDataTypeSV = 7,
35         KLDataTypeEV = 8,
36         KLDataTypeLD = 9,
4b03ae 37
0ed438 38         KLDataSysCfg = 25,
Q 39         KLDataTypeFlash = 33,
40
41         KLDataTypeTest = 254,
4b03ae 42     };
0ed438 43     enum enKLDataCounts
4b03ae 44     {
0ed438 45         KLCoilXCount = 256,
Q 46         KLCoilYCount = 256,
47         KLCoilRCount = 256,
48         KLCoilTCount = 64,
49         KLCoilCCount = 64,
50         KLCoilLCount = 128,
4b03ae 51
0ed438 52         KLDataDTCount = 256,
Q 53         KLDataSDTCount = 256,
54         KLDataWXCount = 16,
55         KLDataWYCount = 16,
56         KLDataWRCount = 16,
57         KLDataSVCount = 64,
58         KLDataEVCount = 64,
59         KLDataLDCount = 64,
60         KLDataWLCount = 8,
4b03ae 61     };
0ed438 62     typedef struct tagKLTypeName
4b03ae 63     {
0ed438 64         int nType;
4b03ae 65         const char * sName;
0ed438 66         int nRange = 0;
Q 67     }stKLTypeName;
68     static const stKLTypeName KLCoilTypeNames[];
69     static const stKLTypeName KLDataTypeNames[];
4b03ae 70
0ed438 71 enum eKLResult
Q 72 {
73     KL_ERR = -1,
74     KL_OK = 0,
75     KL_NG = 1,
76     KL_UNKNOWN = 2,
77     KL_TIMEOUT = 3,
78     KL_BCC_ERR = 4,
79     KL_PKG_ERR = 5,
80     KL_SEQ_ERR = 6,
81     KL_LENTH_EXCEED,
82     KL_NEED_PASS,
83     KL_NOT_SUPPORT,
84 };
85 const char * GetErrDescStr(int nErrNo);
86 enum
87 {
88     KLSignStart = '%',
89     KLSignReply = 'U',
90     KLSignEnd = 0x0D,
91     KLMaxPacketLength = 128,
4b03ae 92
0ed438 93 };
Q 94 enum ExtraDataType
95 {
96     KLExtTypeNone = 0,
97     KLExtTypeText = 1,
98     KLExtTypeCmd = 2,
99     KLExtTypePassTh = 3,
100
101     KLExtTypeTestData = 8,
102 };
103
104 enum enCMDs
105 {
106     //        cmdOK = '1',        //Reply OK
107     //        cmdNG = '2',        //Rplay NG
108     KLCmdNone = 0x00,        //Nothing
109     KLCmdPing = 0x01,            //Ping
110     KLCmdPingReply = 0x02,        //PingReply
111     KLCmdGetInfo = 0x03,            //GetInfo
112     KLCmdVerInfo = 0x04,            //InfoReply
113
114     KLCmdRead = 0x05,            //Read
115     KLCmdReadReply = 0x06,        //ReadReply
116     KLCmdWrite = 0x07,            //Write
117     KLCmdWriteReply = 0x08,    //WriteReply
118     KLCmdSaveToFlash = 0x09,                //Write
119
120     KLCmdExChgData = 0x0A,                //ExChangeDate, = Wirte + Read
121     KLCmdExChgDataReply,            //ExchangeData Reply
122
123     KLCmdSyncRead = 0x0D,                //SyncRead
124     KLCmdSyncWrite,                        //SyncWrite
125     KLCmdSequenRead,                    //Sequence Read
126     KLCmdSyncTime,                        //SyncTime
127
128     KLCmdChgMode = 0x11,
129     KLCmdGetMode = 0x12,
130
131     KLCmdChkPass,
132     KLCmdSetPass,
133     KLCmdEraseAll,
134     KLCmdReadProgram,
135     KLCmdWriteProgram,
136
137     KLCmdRead1Bit = 0x21,                //ReadSingleBit
138     KLCmdWrite1Bit = 0x22,                //WriteSingleBit
139
140     KLCmdReadBits = 0x23,                //ReadBits        n = 1 - 8
141     KLCmdWriteBits = 0x24,                //ReadBits        n = 1 - 8
142
143     KLCmdReadBitsByByte = 0x25,            //ReadBitsByByte    ,byteCount
144     KLCmdWriteBitsByByte = 0x26,        //ReadBitsByByte    ,ByteCount
145
146     KLCmdReadBitsByWord = 0x27,            //ReadBitsByWord    ,WordCount
147     KLCmdWriteBitsByWord = 0x28,        //ReadBitsByWord    ,WordCount
148
149     KLCmdReadDataByte = 0x31,            //ByteCount
150     KLCmdWriteDataByte = 0x32,
151
152     KLCmdReadDataWord = 0x33,            //WordCount
153     KLCmdWriteDataWord = 0x34,
154
155     KLCmdReadData = 0x35,                //?Count
156     KLCmdWriteData = 0x36,
157
158     KLCmdReadProg = 0x51,
159     KLCmdWriteProg,
160     KLCmdReadSysCfg,
161     KLCmdWriteSysCfg,
162     KLCmdSaveSysCfg,
163     KLCmdSaveRunStat,
164
165     KLCmdEraseFlashPage = 0x61,
166     KLCmdWriteToFlash = 0x62,
167
168     KLCmdGetEventLogCount,
169     KLCmdGetEventLog,
170     KLCmdClearEventLog,
171     KLCmdGetFactoryData,
172     KLCmdWriteFactoryData,
173     KLCmdResetMachine,
174     KLCmdGetUid,
175     KLCmdSetTime,
176
177     KLCmdMC,
178     KLCmdMD,
179     KLCmdMG,
180
181     KLCmdErrRply = 0xEE,                //ERRORReply
182 };
183
184 enum enKeventType
185 {
186     EventTypeNone = 0,
187     EventTypePowerUp = 1,
188     EventTypePowerDown = 2,
189     EventTypePowerRecover = 3,
190     EventTypeConnected = 4,
191     EventTypeLostCon = 5,
192     EventTypeSetTime = 6,
193     EventTypeSysCfg = 7,
194     EventTypeProg = 8,
195     EventTypeForce = 9,
196     EventTypeClearEvent = 10,
197
198     EventType
199 };
200
201 typedef struct tagEventLog
202 {
203     unsigned short Sign1;
204     unsigned short Seq1;
205     unsigned int nTime;
206     unsigned short nType;
207     unsigned short nParam1;
208     unsigned int nParam2;
209 }stEventLog, *pEventLog;
4b03ae 210
Q 211
0ed438 212 typedef struct tagKLCmdName
Q 213 {
214     const int nCmd;
215     const char * sName;
216     const char * sType;
4b03ae 217
0ed438 218 }KLCmdName;
4b03ae 219
Q 220 const KLCmdName KLCmdNames[42] =
221 {
222     {KLCmdNone,"None",""},    //==0    //Nothing
223     {KLCmdPing,"Ping"},    //=1        //Ping
224     {KLCmdPingReply,"PingReply"}, //2        //PingReply
225     {KLCmdGetInfo,"GetInfo"},//3            //GetInfo
226     {KLCmdVerInfo,"VerInfo"},// = 0x04,            //InfoReply
227
228     {KLCmdRead,"Read","WX;WY;DT;SDT;EV;SV;LD;WL;CFG;FLASH;RAM"},// = 0x05,            //Read
229     {KLCmdReadReply,"ReadReply"},// = 0x06,        //ReadReply
230     {KLCmdWrite,"Write"},// = 0x07,            //Write
231     {KLCmdWriteReply,"WriteReply"},// = 0x08,    //WriteReply
232     {KLCmdSaveToFlash,"SaveToFlash"},// = 0x09,                //Write
233
234     {KLCmdExChgData,"ExChgData"},// = 0x0A,                //ExChangeDate, = Wirte + Read
235     {KLCmdExChgDataReply,"ExChgDataReply"},//,            //ExchangeData Reply
236
237     {    KLCmdSyncRead,"SyncRead"},// = 0x0D,                //SyncRead
238     {    KLCmdSyncWrite,    "SyncWrite"},//                     //SyncWrite
239     {    KLCmdSequenRead,"SequenRead"},//                     //Sequence Read
240     {    KLCmdSyncTime,"SyncTime"},//                         //SyncTime
241
242     {    KLCmdGetMode,"GetMode"},//  = 0x12,
243
244     {    KLCmdChkPass,"ChkPass"},// 
245     {    KLCmdSetPass,"SetPass"},// 
246     {    KLCmdEraseAll,"EraseAll"},// 
247     {    KLCmdReadProgram,"ReadProgram"},// 
248     {    KLCmdWriteProgram,"WriteProgram"},// 
249
250     {    KLCmdRead1Bit,"Read1Bit"},//  = 0x21,                //ReadSingleBit
251     {    KLCmdWrite1Bit,"Write1Bit"},//  = 0x22,                //WriteSingleBit
252
253     {    KLCmdReadBits,"ReadBits"},//  = 0x23,                //ReadBits        n = 1 - 8
254     {    KLCmdWriteBits,"WriteBits"},//  = 0x24,                //ReadBits        n = 1 - 8
255
256     {    KLCmdReadBitsByByte,"ReadBitsByByte"},//  = 0x25,            //ReadBitsByByte
257     {    KLCmdWriteBitsByByte,"WriteBitsByByte"},//  = 0x26,        //ReadBitsByByte
258
259     {    KLCmdReadBitsByWord,"ReadBitsByWord"},//  = 0x27,            //ReadBitsByWord
260     {    KLCmdWriteBitsByWord,"WriteBitsByWord"},//  = 0x28,        //ReadBitsByWord
261
262     {    KLCmdReadDataByte,"ReadDataByte"},//  = 0x31,
263     {    KLCmdWriteDataByte,"WriteDataByte"},//  = 0x32,
264
265     {    KLCmdReadDataWord,"ReadDataWord"},//  = 0x33,
266     {    KLCmdWriteDataWord,"WriteDataWord"},//  = 0x34,
267
268     {    KLCmdReadData,"ReadData"},//  = 0x35,
269     {    KLCmdWriteData,"WriteData"},//  = 0x36,
270
271     {    KLCmdReadProg,"ReadProg"},//  = 0x51,
272     {    KLCmdWriteProg,"WriteProg"},// 
273     {    KLCmdReadSysCfg,"ReadSysCfg"},// 
274     {    KLCmdWriteSysCfg,"WriteSysCfg"},// 
275
276     {    KLCmdSaveSysCfg,"SaveSysCfg"},// 
277
278     {    KLCmdErrRply,"ErrRply"}//  = 0xEE,                //ERRORReply
279 };
280
0ed438 281 typedef struct tagKLExtDataFrame
Q 282 {
283     UCHAR Dst;
284     UCHAR nType;
285     UCHAR Len2;
286     UCHAR Data[1];
287 }*pKLExtDataFrame;
4b03ae 288
Q 289
290 typedef union tagKLStatDef
291 {
292     UCHAR StatByte;
293     struct {
294         UCHAR nSEQ : 4;
295         UCHAR HasExt : 1;
296         UCHAR HasErr : 1;
297     };
298 }unKLStat, *pKLStat;
299
300 int m_nSeq = 0;
301 int m_nRSeq = 0;
302 unsigned char m_Dst;
0ed438 303
4b03ae 304 //unsigned char m_Stat;
0ed438 305
4b03ae 306 unKLStat m_Stat1 = { 0 };
Q 307 unKLStat m_DstStat;
308 typedef struct tagKLReqPacketDef
309 {
310
311     UCHAR StartSign;
312     UCHAR DstAddr;
313     UCHAR Stat;
314     UCHAR Cmd;
315     UCHAR Type1;
316     UCHAR Params[1];
317 }stKLReqPacket, * pKLReqPacket;
318
319 typedef struct tagKLRplyPktHdr
320 {
321     unsigned char RplyStSgn;    //
322     unsigned char DstAddr;        //
323     unsigned char nStatus;        //
324     unsigned char nRplyCMD;        //
325     unsigned char nSize1;            //
326     unsigned char Datas[1];        //
327 }stKLRplyPktHdr, *pKLRplyPktHdr;
328
329 unsigned char m_Packetbuf[256];
330 unsigned char m_SendBuf[1024];
331 unsigned char m_RecvBuf[1024];
332 unsigned char m_DataBuf[1024];
333
334 typedef struct tagChnStat
335 {
336     unsigned int Stat;
337     unsigned int SendPackets;
338     unsigned int RecvPackets;
339     unsigned int LastSentTimeuS;
340     unsigned int LostPackets;
341     unsigned int CtnLstPkts;
342     unsigned int MaxCtnLstPkts;
343     unsigned int NotPkgErr;
344     unsigned int PkgLenErr;
345     unsigned int BCCErr;
346     unsigned int TimeOutErr;
347     unsigned int Delay;
348     unsigned int MaxDelay;
349     unsigned int SendTimeInterval;
350     union
351     {
352         unsigned int ClientDatas[10];
353         struct {
354             unsigned int ClientRecvPkts;    //
355             unsigned int ClientSendPkts;    //
356             unsigned int ClientNotPktErr;    //
357             unsigned int ClientMisIdPkts;    //
358             unsigned int ClientPkgLenErr;    //
359             unsigned int ClientBccErr;        //
360             unsigned int ClientTimeOutErr;    //
361         };
362     };
363 } stChnStat, * pChnStat;
364
365 struct tagMEM
366 {
367     union //tagWX
368     {
369         USHORT WX[64];
370         UCHAR WXB[128];
371         UINT WXD[32];
372     };
373     union //tagWY
374     {
375         USHORT WY[64];
376         UCHAR WYB[128];
377         UINT WYD[32];
378     };
379     union //tagDT
380     {
381         USHORT DT[256];
382         UCHAR DTB[512];
383         UINT DTD[128];
384     };
385     union //tagWR
386     {
387         USHORT WR[64];
388         UCHAR WRB[128];
389         UINT WRD[32];
390     };
391     union //tagSDT
392     {
393         UCHAR SDB[512];
394         USHORT SDT[256];
395         UINT SDD[128];
396         struct {
397             unsigned int EffJumperSW;
398             unsigned int CurJumperSW;
399             unsigned int haltick;
400             unsigned int RunStat;
401             unsigned int ErrStat;
402             unsigned int nTickCount;
403             unsigned int TotalRunTime;
404             unsigned int ThisRunTime;
405             unsigned int PwrOnCount;
406             unsigned int LastScanTime;
407             unsigned int ScanTimeuS;
408             unsigned int MinScanTimeuS;
409             unsigned int MaxScanTimeuS;
410             unsigned short ADCValues[20];
411         };
412     };
413 }MEM;
414
415 CString m_resultStr;
416
417 static int xtoi(const char * hexstr, int len = 0);
418 unsigned char KLBCC(void * pData, int nSize);
419
420 int CalCRC(void * pBuf, int nSize);
421
0ed438 422 /*
Q 423     连接状态
424     开始连接(未定)-> 心跳包正常->已连接 -> 心跳包连续正常 -> 连接很好
425
426     连接很好 -> 心跳包 失败 / 数据包失败 -> 已连接 (有错误) 
427
428     已连接 (有错误) -> 心跳包正常 -> 已连接 
429
430
431
432 */
433
4b03ae 434 int ParseReqPacket();
Q 435 int MakeRplyPacket();
436
437 typedef int(*pSend)(void *, int);
438 typedef int(*pRecv)(void *, int);
0ed438 439 typedef int(*pOpen)(int);
Q 440 typedef int(*pClose)(int);
4b03ae 441
Q 442 int m_bSendCallBackSet = 0;
443 int m_bRecvCallBackSet = 0;
0ed438 444 int m_bOpenCallBackSet = 0;
Q 445 int m_bCloseCallBackSet = 0;
446
4b03ae 447 pSend SendPkgFunc;
Q 448 pRecv RecvPkgFunc;
0ed438 449 pOpen OpenFunc;
Q 450 pClose CloseFunc;
451
452 bool m_bOpened=false;
453
4b03ae 454 int SetSendCallBackFunc(pSend pSendFunc);
Q 455 int SetRecvCallBackFunc(pRecv pRecvFunc);
0ed438 456 int SetOpenCallBackFunc(pOpen pOpenFunc);
Q 457 int SetCloseCallBackFunc(pClose pCloseFunc);
458
459 int m_nErrCount = 0;
460 int m_nContinueErrCount = 0;
461 int Open(int OpenParam=0)
462 {
463     if (m_bOpenCallBackSet)
464     {
465         OpenFunc(OpenParam);
466     }
467     m_bOpened = true;
468     return KL_OK;
469 };
470 int Close(int CloseParam=0)
471 {
472     if (m_bCloseCallBackSet)
473     {
474         CloseFunc(CloseParam);
475     }
476     m_bOpened = false;
477     return KL_OK;
478 };
4b03ae 479 int SendPacket(void * pBuf, int Len)
Q 480 {
0ed438 481     if (m_bOpened && m_bSendCallBackSet) return SendPkgFunc(pBuf, Len);
4b03ae 482     else return KL_ERR;
Q 483 }
484
485 int RecvPacket(void * pBuf, int LenToRead)
486 {
487     
0ed438 488     if (m_bOpened && m_bRecvCallBackSet)
4b03ae 489     {
Q 490         int len2 = 0;
491         char * pBufByte = (char *)pBuf;
492         for (int i = 0; i < 3; i++)
493         {
494             int len1= RecvPkgFunc(pBufByte + len2, LenToRead - len2);
495             len2 += len1;
496             if (len2 >= LenToRead) break;
497         }
0ed438 498         if (len2 >= LenToRead) {
Q 499             m_nContinueErrCount = 0;
500             return len2;
501         }
502         else {
503             m_nErrCount++;
504             m_nContinueErrCount++;
505             if (m_nContinueErrCount > 10)
506             {
507             //    Close(-1);
508             }
509             return KL_ERR;
510         }
4b03ae 511     }
0ed438 512     else
Q 513     {
514         return KL_ERR;
515     }
4b03ae 516 }
Q 517 int GetNextSeq();
518
519 int MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData);
520
0ed438 521 //int MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr = 0, USHORT nCount = 0, void * pData = NULL);
4b03ae 522
Q 523 int MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr = 0, USHORT nCount = 0, void * pData = NULL, int ExtFrameLen = 0, void * pExtDataFrame = NULL);
524
525 int CheckPackage(void * pBuf, UINT nSize);
526 int ProcessPacket(void *pBuf, int nLen);
527 int ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData);
528
3b04f9 529 int GetInfo(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
4b03ae 530 int GetSN(UCHAR nDst, int * nCount);
Q 531 int GetUID(UCHAR nDst, int * nCount);
3b04f9 532 //int GetTime32(UCHAR nDst, int * nCount);
Q 533 //int SetTime32(UCHAR nDst, int * nCount);
534 int GetDateTime32(UCHAR nDst, UINT * pValue);
535 int SetDateTime32(UCHAR nDst, UINT Value);
4b03ae 536
Q 537
538 int GetEventLogCount(UCHAR nDst, int * nCount);
539 int GetEventLog(UCHAR nDst, int nStartIndex, int nCount , stEventLog * pEventLogs);
540
541
542 int ReadBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR * Value);//Read 1 Single bit
543 int WriteBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR Value);//Write 1 Single bit
3b04f9 544 int ReadBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values);
Q 545 int WriteBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values);
546 int ReadBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values);
547 int WriteBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values);
4b03ae 548
3b04f9 549 int ReadDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values);
Q 550 int WriteDataByte(UCHAR nDst, UCHAR nByteCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values);
4b03ae 551
3b04f9 552 int ReadDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
Q 553 int WriteDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
4b03ae 554
3b04f9 555 int ReadData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
Q 556 int WriteData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values);
4b03ae 557
Q 558 };
559