#include "pch.h"
|
#include "KLinkProtocol.h"
|
|
KLinkProtocol::KLinkProtocol()
|
{
|
|
}
|
|
KLinkProtocol::~KLinkProtocol()
|
{
|
|
}
|
const char * KLinkProtocol::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 KLinkProtocol::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 KLinkProtocol::KLBCC(void * pData, int nSize)
|
{
|
unsigned char k = 0;
|
for (int i = 0; i < nSize; i++)
|
{
|
k ^= ((unsigned char *)pData)[i];
|
}
|
return k;
|
}
|
int KLinkProtocol::CalCRC(void * pBuf, int nSize)
|
{
|
return 0;
|
}
|
int KLinkProtocol::SetSendCallBackFunc(pSend pSendFunc)
|
{
|
SendPkgFunc = pSendFunc;
|
m_bSendCallBackSet = 1;
|
return KL_OK;
|
}
|
int KLinkProtocol::SetRecvCallBackFunc(pRecv pRecvFunc)
|
{
|
RecvPkgFunc = pRecvFunc;
|
m_bRecvCallBackSet = 1;
|
return KL_OK;
|
}
|
|
int KLinkProtocol::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 KLinkProtocol::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;
|
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 KLinkProtocol::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;
|
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 KLCmdRead1Bit:
|
Datalen = 0;
|
pPacket->Params[0] = nAddr % 256;
|
pPacket->Params[1] = nAddr / 256;
|
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
break;
|
case KLCmdWrite1Bit:
|
pPacket->Params[0] = nAddr % 256;
|
pPacket->Params[1] = nAddr / 256;
|
Datalen = 1;
|
memcpy(pPacket->Params + 2, pData, Datalen);
|
PkgLen1 = sizeof(stKLReqPacket) + 2 + Datalen;
|
break;
|
|
case KLCmdReadBits:
|
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 KLCmdWriteBits:
|
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 KLCmdReadDataByte:
|
case KLCmdReadDataWord:
|
case KLCmdReadData:
|
|
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 KLCmdWriteDataByte:
|
case KLCmdWriteDataWord:
|
case KLCmdWriteData:
|
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 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:
|
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 KLinkProtocol::CheckPackage(void * pBuf, UINT 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(pKLRplyPktHdr))
|
{
|
//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 KLinkProtocol::ProcessPacket(void *pBuf, int nLen)
|
{
|
return false;
|
}
|
|
int KLinkProtocol::ParseRplyPacket(void *pBuf, int nPkgLen, UCHAR * nCmd, UCHAR * Status, USHORT* nCount, void * pData)
|
{
|
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(pPacket->Datas, pData, Datalen);
|
break;
|
case KLCmdGetInfo:
|
case KLCmdVerInfo:
|
*nCount = Datalen;
|
memcpy(pPacket->Datas, pData, 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 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:
|
break;
|
}
|
return KL_OK;
|
|
return false;
|
}
|
|
int KLinkProtocol::ReadBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR * Value)
|
{
|
m_Dst = nDst;
|
int len1=MakeReqPacket(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdRead1Bit, nType, nBitAddr);
|
SendPacket(m_Packetbuf, len1);
|
int len2=RecvPacket(m_RecvBuf, 64);
|
unsigned char nCmd;
|
unsigned short nCount;
|
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
Value[0] = m_DataBuf[0];
|
return KL_OK;
|
}
|
int KLinkProtocol::WriteBit(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR Value)
|
{
|
m_Dst = nDst;
|
m_DataBuf[0] = Value;
|
int len1 = MakeReqPacket(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWrite1Bit, nType, nBitAddr,1,m_DataBuf);
|
SendPacket(m_Packetbuf, len1);
|
int len2 = RecvPacket(m_RecvBuf, 64);
|
unsigned char nCmd;
|
unsigned short nCount;
|
ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf);
|
return KL_OK;
|
}
|
int KLinkProtocol::ReadBits(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
{
|
return KL_OK;
|
}
|
int KLinkProtocol::WriteBits(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values)
|
{
|
return KL_OK;
|
}
|
int KLinkProtocol::ReadBitsByWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
{
|
return KL_OK;
|
}
|
int KLinkProtocol::WriteBitsByWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT *Values)
|
{
|
return KL_OK;
|
}
|
int KLinkProtocol::GetNextSeq()
|
{
|
m_nSeq++;
|
if (m_nSeq >= 8) m_nSeq = 0;
|
m_Stat1.nSEQ = m_nSeq;
|
return m_nSeq;
|
}
|
|
int KLinkProtocol::ReadDataByte(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataByte, nType, nByteAddr/2, nCount);
|
SendPacket(m_Packetbuf, len1);
|
int numToRead = sizeof(stKLRplyPktHdr) + nCount;
|
int len2 = 0;
|
int nTryCount = 0;
|
for (int i = 0; i < 2; i++)
|
{
|
nTryCount++;
|
len2 += RecvPacket(m_RecvBuf + len2, numToRead - len2);
|
if (len2 >= numToRead) break;
|
}
|
if (len2 <= 0) 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 Try %d \r\n"),numToRead,len2,nTryCount);
|
m_resultStr += s1;
|
}
|
unsigned char nCmd ;
|
unsigned short nnCount;
|
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);
|
return res;
|
}
|
|
return KL_OK;
|
}
|
int KLinkProtocol::WriteDataByte(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nByteAddr, UCHAR * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataByte, nType, nByteAddr/2, nCount, 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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
}
|
|
int KLinkProtocol::ReadDataWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nCount);
|
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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
Values[0] = m_DataBuf[0];
|
return res;
|
}
|
int KLinkProtocol::WriteDataWord(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, nType, nWordAddr, nCount, 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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
Values[0] = m_DataBuf[0];
|
return res;
|
}
|
|
int KLinkProtocol::ReadData(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadData, nType, nWordAddr,nCount);
|
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;
|
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 KLinkProtocol::WriteData(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteData, nType, nWordAddr, nCount, 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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
}
|
int KLinkProtocol::GetInfo(UCHAR nDst, UCHAR nCount, UCHAR nType, USHORT nWordAddr, USHORT * Values)
|
{
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, nType, nWordAddr, nCount, 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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
// Values[0] = m_DataBuf[0];
|
return res;
|
|
return KL_OK;
|
}
|
int KLinkProtocol::GetEventLogCount(UCHAR nDst, int * nCount)
|
{
|
m_Dst = nDst;
|
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;
|
int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
nCount[0] = *(int *)m_DataBuf;
|
return res;
|
|
return KL_OK;
|
}
|
|
int KLinkProtocol::GetEventLog(UCHAR nDst, int nStartIndex, int nCount, stEventLog * theEventLogs)
|
{
|
m_Dst = nDst;
|
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;
|
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;
|
}
|
|