From 61deef5cdf96cbfdd6ad45be49e80d597c00ca65 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期二, 24 十二月 2024 08:37:21 +0800 Subject: [PATCH] 2024-12-24 --- KLink1/KLink.cpp | 483 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 458 insertions(+), 25 deletions(-) diff --git a/KLink1/KLink.cpp b/KLink1/KLink.cpp index cb0eed8..3bb6e14 100644 --- a/KLink1/KLink.cpp +++ b/KLink1/KLink.cpp @@ -136,6 +136,13 @@ // 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的状态还原 @@ -190,6 +197,7 @@ if (r == MySerPort1.R_OK) { m_bOpened = true; + // GotoCmdMode(); // MyKLink1.Open(); // m_static_connect.SetCtlColor(RGB(0, 255, 0)); return KL_OK; @@ -206,10 +214,12 @@ } */ m_bOpened = true; +// return KL_OK; }; int KLink1::Close(int CloseParam) { + ExitCmdMode(); if (m_bCloseCallBackSet) { m_CallBackFuncs.CloseFunc(); @@ -217,6 +227,33 @@ m_bOpened = false; return KL_OK; }; + +int KLink1::GotoCmdMode(int Param) +{ + int res = KL_OK; + int len1 = 3; + m_Packetbuf[0] = '+'; + m_Packetbuf[1] = '+'; + m_Packetbuf[2] = '+'; + + SendPacket(m_Packetbuf, len1); + + return res; +} + +int KLink1::ExitCmdMode(int Param) +{ + int res = KL_OK; + int len1 = 3; + m_Packetbuf[0] = '-'; + m_Packetbuf[1] = '-'; + m_Packetbuf[2] = '-'; + + SendPacket(m_Packetbuf, len1); + + return res; +} + int KLink1::Connect(int ConnectParam) { int OpenParam = ConnectParam; @@ -224,9 +261,11 @@ unsigned short len1; unsigned short buf1[256]; //m_resultStr = MySerPort1.m_strResult; + GotoCmdMode(); return res; if (res == KL_OK) { + GotoCmdMode(); int res2 = GetInfo(m_Dst, &len1, buf1); if (m_bOnConnectCallBackSet) { m_CallBackFuncs.OnConnect(); } @@ -236,6 +275,7 @@ } int KLink1::DisConnect(int DisConnectParam) { + ExitCmdMode(); if (m_bOnDisConnectCallBackSet) { m_CallBackFuncs.OnDisConnect(DisConnectParam); } return Close(); } @@ -272,6 +312,7 @@ { if (m_bOpened && m_bSendCallBackSet) { + m_nTotalSendCount++; if (m_bClearBufCallBackSet) m_CallBackFuncs.ClearBufFunc(); return m_CallBackFuncs.SendPkgFunc(pBuf, Len); } @@ -296,6 +337,7 @@ } if (len2 >= LenToRead) { m_nContinueErrCount = 0; + m_nTotalRecvCount++; return len2; } else if (len2 <= 0) { @@ -308,6 +350,7 @@ return KL_ERR; } else { + m_nTotalRecvCount++; return len2; } } @@ -342,7 +385,7 @@ pPacket->Cmd = nCMD; pPacket->Type1 = Type; int Datalen = 0; - int nWordAddr; + int nByteAddr; int nWordCount; switch (nCMD) @@ -500,6 +543,23 @@ Datalen = 0; PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; + case KLCmdReadPLCAnno: + Datalen = 0; + PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; + break; + case KLCmdStartPLCAnno: + Datalen = 0; + PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; + break; + case KLCmdWritePLCAnno: + Datalen = nCount; + memcpy(pPacket->Params + 4, pData, Datalen); + PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; + break; + case KLCmdFinishPLCAnno: + Datalen = 0; + PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; + break; case KLCmdReadRunStat: Datalen = 0; @@ -558,11 +618,7 @@ break; default: - pPacket->Params[0] = nAddr % 256; - pPacket->Params[1] = nAddr / 256; - pPacket->Params[2] = nCount % 256; - pPacket->Params[3] = nCount / 256; - Datalen = 0; + Datalen = nCount; memcpy(pPacket->Params + 4, pData, Datalen); PkgLen1 = sizeof(stKLReqPacket) + 4 + Datalen; break; @@ -583,6 +639,45 @@ return PkgLen1; } +int KLink1::MakeRemoteReqPacketEx(void* pBuf, UCHAR nDst, UCHAR Stat, UCHAR nPort, UCHAR nChildId, UCHAR nReqId, UCHAR Param1, 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 = KLCmdPortRemoteReq; + pPacket->Type1 = nReqId; + + pKLStat pStat = (pKLStat) & (pPacket->Stat); + int Datalen = nCount; + int nWordAddr = nAddr; + int nWordCount = nCount; + pPacket->Params[0] = nPort; + pPacket->Params[1] = nChildId; + pPacket->Params[2] = Param1; + pPacket->Params[3] = nAddr % 256; + pPacket->Params[4] = nAddr / 256; + pPacket->Params[5] = nCount % 256; +// pPacket->Params[6] = nCount / 256; + memcpy(&pPacket->Params[6], pData, Datalen); + PkgLen1 = sizeof(stKLReqPacket) + 6 + Datalen; + + 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) { @@ -640,6 +735,7 @@ m_nRSeq = ((pKLStat)&nStatus)->nSEQ; int Datalen = pPacket->nSize1; *nCount = Datalen; + USHORT ErrCode; switch (*nCmd) { case KLCmdNone: @@ -694,7 +790,10 @@ case KLCmdSaveToFlash: break; - + case KLCmdGetMode: + *nCount = Datalen; + memcpy(pData, pPacket->Datas, Datalen); + break; case KLCmdReadProgram: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); @@ -706,6 +805,19 @@ *nCount = Datalen; break; case KLCmdFinishProgram: + *nCount = Datalen; + break; + case KLCmdReadPLCAnno: + *nCount = Datalen; + memcpy(pData, pPacket->Datas, Datalen); + break; + case KLCmdStartPLCAnno: + *nCount = Datalen; + break; + case KLCmdWritePLCAnno: + *nCount = Datalen; + break; + case KLCmdFinishPLCAnno: *nCount = Datalen; break; @@ -728,10 +840,23 @@ *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); break; - - case KLCmdErrRply: + case KLCmdGetPortInfo: *nCount = Datalen; memcpy(pData, pPacket->Datas, Datalen); + break; + case KLCmdGetPortChildInfo: + *nCount = Datalen; + memcpy(pData, pPacket->Datas, Datalen); + break; + case KLCmdGetPortChnInfo: + *nCount = Datalen; + memcpy(pData, pPacket->Datas, Datalen); + break; + case KLCmdErrRply: + ErrCode = Datalen + 1000; + *nCount = 0; + return ErrCode; + //memcpy(pData, pPacket->Datas, Datalen); break; default: *nCount = Datalen; @@ -1086,12 +1211,12 @@ } -int KLink1::ReadSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * nReadBytes, USHORT * Values) +int KLink1::ReadSysCfgData(UCHAR nDst, UCHAR nType, 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); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadSysCfg, nType, nStartAddr, nByteCount, Values); CString s1; for (int i = 0; i < len1; i++) { s1.AppendFormat(_T("%02X "), m_Packetbuf[i]); @@ -1110,13 +1235,12 @@ return res; return KL_OK; } -int KLink1::WriteSysCfgData(UCHAR nDst, USHORT nStartAddr, UCHAR nByteCount, USHORT * Values) +int KLink1::WriteSysCfgData(UCHAR nDst, UCHAR nType, 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); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteSysCfg, nType, nStartAddr, nByteCount, Values); SendPacket(m_Packetbuf, len1); int nReadBytes = 0; int numToRead = sizeof(stKLRplyPktHdr) + nReadBytes; @@ -1187,7 +1311,7 @@ return res; } -int KLink1::GetMode(UCHAR nDst, UCHAR nType) +int KLink1::GetMode(UCHAR nDst, UCHAR nType, UCHAR * value) { int res = KL_OK; // res = ReadDataByte(nDst, 4, KLDataTypeSDT, 36, (UCHAR *)pValue); @@ -1201,6 +1325,7 @@ 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; } @@ -1245,7 +1370,7 @@ } -int KLink1::ReadProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values) +int KLink1::ReadPLCProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values) { int res = KL_OK; m_Dst = nDst; @@ -1260,18 +1385,20 @@ 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]; + if (res == KL_OK) { + 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 KLink1::StartDownloadPLCProgram(UCHAR nDst, UCHAR nType, USHORT nProgBytes, USHORT nCRC) { int res = KL_OK; m_Dst = nDst; UCHAR nExpSeq = GetNextSeq(); - int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartProgram, nType, nProgBytes,nCRC); SendPacket(m_Packetbuf, len1); int len2 = RecvPacket(m_RecvBuf, 6); if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6); } @@ -1283,12 +1410,88 @@ } -int KLink1::WriteProgram(UCHAR nDst, UCHAR nType, USHORT nWordAddr, UCHAR nWordCount, USHORT * Values) +int KLink1::DownloadPLCProgram(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, 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); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWriteProgram, nType, nByteAddr, nByteCount, Values); + + SendPacket(m_Packetbuf, len1); + int len2 = RecvPacket(m_RecvBuf, 6, 10); + 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::FinishDownloadPLCProgram(UCHAR nDst, UCHAR nType, USHORT nProgSteps, USHORT nCRC) +{ + int res = KL_OK; + + m_Dst = nDst; + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nProgSteps,nCRC); + SendPacket(m_Packetbuf, len1); + int len2 = RecvPacket(m_RecvBuf, 6,10); + 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::ReadPLCAnno(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR* Values) +{ + int res = KL_OK; + m_Dst = nDst; + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdReadPLCAnno, nType, nByteAddr, nByteCount); + SendPacket(m_Packetbuf, len1); +// int nByteCount = nByteCount; + 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); + if (res == KL_OK) { + unsigned char* values2 = (unsigned char*)Values; + for (int i = 0; i < nnCount; i++) values2[i] = m_DataBuf[i]; + } + return res; + +} + +int KLink1::StartDownloadPLCAnno(UCHAR nDst, UCHAR nType, USHORT nBytes, USHORT nCRC) +{ + int res = KL_OK; + m_Dst = nDst; + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdStartPLCAnno, nType, nBytes, nCRC); + 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::DownloadPLCAnno(UCHAR nDst, UCHAR nType, USHORT nByteAddr, UCHAR nByteCount, UCHAR* Values) +{ + int res = KL_OK; + m_Dst = nDst; + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdWritePLCAnno, nType, nByteAddr, nByteCount, Values); SendPacket(m_Packetbuf, len1); int len2 = RecvPacket(m_RecvBuf, 6); @@ -1302,13 +1505,13 @@ } -int KLink1::FinishProgram(UCHAR nDst, UCHAR nType, USHORT nStepSize) +int KLink1::FinishDownloadPLCAnno(UCHAR nDst, UCHAR nType, USHORT nByteSize, USHORT nCRC) { int res = KL_OK; m_Dst = nDst; UCHAR nExpSeq = GetNextSeq(); - int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishProgram, nType, nStepSize); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdFinishPLCAnno, nType, nByteSize, nCRC); SendPacket(m_Packetbuf, len1); int len2 = RecvPacket(m_RecvBuf, 6); if (len2 <= 0) { len2 = RecvPacket(m_RecvBuf, 6, 30); } @@ -1318,7 +1521,6 @@ 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) { @@ -1359,3 +1561,234 @@ return res; } + +int KLink1::ResetDevice(UCHAR nDst, UCHAR nType) +{ + m_Dst = nDst; + m_resultStr.Empty(); + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdResetMachine, nType, 0, 0, 0); + 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_TIMEOUT; + unsigned char nCmd; + unsigned short nnCount = 0; + int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); + return res; +} + +int KLink1::WriteFirmware(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, KLCmdWriteFirmware, nType, nByteAddr , 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); } + m_resultStr.Format(_T("S %d, R %d "), len1, len2); + if (len2 <= 0) return KL_TIMEOUT; + m_resultStr.Append(_T("\r\n")); + for (int j = 0; j < len2; j++) { + m_resultStr.AppendFormat(_T("%02X "), m_RecvBuf[j]); + } + unsigned char nCmd; + unsigned short nnCount = 0; + int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); + return res; +} + +int KLink1::WriteFirmInfo(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, KLCmdWriteFirmInfo, nType, nByteAddr , 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_TIMEOUT; + unsigned char nCmd; + unsigned short nnCount = 0; + int res = ParseRplyPacket(m_RecvBuf, len2, &nCmd, &m_DstStat.StatByte, &nnCount, m_DataBuf); + return res; +} + +int KLink1::GetPortInfo(UCHAR nDst, UCHAR nType, UCHAR nByteCount, USHORT* nByteRead, USHORT* Values) +{ + m_Dst = nDst; + m_resultStr.Empty(); + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetPortInfo, nType, 0, 0); + 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) + { + memcpy(Values, m_DataBuf, nnCount); + } + 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::GetPortChildInfo(UCHAR nDst, UCHAR nType, UCHAR nChildId, UCHAR nByteCount, USHORT* nByteRead, USHORT* Values) +{ + m_Dst = nDst; + m_resultStr.Empty(); + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetPortChildInfo, nType, nChildId, 0); + 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) + { + memcpy(Values, m_DataBuf, nnCount); + } + 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::GetPortChnInfo(UCHAR nDst, UCHAR nType, UCHAR nChnId, UCHAR nByteCount, USHORT* nByteRead, USHORT* Values) +{ + m_Dst = nDst; + m_resultStr.Empty(); + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, KLCmdGetPortChnInfo, nType, nChnId, 0); + 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) + { + memcpy(Values, m_DataBuf, nnCount); + } + 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::RunRemoteReq(UCHAR nDst, UCHAR nPort, UCHAR nChnId, UCHAR nReqId, UCHAR nParam,USHORT nByteAddr, UCHAR nByteCount, void * pData) +{ + m_Dst = nDst; + m_resultStr.Empty(); + UCHAR nExpSeq = GetNextSeq(); + int len1 = MakeRemoteReqPacketEx(m_Packetbuf, nDst, m_Stat1.StatByte, nPort, nChnId, nReqId,nParam,nByteAddr,nByteCount,pData); + SendPacket(m_Packetbuf, len1); + int numToRead = sizeof(stKLRplyPktHdr) + 0; + 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) + { + } + 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 KLink1::RmoteBlinkLED(UCHAR nDst, UCHAR nPort, UCHAR nChnId, UCHAR nSecond) +{ + return RunRemoteReq(nDst, nPort, nChnId, ReqBlinkLED, nSecond,0,0,0); +} + + + + -- Gitblit v1.9.1