#include "pch.h" #include "KLink2.h" #include "KLink.h" #include #include "CDialogCommSet1.h" KLink2::KLink2() { } KLink2::~KLink2() { } int KLink2::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; } static const KLink2::stKLTypeName KLCoilTypeNames[] = { {KLink2::KLCoilTypeX,"X",KLink2::KLCoilXCount}, {KLink2::KLCoilTypeY,"Y",KLink2::KLCoilYCount}, {KLink2::KLCoilTypeR,"R",KLink2::KLCoilRCount}, {KLink2::KLCoilTypeLX,"LX",KLink2::KLCoilLXCount}, {KLink2::KLCoilTypeLY,"LY",KLink2::KLCoilLYCount}, {KLink2::KLCoilTypeT,"T",KLink2::KLCoilTCount}, {KLink2::KLCoilTypeC,"C",KLink2::KLCoilCCount} }; static const KLink2::stKLTypeName KLDataTypeNames[] = { KLink2::KLDataTypeWX,"WX", KLink2::KLDataWXCount, KLink2::KLDataTypeWY,"WY", KLink2::KLDataWYCount, KLink2::KLDataTypeWR,"WR",KLink2::KLDataWRCount, KLink2::KLDataTypeWLX,"WLX",KLink2::KLDataWLCount, KLink2::KLDataTypeWLY,"WLY",KLink2::KLDataWLCount, KLink2::KLDataTypeDT,"DT",KLink2::KLDataDTCount, KLink2::KLDataTypeSDT,"SDT",KLink2::KLDataSDTCount, KLink2::KLDataTypeSV,"SV",KLink2::KLDataSVCount , KLink2::KLDataTypeEV,"EV",KLink2::KLDataEVCount , KLink2::KLDataTypeLD,"LD",KLink2::KLDataLDCount, KLink2::KLDataSysCfg,"Cfg",128, KLink2::KLDataTypeFlash,"Flash",256, KLink2::KLDataTypeTest,"Test",256 }; const char * KLink2::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 KLink2::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 KLink2::KLBCC(void * pData, int nSize) { unsigned char k = 0; for (int i = 0; i < nSize; i++) { k ^= ((unsigned char *)pData)[i]; } return k; } int KLink2::CalCRC(void * pBuf, int nSize) { return 0; } int KLink2::SetSendCallBackFunc(std::function pSendFunc) { SendPkgFunc = pSendFunc; m_bSendCallBackSet = 1; return KL_OK; } int KLink2::SetRecvCallBackFunc(std::function pRecvFunc) { RecvPkgFunc = pRecvFunc; m_bRecvCallBackSet = 1; return KL_OK; } int KLink2::SetOpenCallBackFunc(std::function pOpenFunc) { OpenFunc = pOpenFunc; m_bOpenCallBackSet = 1; return KL_OK; } int KLink2::SetCloseCallBackFunc(std::function pCloseFunc) { CloseFunc = pCloseFunc; m_bCloseCallBackSet = 1; return KL_OK; } int KLink2::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 KLink2::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 KLink2::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: 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 KLink2::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 KLink2::ProcessPacket(void *pBuf, int nLen) { return false; } int KLink2::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(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 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 KLink2::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; ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf); Value[0] = m_DataBuf[0]; return KL_OK; } int KLink2::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, 64); unsigned char nCmd; unsigned short nCount; ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nCount, m_DataBuf); return KL_OK; } int KLink2::ReadBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values) { return KL_OK; } int KLink2::WriteBits(UCHAR nDst, UCHAR nBitCount, UCHAR nType, USHORT nBitAddr, UCHAR * Values) { return KL_OK; } int KLink2::ReadBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values) { return KL_OK; } int KLink2::WriteBitsByWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT *Values) { return KL_OK; } int KLink2::GetNextSeq() { m_nSeq++; if (m_nSeq >= 8) m_nSeq = 0; m_Stat1.nSEQ = m_nSeq; return m_nSeq; } int KLink2::ReadDataByte(UCHAR nDst, UCHAR nByteCount, 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, 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; 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 KLink2::WriteDataByte(UCHAR nDst, UCHAR nByteCount, 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, nByteCount, 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 KLink2::ReadDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values) { m_Dst = nDst; UCHAR nExpSeq = GetNextSeq(); int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadDataWord, nType, nWordAddr, nWordCount); 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; int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); 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); } return res; } int KLink2::WriteDataWord(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values) { m_Dst = nDst; UCHAR nExpSeq = GetNextSeq(); int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteDataWord, 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; int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); Values[0] = m_DataBuf[0]; return res; } int KLink2::ReadData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values) { m_Dst = nDst; 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; 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 KLink2::WriteData(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values) { m_Dst = nDst; 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; int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); // Values[0] = m_DataBuf[0]; return res; return KL_OK; } int KLink2::GetInfo(UCHAR nDst, UCHAR nWordCount, UCHAR nType, USHORT nWordAddr, USHORT * Values) { m_Dst = nDst; UCHAR nExpSeq = GetNextSeq(); int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetInfo, 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; int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); // Values[0] = m_DataBuf[0]; return res; return KL_OK; } int KLink2::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 KLink2::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; } int KLink2::GetDateTime32(UCHAR nDst, UINT * pValue) { int res = KL_OK; res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue); return res; } int KLink2::SetDateTime32(UCHAR nDst, UINT Value) { int res = KL_OK; res = WriteDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)&Value); return res; } int KLink2::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; res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); return res; } int KLink2::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; res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); return res; } int KLink2::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; 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 KLink2::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; res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); return res; } int KLink2::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; res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); // Values[0] = m_DataBuf[0]; return res; } int KLink2::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; res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); return res; } int KLink2::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; 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; }