/* */ #pragma once #include "KDefine.h" #include "HvSerialPort.h" #include class KLink { typedef unsigned char UCHAR; typedef unsigned short USHORT; typedef unsigned int UINT; public: KLink(); virtual ~KLink(); HvSerialPort m_SerialPort; #define TYPECOIL 0x00 #define TYPEDATA 0x80 enum enKLCoilTypes { KLCoilTypeX = 0 | TYPECOIL, //X Input KLCoilTypeY = 1 | TYPECOIL, //Y Output KLCoilTypeR = 2 | TYPECOIL, //R register KLCoilTypeLX = 3 | TYPECOIL, //Link register KLCoilTypeLY = 4 | TYPECOIL, //Link register KLCoilTypeT = 5 | TYPECOIL, //Timer KLCoilTypeC = 6 | TYPECOIL, //Counter KLCoilTypeLR = 7 | TYPECOIL, //Link register KLCoilTypeSR = 8 | TYPECOIL, //Link register }; enum enKLDataTypes { KLDataTypeDEC = 0 | TYPEDATA, KLDataTypeHEX = 1 | TYPEDATA, KLDataTypeFloat = 2 | TYPEDATA, KLDataTypeWX = 3 | TYPEDATA, KLDataTypeWY = 4 | TYPEDATA, KLDataTypeWR = 5 | TYPEDATA, KLDataTypeWLX = 6 | TYPEDATA, KLDataTypeWLY = 7 | TYPEDATA, KLDataTypeDT = 8 | TYPEDATA, KLDataTypeSDT = 9 | TYPEDATA, KLDataTypeWSR = 10 | TYPEDATA, KLDataTypeSV = 11 | TYPEDATA, KLDataTypeEV = 12 | TYPEDATA, KLDataTypeLD = 13 | TYPEDATA, KLDataSysCfg = 25 | TYPEDATA, KLDataTypeFlash = 33 | TYPEDATA, KLDataTypeTest = 254 | TYPEDATA, }; enum enKLDataCounts { KLDataDTCount = 256, KLDataSDTCount = 256, KLDataWXCount = 16, KLDataWYCount = 16, KLDataWRCount = 16, KLDataLDCount = 64, KLDataWLCount = 8, KLCoilXCount = KLDataWXCount * 16, KLCoilYCount = KLDataWYCount * 16, KLCoilRCount = KLDataWRCount * 16, KLCoilTCount = 64, KLCoilCCount = KLCoilTCount, KLDataSVCount = KLCoilTCount, KLDataEVCount = KLCoilTCount, KLCoilLXCount = 128, KLCoilLYCount = 128, KLCoilLRCount = 128, KLCoilSRCount = 128, }; typedef struct tagKLTypeName { int nType; const char * sName; int nRange = 0; }stKLTypeName; static const stKLTypeName KLCoilTypeNames[]; static const stKLTypeName KLDataTypeNames[]; enum eKLResult { KL_ERR = -1, KL_OK = 0, KL_NG = 1, KL_UNKNOWN = 2, KL_TIMEOUT = 3, KL_BCC_ERR = 4, KL_PKG_ERR = 5, KL_SEQ_ERR = 6, KL_LENTH_EXCEED, KL_NEED_PASS, KL_NOT_SUPPORT, }; const char * GetErrDescStr(int nErrNo); enum { KLSignStart = '%', KLSignReply = 'U', KLSignEnd = 0x0D, KLMaxPacketLength = 128, }; enum ExtraDataType { KLExtTypeNone = 0, KLExtTypeText = 1, KLExtTypeCmd = 2, KLExtTypePassTh = 3, KLExtTypeTestData = 8, }; enum enCMDs { // cmdOK = '1', //Reply OK // cmdNG = '2', //Rplay NG KLCmdNone = 0x00, //Nothing KLCmdPing = 0x01, //Ping KLCmdPingReply = 0x02, //PingReply KLCmdGetInfo = 0x03, //GetInfo KLCmdVerInfo = 0x04, //InfoReply KLCmdRead = 0x05, //Read KLCmdReadReply = 0x06, //ReadReply KLCmdWrite = 0x07, //Write KLCmdWriteReply = 0x08, //WriteReply KLCmdSaveToFlash = 0x09, //Write KLCmdExChgData = 0x0A, //ExChangeDate, = Wirte + Read KLCmdExChgDataReply, //ExchangeData Reply KLCmdSyncRead = 0x0D, //SyncRead KLCmdSyncWrite, //SyncWrite KLCmdSequenRead, //Sequence Read KLCmdSyncTime, //SyncTime KLCmdChgMode = 0x11, KLCmdGetMode = 0x12, KLCmdChkPass, KLCmdSetPass, KLCmdEraseAll, KLCmdReadProgram, KLCmdStartProgram, KLCmdWriteProgram, KLCmdFinishProgram, KLCmdRead1Bit = 0x21, //ReadSingleBit KLCmdWrite1Bit = 0x22, //WriteSingleBit KLCmdReadBits = 0x23, //ReadBits n = 1 - 8 KLCmdWriteBits = 0x24, //ReadBits n = 1 - 8 KLCmdReadBitsByByte = 0x25, //ReadBitsByByte ,byteCount KLCmdWriteBitsByByte = 0x26, //ReadBitsByByte ,ByteCount KLCmdReadBitsByWord = 0x27, //ReadBitsByWord ,WordCount KLCmdWriteBitsByWord = 0x28, //ReadBitsByWord ,WordCount KLCmdReadDataByte = 0x31, //ByteCount KLCmdWriteDataByte = 0x32, KLCmdReadDataWord = 0x33, //WordCount KLCmdWriteDataWord = 0x34, KLCmdReadData = 0x35, //?Count KLCmdWriteData = 0x36, KLCmdReadProg = 0x51, KLCmdWriteProg, KLCmdReadSysCfg, KLCmdWriteSysCfg, KLCmdSaveSysCfg, KLCmdSaveRunStat, KLCmdReadRunStat, KLCmdEraseFlashPage = 0x61, KLCmdWriteToFlash = 0x62, KLCmdGetEventLogCount, KLCmdGetEventLog, KLCmdClearEventLog, KLCmdGetFactoryData, KLCmdWriteFactoryData, KLCmdResetMachine, KLCmdGetUid, KLCmdSetTime, KLCmdGetSN, KLCmdMC = 0x70, KLCmdMD, KLCmdMG, KLCmdErrRply = 0xEE, //ERRORReply }; typedef struct tagRunStat { unsigned short Sign1; unsigned short Seq1; volatile unsigned short PowerCount; // volatile unsigned short Reserved1; volatile unsigned int UpTime; //Seconds; volatile unsigned int UserData1; volatile unsigned short WorkMode; volatile unsigned short WorkMode2; volatile unsigned short nBinProgBank; volatile unsigned short nBinProgSize; unsigned int Reserved2[1]; unsigned short CRC1; unsigned short EndSign1; }stRunStat, *pRunStat; stRunStat KMRunStat; enum enKeventType { EventTypeNone = 0, EventTypePowerUp = 1, EventTypePowerDown = 2, EventTypePowerRecover = 3, EventTypeConnected = 4, EventTypeLostCon = 5, EventTypeSetTime = 6, EventTypeSysCfg = 7, EventTypeProg = 8, EventTypeForce = 9, EventTypeClearEvent = 10, EventType }; typedef struct tagEventLog { unsigned short Sign1; unsigned short Seq1; unsigned int nTime; unsigned short nType; unsigned short nParam1; unsigned int nParam2; }stEventLog, *pEventLog; typedef struct tagKLCmdName { const int nCmd; const char * sName; const char * sType; }KLCmdName; const KLCmdName KLCmdNames[42] = { {KLCmdNone,"None",""}, //==0 //Nothing {KLCmdPing,"Ping"}, //=1 //Ping {KLCmdPingReply,"PingReply"}, //2 //PingReply {KLCmdGetInfo,"GetInfo"},//3 //GetInfo {KLCmdVerInfo,"VerInfo"},// = 0x04, //InfoReply {KLCmdRead,"Read","WX;WY;DT;SDT;EV;SV;LD;WL;CFG;FLASH;RAM"},// = 0x05, //Read {KLCmdReadReply,"ReadReply"},// = 0x06, //ReadReply {KLCmdWrite,"Write"},// = 0x07, //Write {KLCmdWriteReply,"WriteReply"},// = 0x08, //WriteReply {KLCmdSaveToFlash,"SaveToFlash"},// = 0x09, //Write {KLCmdExChgData,"ExChgData"},// = 0x0A, //ExChangeDate, = Wirte + Read {KLCmdExChgDataReply,"ExChgDataReply"},//, //ExchangeData Reply { KLCmdSyncRead,"SyncRead"},// = 0x0D, //SyncRead { KLCmdSyncWrite, "SyncWrite"},// //SyncWrite { KLCmdSequenRead,"SequenRead"},// //Sequence Read { KLCmdSyncTime,"SyncTime"},// //SyncTime { KLCmdGetMode,"GetMode"},// = 0x12, { KLCmdChkPass,"ChkPass"},// { KLCmdSetPass,"SetPass"},// { KLCmdEraseAll,"EraseAll"},// { KLCmdReadProgram,"ReadProgram"},// { KLCmdWriteProgram,"WriteProgram"},// { KLCmdRead1Bit,"Read1Bit"},// = 0x21, //ReadSingleBit { KLCmdWrite1Bit,"Write1Bit"},// = 0x22, //WriteSingleBit { KLCmdReadBits,"ReadBits"},// = 0x23, //ReadBits n = 1 - 8 { KLCmdWriteBits,"WriteBits"},// = 0x24, //ReadBits n = 1 - 8 { KLCmdReadBitsByByte,"ReadBitsByByte"},// = 0x25, //ReadBitsByByte { KLCmdWriteBitsByByte,"WriteBitsByByte"},// = 0x26, //ReadBitsByByte { KLCmdReadBitsByWord,"ReadBitsByWord"},// = 0x27, //ReadBitsByWord { KLCmdWriteBitsByWord,"WriteBitsByWord"},// = 0x28, //ReadBitsByWord { KLCmdReadDataByte,"ReadDataByte"},// = 0x31, { KLCmdWriteDataByte,"WriteDataByte"},// = 0x32, { KLCmdReadDataWord,"ReadDataWord"},// = 0x33, { KLCmdWriteDataWord,"WriteDataWord"},// = 0x34, { KLCmdReadData,"ReadData"},// = 0x35, { KLCmdWriteData,"WriteData"},// = 0x36, { KLCmdReadProg,"ReadProg"},// = 0x51, { KLCmdWriteProg,"WriteProg"},// { KLCmdReadSysCfg,"ReadSysCfg"},// { KLCmdWriteSysCfg,"WriteSysCfg"},// { KLCmdSaveSysCfg,"SaveSysCfg"},// // KLCmdReadRunStat, { KLCmdErrRply,"ErrRply"}// = 0xEE, //ERRORReply }; typedef struct tagKLExtDataFrame { UCHAR Dst; UCHAR nType; UCHAR Len2; UCHAR Data[1]; }*pKLExtDataFrame; typedef union tagKLStatDef { UCHAR StatByte; struct { UCHAR nSEQ : 4; UCHAR HasExt : 1; UCHAR HasErr : 1; }; }unKLStat, *pKLStat; int m_nSeq = 0; int m_nRSeq = 0; unsigned char m_Dst; //unsigned char m_Stat; unKLStat m_Stat1 = { 0 }; unKLStat m_DstStat; typedef struct tagKLReqPacketDef { UCHAR StartSign; UCHAR DstAddr; UCHAR Stat; UCHAR Cmd; UCHAR Type1; UCHAR Params[1]; }stKLReqPacket, * pKLReqPacket; typedef struct tagKLRplyPktHdr { unsigned char RplyStSgn; // unsigned char DstAddr; // unsigned char nStatus; // unsigned char nRplyCMD; // unsigned char nSize1; // unsigned char Datas[1]; // }stKLRplyPktHdr, *pKLRplyPktHdr; unsigned char m_Packetbuf[256]; unsigned char m_SendBuf[1024]; unsigned char m_RecvBuf[1024]; unsigned char m_DataBuf[1024]; typedef struct tagChnStat { unsigned int Stat; unsigned int SendPackets; unsigned int RecvPackets; unsigned int LastSentTimeuS; unsigned int LostPackets; unsigned int CtnLstPkts; unsigned int MaxCtnLstPkts; unsigned int NotPkgErr; unsigned int PkgLenErr; unsigned int BCCErr; unsigned int TimeOutErr; unsigned int Delay; unsigned int MaxDelay; unsigned int SendTimeInterval; union { unsigned int ClientDatas[10]; struct { unsigned int ClientRecvPkts; // unsigned int ClientSendPkts; // unsigned int ClientNotPktErr; // unsigned int ClientMisIdPkts; // unsigned int ClientPkgLenErr; // unsigned int ClientBccErr; // unsigned int ClientTimeOutErr; // }; }; } stChnStat, * pChnStat; struct tagMEM { union //tagWX { USHORT WX[64]; UCHAR WXB[128]; UINT WXD[32]; }; union //tagWY { USHORT WY[64]; UCHAR WYB[128]; UINT WYD[32]; }; union //tagDT { USHORT DT[256]; UCHAR DTB[512]; UINT DTD[128]; }; union //tagWR { USHORT WR[64]; UCHAR WRB[128]; UINT WRD[32]; }; union //tagSDT { UCHAR SDB[512]; USHORT SDT[256]; UINT SDD[128]; struct { unsigned int EffJumperSW; unsigned int CurJumperSW; unsigned int haltick; unsigned int RunStat; unsigned int ErrStat; unsigned int nTickCount; unsigned int TotalRunTime; unsigned int ThisRunTime; unsigned int PwrOnCount; unsigned int LastScanTime; unsigned int ScanTimeuS; unsigned int MinScanTimeuS; unsigned int MaxScanTimeuS; unsigned short ADCValues[20]; }; }; }MEM; CString m_resultStr; static int xtoi(const char * hexstr, int len = 0); unsigned char KLBCC(void * pData, int nSize); int CalCRC(void * pBuf, int nSize); /* Á¬½Ó״̬ ¿ªÊ¼Á¬½Ó(䶨)-> ÐÄÌø°üÕý³£->ÒÑÁ¬½Ó -> ÐÄÌø°üÁ¬ÐøÕý³£ -> Á¬½ÓºÜºÃ Á¬½ÓºÜºÃ -> ÐÄÌø°ü ʧ°Ü / Êý¾Ý°üʧ°Ü -> ÒÑÁ¬½Ó (ÓдíÎó) ÒÑÁ¬½Ó (ÓдíÎó) -> ÐÄÌø°üÕý³£ -> ÒÑÁ¬½Ó */ int ParseReqPacket(); int MakeRplyPacket(); typedef int(*pSend)(void *, int); typedef int(*pRecv)(void *, int); typedef int(*pOpen)(int); typedef int(*pClose)(int); // pSend SendPkgFunc; // pRecv RecvPkgFunc; // pOpen OpenFunc; // pClose CloseFunc; typedef struct tagCallBackFuncs { std::function OpenFunc; std::function CloseFunc; std::function ClearBufFunc; std::function SendPkgFunc; std::function RecvPkgFunc; }stCallBackFuncs, *pCallBackFuncs; stCallBackFuncs m_CallBackFuncs; int m_bSendCallBackSet = 0; int m_bRecvCallBackSet = 0; int m_bOpenCallBackSet = 0; int m_bCloseCallBackSet = 0; int m_bClearBufCallBackSet = 0; bool m_bOpened=false; int SetCallBackFuncs(pCallBackFuncs pFuncSt); int m_nErrCount = 0; int m_nContinueErrCount = 0; int Open(int OpenParam=0) { if (m_bOpenCallBackSet) { m_CallBackFuncs.OpenFunc(); } m_bOpened = true; return KL_OK; }; int Close(int CloseParam=0) { if (m_bCloseCallBackSet) { m_CallBackFuncs.CloseFunc(); } m_bOpened = false; return KL_OK; }; int SendPacket(void * pBuf, int Len) { if (m_bOpened && m_bSendCallBackSet) { if (m_bClearBufCallBackSet) m_CallBackFuncs.ClearBufFunc(); return m_CallBackFuncs.SendPkgFunc(pBuf, Len); } else return KL_ERR; } int RecvPacket(void * pBuf, int LenToRead, int ReTryCount=1) { if (m_bOpened && m_bRecvCallBackSet) { int len2 = 0; char * pBufByte = (char *)pBuf; for (int i = 0; i < ReTryCount; i++) { int len1= m_CallBackFuncs.RecvPkgFunc(pBufByte + len2, LenToRead - len2); if (len1 <= 0 || len1 > 256) { continue; } len2 += len1; if (len2 >= LenToRead) break; } if (len2 >= LenToRead) { m_nContinueErrCount = 0; return len2; } else if (len2<=0) { m_nErrCount++; m_nContinueErrCount++; if (m_nContinueErrCount > 10) { // Close(-1); } return KL_ERR; } else { return len2; } } else { return KL_ERR; } } int GetNextSeq(); int MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData); //int MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr = 0, USHORT nCount = 0, void * pData = NULL); 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); int CheckPackage(void * pBuf, int nSize); int ProcessPacket(void *pBuf, int nLen); int ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData); int GetInfo(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int GetSN(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int GetUID(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int GetFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int WriteFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); //int GetTime32(UCHAR nDst, int * nCount); //int SetTime32(UCHAR nDst, int * nCount); int GetDateTime32(UCHAR nDst, UINT * pValue); int SetDateTime32(UCHAR nDst, UINT Value); int GetEventLogCount(UCHAR nDst, int * nCount); int GetEventLog(UCHAR nDst, int nStartIndex, int nCount , stEventLog * pEventLogs); int ReadBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR * Value);//Read 1 Single bit int WriteBit(UCHAR nDst, UCHAR nType,USHORT nBitAddr, UCHAR Value);//Write 1 Single bit int ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values); int WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values); int ReadBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values); int WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values); int ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values); int WriteDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values); int ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int WriteData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int GetMode(UCHAR nDst, UCHAR nType); int ChangeMode(UCHAR nDst, UCHAR nType); int ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int StartProgram(UCHAR nDst, UCHAR nType); int WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); int FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize); int ReadRunStat(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values); };