#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<len2;i++)
|
{
|
s1.AppendFormat(_T("%02X "),m_RecvBuf[i]);
|
}
|
s1.Append(_T("\r\n"));
|
m_resultStr.Format(_T("ToRead %d R= %d \r\n"),numToRead,len2);
|
m_resultStr += s1;
|
}
|
unsigned char nCmd ;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
if (res == KL_OK)
|
{
|
for (int i = 0; i < nnCount; i++)
|
{ Values[i] = m_DataBuf[i]; }
|
}
|
else
|
{
|
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
CString s1;
|
s1= GetErrDescStr(res);
|
m_resultStr += s1;
|
return res;
|
}
|
|
return KL_OK;
|
}
|
int KLink::WriteDataByte(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, KLCmdWriteDataByte, nType, nByteAddr/2, nByteCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
nByteCount = 0;
|
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
// if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
}
|
|
int KLink::ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nWordCount*2);
|
SendPacket(m_Packetbuf, len1);
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount*2;
|
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (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 < len2; i++)
|
{
|
s1.AppendFormat(_T("%02X "), m_RecvBuf[i]);
|
}
|
s1.Append(_T("\r\n"));
|
m_resultStr.Format(_T("ToRead %d R= %d \r\n"), numToRead, len2);
|
m_resultStr += s1;
|
}
|
unsigned char nCmd;
|
unsigned short nnCount = 0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
if (res == KL_OK)
|
{
|
for (int i = 0; i < nnCount / 2; i++)
|
{
|
// Values[0] = m_DataBuf[0];
|
Values[i] = m_DataBuf[i * 2] + (m_DataBuf[i * 2 + 1] << 8);
|
}
|
}
|
else
|
{
|
m_resultStr.AppendFormat(_T("Res=%d ToRead %d Count %d "), res, numToRead, len2);
|
CString s1;
|
s1 = GetErrDescStr(res);
|
m_resultStr += s1;
|
return res;
|
}
|
return KL_OK;
|
}
|
int KLink::WriteDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nWordCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
nWordCount = 0;
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
Values[0] = m_DataBuf[0];
|
return res;
|
}
|
|
int KLink::ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr, nWordCount);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
for (int i=0;i<nnCount;i++) Values[i] = m_DataBuf[i];
|
return res;
|
|
return KL_OK;
|
}
|
int KLink::WriteData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nWordCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
}
|
int KLink::GetInfo(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nWordCount, Values);
|
CString s1;
|
for (int i = 0; i < len1; i++) {
|
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
}
|
m_resultStr = s1;
|
SendPacket(m_Packetbuf, len1);
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
}
|
int KLink::GetSN(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetSN, nType, nWordAddr, nWordCount, Values);
|
CString s1;
|
for (int i = 0; i < len1; i++) {
|
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
}
|
m_resultStr = s1;
|
SendPacket(m_Packetbuf, len1);
|
nWordCount = 4;
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount = 0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
return 0;
|
}
|
int KLink::GetUID(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetUid, nType, nWordAddr, nWordCount, Values);
|
CString s1;
|
for (int i = 0; i < len1; i++) {
|
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
}
|
m_resultStr = s1;
|
SendPacket(m_Packetbuf, len1);
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount = 0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
return 0;
|
}
|
|
int KLink::GetFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetFactoryData, nType, nWordAddr, nWordCount, Values);
|
CString s1;
|
for (int i = 0; i < len1; i++) {
|
s1.AppendFormat(_T("%02X "), m_Packetbuf[i]);
|
}
|
m_resultStr = s1;
|
SendPacket(m_Packetbuf, len1);
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount = 0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, Values);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
return KL_OK;
|
}
|
int KLink::WriteFactoryData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteFactoryData, nType, nWordAddr, nWordCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
nWordCount = 0;
|
int numToRead = sizeof(stKLRplyPktHdr) + nWordCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount = 0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
}
|
|
int KLink::GetEventLogCount(UCHAR nDst, int * nCount)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLogCount, 0);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
nCount[0] = *(int *)m_DataBuf;
|
return res;
|
|
return KL_OK;
|
}
|
|
int KLink::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetEventLog, 0, nStartIndex, nCount, 0);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
|
memcpy(theEventLogs, m_DataBuf, nCount * sizeof(stEventLog));
|
|
return res;
|
|
return KL_OK;
|
}
|
|
int KLink::GetDateTime32(UCHAR nDst, UINT * pValue)
|
{
|
int res = KL_OK;
|
res = ReadDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)pValue);
|
return res;
|
|
}
|
|
int KLink::SetDateTime32(UCHAR nDst, UINT Value)
|
{
|
int res = KL_OK;
|
res = WriteDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)&Value);
|
return res;
|
}
|
|
int KLink::GetMode(UCHAR nDst, UCHAR nType)
|
{
|
int res = KL_OK;
|
// res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue);
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetMode, nType);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
|
}
|
|
|
int KLink::ChangeMode(UCHAR nDst, UCHAR nType)
|
{
|
int res = KL_OK;
|
// res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value);
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdChgMode, nType);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
if (len2 == 0) { len2 = RecvPacket(m_RecvBuf, 64); }
|
if (len2 == 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
|
}
|
|
|
|
int KLink::ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
int res = KL_OK;
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadProgram, nType, nWordAddr, nWordCount);
|
SendPacket(m_Packetbuf, len1);
|
int nByteCount = nWordCount;
|
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
unsigned char * values2 = (unsigned char *)Values;
|
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
return res;
|
|
}
|
|
int KLink::StartProgram(UCHAR nDst, UCHAR nType)
|
{
|
int res = KL_OK;
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 6);
|
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
|
}
|
int KLink::WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
int res = KL_OK;
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteProgram, nType, nWordAddr, nWordCount, Values);
|
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 6);
|
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
}
|
int KLink::FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize)
|
{
|
int res = KL_OK;
|
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nStepSize);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 6);
|
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
}
|
|
int KLink::ReadRunStat(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values)
|
{
|
int res = KL_OK;
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadRunStat, nType, nWordAddr, nWordCount);
|
SendPacket(m_Packetbuf, len1);
|
int nByteCount = nWordCount;
|
int numToRead = sizeof(stKLRplyPktHdr) + nByteCount;
|
int len2 = RecvPacket(m_RecvBuf, numToRead);
|
if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, numToRead); }
|
if (len2 <= 0) return KL_ERR;
|
unsigned char nCmd;
|
unsigned short nnCount=0;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
unsigned char * values2 = (unsigned char *)Values;
|
for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i];
|
return res;
|
|
}
|