#include "pch.h" #include "KLink.h" KLink::KLink() { } KLink::~KLink() { } static const KLink::stKLTypeName KLCoilTypeNames[] = { {KLink::KLCoilTypeX,"X",KLink::KLCoilXCount}, {KLink::KLCoilTypeY,"Y",KLink::KLCoilYCount}, {KLink::KLCoilTypeR,"R",KLink::KLCoilRCount}, {KLink::KLCoilTypeLX,"LX",KLink::KLCoilLXCount}, {KLink::KLCoilTypeLY,"LY",KLink::KLCoilLYCount}, {KLink::KLCoilTypeT,"T",KLink::KLCoilTCount}, {KLink::KLCoilTypeC,"C",KLink::KLCoilCCount} }; static const KLink::stKLTypeName KLDataTypeNames[] = { KLink::KLDataTypeWX,"WX", KLink::KLDataWXCount, KLink::KLDataTypeWY,"WY", KLink::KLDataWYCount, KLink::KLDataTypeWR,"WR",KLink::KLDataWRCount, KLink::KLDataTypeWLX,"WLX",KLink::KLDataWLCount, KLink::KLDataTypeWLY,"WLY",KLink::KLDataWLCount, KLink::KLDataTypeDT,"DT",KLink::KLDataDTCount, KLink::KLDataTypeSDT,"SDT",KLink::KLDataSDTCount, KLink::KLDataTypeSV,"SV",KLink::KLDataSVCount , KLink::KLDataTypeEV,"EV",KLink::KLDataEVCount , KLink::KLDataTypeLD,"LD",KLink::KLDataLDCount, KLink::KLDataSysCfg,"Cfg",128, KLink::KLDataTypeFlash,"Flash",256, KLink::KLDataTypeTest,"Test",256 }; const char * KLink::GetErrDescStr(int nErrNo) { switch (nErrNo) { case KL_ERR: return "ERROR"; case KL_OK: //=0 return "OK"; case KL_NG:// = 1, return "NG"; case KL_UNKNOWN: // = 2, return "KL_UNKNOWN"; case KL_TIMEOUT: // = 3, return "KL_TIMEOUT"; case KL_BCC_ERR: // = 4, return "KL_BCC_ERR"; case KL_PKG_ERR: // = 5, return "KL_PKG_ERR"; case KL_SEQ_ERR: // = 6, return "KL_SEQ_ERR"; case KL_LENTH_EXCEED: //, return "KL_LENTH_EXCEED"; case KL_NEED_PASS: //, return "KL_NEED_PASS"; case KL_NOT_SUPPORT://, return "KL_NOT_SUPPORT"; default: return "KL_OTHER_ERR"; break; } } int KLink::xtoi(const char * hexstr, int len) { int len1 = len; if (len <= 0) len1 = (int)strlen(hexstr); int j=0; for (int i = 0; i < len1; i++) { unsigned char ch = hexstr[i]; if (ch >= '0'&&ch <= '9') { int k = ch - '0'; j = j * 16 + k; continue; } if (ch >= 'A'&&ch <= 'F') { int k = ch - 'A' + 10; j = j * 16 + k; continue; } if (ch >= 'a'&&ch <= 'f') { int k = ch - 'a' + 10; j = j * 16 + k; continue; } if (ch == ' ' || ch == ' ') { continue; } break; } return j; } unsigned char KLink::KLBCC(void * pData, int nSize) { unsigned char k = 0; for (int i = 0; i < nSize; i++) { k ^= ((unsigned char *)pData)[i]; } return k; } int KLink::CalCRC(void * pBuf, int nSize) { return 0; } int KLink::SetCallBackFuncs(pCallBackFuncs pFuncSt) { if (pFuncSt->OpenFunc) { m_CallBackFuncs.OpenFunc = pFuncSt->OpenFunc; m_bOpenCallBackSet = 1; } if (pFuncSt->CloseFunc) { m_CallBackFuncs.CloseFunc = pFuncSt->CloseFunc; m_bCloseCallBackSet = 1; } if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; } if (pFuncSt->RecvPkgFunc) { m_CallBackFuncs.RecvPkgFunc = pFuncSt->RecvPkgFunc; m_bRecvCallBackSet = 1; } if (pFuncSt->ClearBufFunc) { m_CallBackFuncs.ClearBufFunc = pFuncSt->ClearBufFunc; m_bClearBufCallBackSet = 1; } // if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; } return KL_OK; } int KLink::MakeExtDataFrame(void * pBuf, UCHAR nDst, UCHAR nType, UCHAR len2, void * pData) { int framelen1 = 0; pKLExtDataFrame pF1 = (pKLExtDataFrame)pBuf; pF1->Dst = nDst; pF1->nType = nType; pF1->Len2 = len2; memcpy(pF1->Data, pData, len2); UCHAR bcc = KLBCC(pBuf, sizeof(tagKLExtDataFrame) + len2 - 1); pF1->Data[len2] = bcc; framelen1 = sizeof(tagKLExtDataFrame) + len2; return framelen1; } /* int KLink::MakeReqPacket(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData) { int PkgLen1 = 0; pKLReqPacket pPacket = (pKLReqPacket)pBuf; pPacket->StartSign = KLSignStart; pPacket->DstAddr = nDst; pPacket->Stat = Stat; pPacket->Cmd = nCMD; pPacket->Type1 = Type; int Datalen = 0; int nWordAddr; int nWordCount; switch (nCMD) { case KLCmdNone: break; case KLCmdPing: Datalen = Type; memcpy(pPacket->Params, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + Datalen; break; case KLCmdGetInfo: break; case KLCmdRead: Datalen = 0; pPacket->Params[0] = nAddr % 256; pPacket->Params[1] = nAddr / 256; pPacket->Params[2] = nCount % 256; pPacket->Params[3] = nCount / 256; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWrite: pPacket->Params[0] = nAddr % 256; pPacket->Params[1] = nAddr / 256; pPacket->Params[2] = nCount % 256; pPacket->Params[3] = nCount / 256; Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdSaveToFlash: break; case KLCmdExChgData: break; case KLCmdGetEventLogCount: pPacket->Params[0] = 0; pPacket->Params[1] = 0; pPacket->Params[2] = 0; pPacket->Params[3] = 0; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; default: break; } if (PkgLen1 >= sizeof(stKLReqPacket)) { UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1); pPacket->Params[PkgLen1-sizeof(stKLReqPacket)] = BCC; } return PkgLen1; } */ int KLink::MakeReqPacketEx(void * pBuf, UCHAR nDst, UCHAR Stat, UCHAR nCMD, UCHAR Type, USHORT nAddr, USHORT nCount, void * pData, int ExtFrameLen, void * pExtDataFrame) { int PkgLen1 = 0; pKLReqPacket pPacket = (pKLReqPacket)pBuf; pPacket->StartSign = KLSignStart; pPacket->DstAddr = nDst; pPacket->Stat = Stat; pPacket->Cmd = nCMD; pPacket->Type1 = Type; pKLStat pStat = (pKLStat)&(pPacket->Stat); int Datalen = 0; int nWordAddr=nAddr; int nWordCount=nCount; pPacket->Params[0] = LOBYTE(nAddr); pPacket->Params[1] = HIBYTE(nAddr); pPacket->Params[2] = nCount % 256; pPacket->Params[3] = nCount / 256; switch (nCMD) { case KLCmdNone: break; case KLCmdPing: Datalen = Type; memcpy(pPacket->Params, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + Datalen; break; case KLCmdGetInfo: case KLCmdGetSN: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdGetFactoryData: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWriteFactoryData: Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdRead: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWrite: Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdRead1Bit: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen; break; case KLCmdWrite1Bit: Datalen = 1; memcpy(pPacket->Params + 2, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen; break; case KLCmdReadBits: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWriteBits: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdReadDataByte: case KLCmdReadDataWord: case KLCmdReadData: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWriteDataByte: case KLCmdWriteDataWord: case KLCmdWriteData: Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdSaveToFlash: break; case KLCmdExChgData: break; case KLCmdReadProgram: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdStartProgram: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdWriteProgram: Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdFinishProgram: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdReadRunStat: Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdSaveSysCfg: pPacket->Params[0] = 0; pPacket->Params[1] = 0; pPacket->Params[2] = 0; pPacket->Params[3] = 0; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdSaveRunStat: pPacket->Params[0] = 0; pPacket->Params[1] = 0; pPacket->Params[2] = 0; pPacket->Params[3] = 0; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdClearEventLog: pPacket->Params[0] = 0; pPacket->Params[1] = 0; pPacket->Params[2] = 0; pPacket->Params[3] = 0; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; case KLCmdGetEventLogCount: pPacket->Params[0] = 0; pPacket->Params[1] = 0; pPacket->Params[2] = 0; pPacket->Params[3] = 0; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; case KLCmdGetEventLog: pPacket->Params[0] = nAddr % 256; pPacket->Params[1] = nAddr / 256; pPacket->Params[2] = nCount % 256; pPacket->Params[3] = nCount / 256; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; default: pPacket->Params[0] = nAddr % 256; pPacket->Params[1] = nAddr / 256; pPacket->Params[2] = nCount % 256; pPacket->Params[3] = nCount / 256; Datalen = 0; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; } if (PkgLen1 >= sizeof(stKLReqPacket)) { UCHAR BCC = KLBCC(pBuf, PkgLen1 - 1); pPacket->Params[PkgLen1 - sizeof(stKLReqPacket)] = BCC; } if (pStat->HasExt && ExtFrameLen > 0 && pExtDataFrame != NULL) { memcpy(pPacket->Params + PkgLen1 - sizeof(stKLReqPacket) + 1, pExtDataFrame, ExtFrameLen); PkgLen1 += ExtFrameLen; }; return PkgLen1; } int KLink::CheckPackage(void * pBuf, int nSize) { pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf; if (p1->RplyStSgn != KLSignReply) { return KL_UNKNOWN; } int DataLen = 0; //p1->LoadLen; if (DataLen > KLMaxPacketLength) { return KL_PKG_ERR; } if (nSize < DataLen + sizeof(stKLRplyPktHdr)) { //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket)); //PutStr(str3,len4); return KL_PKG_ERR; //not long enough } // if (p1->data[DataLen+1] != EndSign) // { // ClientStat[nCurPollId].NoEndErr++; // Uart2Stat.LengthErr++; // return -2; // } unsigned char thisBCC = KLBCC(p1, nSize - 1); if (thisBCC != ((UCHAR *)pBuf)[nSize - 1]) {//BCC Error; return KL_BCC_ERR; } return KL_OK; return true; return false; } int KLink::ProcessPacket(void *pBuf, int nLen) { return false; } int KLink::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData) { if (nPkgLen < 0) { return KL_ERR; } int res = CheckPackage(pBuf, nPkgLen); if (res != KL_OK) return res; int len1 = 0; pKLRplyPktHdr pPacket = (pKLRplyPktHdr)pBuf; UCHAR Sign = pPacket->RplyStSgn; UCHAR nDst= pPacket->DstAddr; *nCmd = pPacket->nRplyCMD; UCHAR nStatus = pPacket->nStatus; *Status = nStatus; m_nRSeq = ((pKLStat)&nStatus)->nSEQ; int Datalen = pPacket->nSize1; *nCount = Datalen; switch (*nCmd) { case KLCmdNone: break; case KLCmdPing: case KLCmdPingReply: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdGetInfo: case KLCmdVerInfo: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdGetUid: case KLCmdGetSN: case KLCmdGetFactoryData: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdWriteFactoryData: *nCount = Datalen; break; case KLCmdRead: case KLCmdReadReply: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdWrite: case KLCmdWriteReply: *nCount = Datalen; break; case KLCmdRead1Bit: case KLCmdReadBits: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdReadDataByte: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdReadDataWord: case KLCmdReadData: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdWriteDataByte: case KLCmdWriteDataWord: case KLCmdWriteData: *nCount = Datalen; break; case KLCmdSaveToFlash: break; case KLCmdReadProgram: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdStartProgram: *nCount =Datalen; break; case KLCmdWriteProgram: *nCount = Datalen; break; case KLCmdFinishProgram: *nCount = Datalen; break; case KLCmdReadRunStat: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdExChgData: case KLCmdExChgDataReply: break; case KLCmdGetEventLogCount: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdGetEventLog: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; case KLCmdErrRply: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; default: *nCount = Datalen; //memcpy(pData, pPacket->Datas, Datalen); break; } return KL_OK; return false; } int KLink::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value) { m_Dst = nDst; int len1=MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr); SendPacket(m_Packetbuf, len1); int numToRead = sizeof(stKLRplyPktHdr) + 1; int len2=RecvPacket(m_RecvBuf, numToRead); unsigned char nCmd; unsigned short nCount=0; ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf); Value[0] = m_DataBuf[0]; return KL_OK; } int KLink::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value) { m_Dst = nDst; m_DataBuf[0] = Value; int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr,1,m_DataBuf); SendPacket(m_Packetbuf, len1); int len2 = RecvPacket(m_RecvBuf, 6); unsigned char nCmd; unsigned short nCount=0; ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf); return KL_OK; } int KLink::ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values) { return KL_OK; } int KLink::WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values) { return KL_OK; } int KLink::ReadBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values) { return KL_OK; } int KLink::WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values) { return KL_OK; } int KLink::GetNextSeq() { m_nSeq++; if (m_nSeq >= 8) m_nSeq = 0; m_Stat1.nSEQ = m_nSeq; return m_nSeq; } int KLink::ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR * Values) { m_Dst = nDst; m_resultStr.Empty(); UCHAR nExpSeq = GetNextSeq(); int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr/2, nByteCount); SendPacket(m_Packetbuf, len1); int numToRead = sizeof(stKLRplyPktHdr) + nByteCount; int len2 = 0; len2= RecvPacket(m_RecvBuf, numToRead); if (len2 <= 0) { m_resultStr.Format(_T("Recv Failed len %d To=%d"),len2,numToRead); return KL_ERR; } if (len2 < numToRead) { CString s1; for (int i=0;i