#include "pch.h"
|
#include "KLink1.h"
|
#include "KLink.h"
|
#include <afxdialogex.h>
|
#include "CDialogCommSet1.h"
|
|
KLink1::KLink1()
|
{
|
|
}
|
|
KLink1::~KLink1()
|
{
|
|
}
|
|
static const KLink1::stKLTypeName KLCoilTypeNames[] =
|
{
|
{KLink1::KLCoilTypeX,"X",KLink1::KLCoilXCount},
|
{KLink1::KLCoilTypeY,"Y",KLink1::KLCoilYCount},
|
{KLink1::KLCoilTypeR,"R",KLink1::KLCoilRCount},
|
{KLink1::KLCoilTypeLX,"LX",KLink1::KLCoilLXCount},
|
{KLink1::KLCoilTypeLY,"LY",KLink1::KLCoilLYCount},
|
{KLink1::KLCoilTypeT,"T",KLink1::KLCoilTCount},
|
{KLink1::KLCoilTypeC,"C",KLink1::KLCoilCCount}
|
};
|
|
static const KLink1::stKLTypeName KLDataTypeNames[] =
|
{
|
|
KLink1::KLDataTypeWX,"WX", KLink1::KLDataWXCount,
|
KLink1::KLDataTypeWY,"WY", KLink1::KLDataWYCount,
|
KLink1::KLDataTypeWR,"WR",KLink1::KLDataWRCount,
|
KLink1::KLDataTypeWLX,"WLX",KLink1::KLDataWLCount,
|
KLink1::KLDataTypeWLY,"WLY",KLink1::KLDataWLCount,
|
KLink1::KLDataTypeDT,"DT",KLink1::KLDataDTCount,
|
KLink1::KLDataTypeSDT,"SDT",KLink1::KLDataSDTCount,
|
KLink1::KLDataTypeSV,"SV",KLink1::KLDataSVCount ,
|
KLink1::KLDataTypeEV,"EV",KLink1::KLDataEVCount ,
|
KLink1::KLDataTypeLD,"LD",KLink1::KLDataLDCount,
|
KLink1::KLDataSysCfg,"Cfg",128,
|
KLink1::KLDataTypeFlash,"Flash",256,
|
KLink1::KLDataTypeTest,"Test",256
|
};
|
|
const char * KLink1::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 KLink1::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 KLink1::KLBCC(void * pData, int nSize)
|
{
|
unsigned char k = 0;
|
for (int i = 0; i < nSize; i++)
|
{
|
k ^= ((unsigned char *)pData)[i];
|
}
|
return k;
|
}
|
int KLink1::CalCRC(void * pBuf, int nSize)
|
{
|
return 0;
|
}
|
|
int KLink1::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->OnConnect) { m_CallBackFuncs.OnConnect = pFuncSt->OnConnect; m_bOnConnectCallBackSet = 1; }
|
if (pFuncSt->OnDisConnect) { m_CallBackFuncs.OnDisConnect = pFuncSt->OnDisConnect; m_bOnDisConnectCallBackSet = 1; }
|
if (pFuncSt->OnError) { m_CallBackFuncs.OnError = pFuncSt->OnError; m_bOnErrorCallBackSet = 1; }
|
|
// if (pFuncSt->SendPkgFunc) { m_CallBackFuncs.SendPkgFunc = pFuncSt->SendPkgFunc; m_bSendCallBackSet = 1; }
|
|
return KL_OK;
|
}
|
|
|
int KLink1::SetCommParam()
|
{
|
AFX_MANAGE_STATE(AfxGetStaticModuleState());
|
/*
|
//·½·¨2µÄ״̬±ä¸ü
|
HINSTANCE save_hInstance = AfxGetResourceHandle();
|
|
AfxSetResourceHandle(theApp.m_hInstance);
|
|
// CDialog dlg(IDD_DLL_DIALOG);//´ò¿ªIDΪ2000µÄ¶Ô»°¿ò
|
// dlg.DoModal();
|
*/
|
CDialogCommSet1 dialog1;
|
if (m_bCommParamSet)
|
{
|
dialog1.m_nComNum = m_nPort;
|
dialog1.m_nBaudRate = m_nBaudRate;
|
dialog1.m_Settings = m_Settings;
|
dialog1.m_bOpened = m_bOpened;
|
}
|
INT_PTR r = dialog1.DoModal();
|
//·½·¨2µÄ״̬»¹Ô
|
|
// AfxSetResourceHandle(save_hInstance);
|
if (r == IDOK)
|
{
|
m_nPort = dialog1.m_nComNum;
|
m_nBaudRate = dialog1.m_nBaudRate;
|
m_Settings = dialog1.m_Settings;
|
m_bCommParamSet = true;
|
}
|
else
|
{
|
return -1;
|
}
|
|
return 0;
|
}
|
int KLink1::fnTest1(int n)
|
{
|
AFX_MANAGE_STATE(AfxGetStaticModuleState());
|
/*
|
//·½·¨2µÄ״̬±ä¸ü
|
HINSTANCE save_hInstance = AfxGetResourceHandle();
|
|
AfxSetResourceHandle(theApp.m_hInstance);
|
|
// CDialog dlg(IDD_DLL_DIALOG);//´ò¿ªIDΪ2000µÄ¶Ô»°¿ò
|
// dlg.DoModal();
|
*/
|
CDialogCommSet1 dialog1;
|
INT_PTR r = dialog1.DoModal();
|
//·½·¨2µÄ״̬»¹Ô
|
|
// AfxSetResourceHandle(save_hInstance);
|
|
return 0;
|
}
|
|
int KLink1::Open(int OpenParam)
|
{
|
CString s1;
|
if (!m_bCommParamSet)
|
{
|
if (SetCommParam()) return -1;
|
m_bCommParamSet = 1;
|
}
|
MySerPort1.SetParams(m_nPort, m_nBaudRate, _T("8,N,1"));
|
int r = MySerPort1.Open();
|
s1.Format(_T("Open %s = %d"), MySerPort1.m_strResult, r);
|
m_resultStr = s1; // MySerPort1.m_strResult;
|
if (r == MySerPort1.R_OK)
|
{
|
m_bOpened = true;
|
// MyKLink1.Open();
|
// m_static_connect.SetCtlColor(RGB(0, 255, 0));
|
return KL_OK;
|
}
|
else
|
{
|
m_bOpened = false;
|
return r;
|
}
|
/*
|
if (m_bOpenCallBackSet)
|
{
|
m_CallBackFuncs.OpenFunc();
|
}
|
*/
|
m_bOpened = true;
|
return KL_OK;
|
};
|
int KLink1::Close(int CloseParam)
|
{
|
if (m_bCloseCallBackSet)
|
{
|
m_CallBackFuncs.CloseFunc();
|
}
|
m_bOpened = false;
|
return KL_OK;
|
};
|
int KLink1::Connect(int ConnectParam)
|
{
|
int OpenParam = ConnectParam;
|
int res=Open(OpenParam);
|
unsigned short len1;
|
unsigned short buf1[256];
|
//m_resultStr = MySerPort1.m_strResult;
|
return res;
|
if (res == KL_OK)
|
{
|
int res2 = GetInfo(m_Dst, &len1, buf1);
|
|
if (m_bOnConnectCallBackSet) { m_CallBackFuncs.OnConnect(); }
|
return res;
|
}
|
return res;
|
}
|
int KLink1::DisConnect(int DisConnectParam)
|
{
|
if (m_bOnDisConnectCallBackSet) { m_CallBackFuncs.OnDisConnect(DisConnectParam); }
|
return Close();
|
}
|
|
int KLink1::CalSpeed()
|
{
|
double thistime = MySerPort1.GetTimemS();
|
double diftime = thistime - LastTime;
|
if (diftime >= 500)
|
{
|
m_SendSpeed = (DWORD)(m_nSendCount * 1000 / diftime);
|
m_RecvSpeed = static_cast<DWORD> (m_nRecvCount * 1000 / diftime);
|
m_nLastSendCount = m_nSendCount; m_nSendCount = 0;
|
m_nLastRecvCount = m_nRecvCount; m_nRecvCount = 0;
|
LastTime = thistime;
|
}
|
return 0;
|
}
|
|
int KLink1::DoHeartBeat(int nParam)
|
{
|
unsigned short nByteCount;
|
unsigned short buf1[256];
|
int res = GetInfo(m_Dst, &nByteCount, buf1);
|
return KL_OK;
|
}
|
|
void KLink1::OnTimer()
|
{
|
DoHeartBeat();
|
}
|
|
int KLink1::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 KLink1::RecvPacket(void * pBuf, int LenToRead, int ReTryCount)
|
{
|
|
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 KLink1::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 KLink1::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 KLink1::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 KLCmdWriteSysCfg:
|
Datalen = nCount;
|
memcpy(pPacket->Params + 4, pData, Datalen);
|
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 KLink1::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 KLink1::ProcessPacket(void *pBuf, int nLen)
|
{
|
return false;
|
}
|
|
int KLink1::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 KLCmdReadSysCfg:
|
*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 KLink1::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 KLink1::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 KLink1::ReadBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
{
|
return KL_OK;
|
}
|
int KLink1::WriteBits(UCHAR nDst, UCHAR nType, USHORT nBitAddr, UCHAR nBitCount, UCHAR * Values)
|
{
|
return KL_OK;
|
}
|
int KLink1::ReadBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
{
|
return KL_OK;
|
}
|
int KLink1::WriteBitsByWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT *Values)
|
{
|
return KL_OK;
|
}
|
int KLink1::GetNextSeq()
|
{
|
m_nSeq++;
|
if (m_nSeq >= 8) m_nSeq = 0;
|
m_Stat1.nSEQ = m_nSeq;
|
return m_nSeq;
|
}
|
|
int KLink1::ReadDataByte(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, USHORT * nByteRead, 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;
|
}
|
*nByteRead = nnCount;
|
return KL_OK;
|
}
|
|
int KLink1::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 KLink1::ReadDataWord(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * nByteRead, 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;
|
}
|
*nByteRead = nnCount;
|
return KL_OK;
|
}
|
|
int KLink1::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 KLink1::ReadData(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * nByteRead, 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];
|
*nByteRead = nnCount;
|
return res;
|
|
return KL_OK;
|
}
|
int KLink1::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 KLink1::GetInfo(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, 0, 0, 0, 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) + 32;
|
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];
|
*nReadBytes = nnCount;
|
return res;
|
|
return KL_OK;
|
}
|
|
int KLink1::GetSN(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetSN, 0, 0, 4, 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 ByteCount = 4;
|
int numToRead = sizeof(stKLRplyPktHdr) + ByteCount;
|
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];
|
*nReadBytes = nnCount;
|
return res;
|
|
return KL_OK;
|
return 0;
|
}
|
int KLink1::GetUID(UCHAR nDst, USHORT * nReadBytes, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetUid, 0, 0, 12, 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) + 12;
|
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];
|
*nReadBytes = nnCount;
|
return res;
|
|
return KL_OK;
|
return 0;
|
}
|
|
int KLink1::ReadFactoryData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * nReadBytes, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetFactoryData, 0, nStartAddr, nByteCount, 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) + 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, Values);
|
// Values[0] = m_DataBuf[0];
|
*nReadBytes = nnCount;
|
return res;
|
return KL_OK;
|
}
|
int KLink1::WriteFactoryData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * Values)
|
{
|
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteFactoryData, 0, nStartAddr, nByteCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
int nReadBytes = 0;
|
int numToRead = sizeof(stKLRplyPktHdr) + nReadBytes;
|
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 KLink1::ReadSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * nReadBytes, USHORT * Values)
|
{
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadSysCfg, 0, nStartAddr, nByteCount, 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) + 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, Values);
|
// Values[0] = m_DataBuf[0];
|
*nReadBytes = nnCount;
|
return res;
|
return KL_OK;
|
}
|
int KLink1::WriteSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * Values)
|
{
|
|
m_Dst = nDst;
|
m_resultStr.Empty();
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteSysCfg, 0, nStartAddr, nByteCount, Values);
|
SendPacket(m_Packetbuf, len1);
|
int nReadBytes = 0;
|
int numToRead = sizeof(stKLRplyPktHdr) + nReadBytes;
|
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 KLink1::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 KLink1::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 KLink1::GetDateTime32(UCHAR nDst, UINT * pValue)
|
{
|
int res = KL_OK;
|
unsigned short nCount;
|
res = ReadDataByte(nDst, KLDataTypeSDT, 36, 4, &nCount, (UCHAR *)pValue);
|
return res;
|
|
}
|
|
int KLink1::SetDateTime32(UCHAR nDst, UINT Value)
|
{
|
int res = KL_OK;
|
res = WriteDataByte(nDst, KLDataTypeSDT, 36, 4, (UCHAR *)&Value);
|
return res;
|
}
|
|
int KLink1::GetMode(UCHAR nDst, UCHAR nType, UCHAR * value)
|
{
|
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);
|
if (nnCount > 0) { *value = m_DataBuf[0]; }
|
return res;
|
|
}
|
|
|
int KLink1::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 KLink1::BlinkLED(UCHAR nDst, UCHAR nSecond)
|
{
|
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, KLCmdBlinkLED, nSecond);
|
SendPacket(m_Packetbuf, len1);
|
int 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;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
|
}
|
|
int KLink1::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 KLink1::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 KLink1::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 KLink1::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 KLink1::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;
|
|
}
|
|
int KLink1::ClearStatistics(UCHAR nDst, UCHAR nType)
|
{
|
int res = KL_OK;
|
m_Dst = nDst;
|
UCHAR nExpSeq = GetNextSeq();
|
int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdClearRunStat, nType);
|
SendPacket(m_Packetbuf, len1);
|
int 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;
|
res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf);
|
return res;
|
|
}
|