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