From f4f290279d32d3cd656472a3298345bc7418cd78 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期一, 17 十月 2022 20:42:45 +0800 Subject: [PATCH] arrange again radio fp0 klink knet --- Src/KLink.c | 223 ++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 160 insertions(+), 63 deletions(-) diff --git a/Src/KLink.c b/Src/KLink.c index e5ffe4c..da4fd03 100644 --- a/Src/KLink.c +++ b/Src/KLink.c @@ -24,7 +24,7 @@ int KLThisuS=0; int KLRecvTimeuS=0; -unsigned char KLBCC(void * pData, int nSize) +unsigned char KLBCC(void const * pData, int nSize) { unsigned char k; k=0; @@ -34,17 +34,17 @@ } return k; } -int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,void * pData ) +int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,const void * pData ) { pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf; int PacketLenth=0; p1->RplyStSgn=KLSignReply; - p1->DstAddr=dst; + p1->DstHost=dst; p1->nStatus=Status; p1->nRplyCMD=nCmd; p1->nSize1=DataLen; memcpy(p1->Datas,pData,DataLen); - p1->Datas[DataLen]=BCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1); + p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1); PacketLenth=sizeof(stKLRplyPktHdr)+DataLen; @@ -76,7 +76,7 @@ return PacketLenth; } -int KLCheckPacket(void * pBuf,int nLen1) +int KLCheckPacket(int nChn, void * pBuf, int nLen1) { pKLReqPktHdr p1 = (pKLReqPktHdr) pBuf; if (p1->ReqStSgn != KLSignStart) @@ -111,12 +111,12 @@ inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr) { - (*pW)|=bitMasks[bitAddr&0xf]; + (*pW)|=1<<(bitAddr&0xf); } inline void ResetBit(unsigned short * pW, unsigned char bitAddr) { - (*pW)&=~bitMasks[bitAddr&0xf]; + (*pW)&=~(1<<(bitAddr&0xf)); } static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value) @@ -124,11 +124,12 @@ if (Value) { SetAddrBit(pW, bitAddr);} else {ResetBit(pW, bitAddr);} } -int KLParseReqPacket(void * pBuf,int Len1) + +int KLParseReqPacket(int nChn, void * pBuf, int Len1) { pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf; - int nDstAddr=p1->DstAddr; + int nDstHost=p1->DstHost; KLRecvTimeuS=KLThisuS; int nDataType=p1->nType1; int nAddr=0; @@ -143,7 +144,7 @@ int PacketLen=0; KLRecvTimeuS=KLThisuS; - if (nDstAddr==nKLAddr || nDstAddr==0xff) + if (nDstHost==nKLAddr || nDstHost==0xff) { switch (p1->nCMD) { @@ -153,43 +154,73 @@ DataLen=nDataType; // KLBufferIn[0]=p1->Params[0]; // p1->Params[0]=KLBufferOut[0]; - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params); + SendPacket(nChn, p2, PacketLen); break; // case KLCmdPingReply: // break; case KLCmdInfo: //if (nDataType == KLDataTypeDT) + DataLen= sizeof(KMInfoBlock); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&KMInfoBlock); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdGetUid: + //if (nDataType == KLDataTypeDT) + DataLen= 12; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(uint32_t *)UID_BASE); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdGetSN: + //if (nDataType == KLDataTypeDT) DataLen= 4; - pData=&KMem.nEventCount; - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData); - SendPacket1(p2,PacketLen); - break; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdGetFactoryData: + DataLen= sizeof(stFactoryData); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(pFactoryData)FACTORY_DATA_BASE); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdWriteFactoryData: + nAddr=p1->Params[0]+ (p1->Params[1]<<8); + DataLen= p1->Params[2]; + //DataLen=16; + for (int i=0;i<DataLen;i++) + {KLPacketBuf2[i]=p1->Params[4+i];} + WriteFactoryData(KLPacketBuf2, DataLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdRead: + case KLCmdReadDataWord: case KLCmdReadDataByte: nAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen= p1->Params[2]; if (nDataType ==KLDataTypeDT) { pData=KMem.DT+nAddr; } else if (nDataType == KLDataTypeSDT) { pData=KMem.SDT+nAddr; } + else if (nDataType == KLDataTypeWSR) { pData=KMem.WSR+nAddr; } else if (nDataType == KLDataTypeWX) { pData=KMem.WX+nAddr; } else if (nDataType == KLDataTypeWY) { pData=KMem.WY+nAddr; } else if (nDataType == KLDataTypeWR) { pData=KMem.WR+nAddr; } else if (nDataType == KLDataTypeWLX) { pData=KMem.WLX+nAddr; } else if (nDataType == KLDataTypeWLY) { pData=KMem.WLY+nAddr; } - else if (nDataType == KLDataTypeSV) { pData=KMem.SDT+nAddr; } - else if (nDataType == KLDataTypeEV) { pData=KMem.SDT+nAddr; } + else if (nDataType == KLDataTypeSV) { pData=KMem.SV+nAddr; } + else if (nDataType == KLDataTypeEV) { pData=KMem.EV+nAddr; } else if (nDataType == KLDataTypeTest) { pData=KMem.SDT+nAddr; } - else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&KMSysCfg + nAddr;} + else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg + nAddr;} else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;} else { pData=KLBufferOut+nAddr; } - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); break; // case KLCmdReadReply: // break; case KLCmdWrite: + case KLCmdWriteDataWord: case KLCmdWriteDataByte: //memcpy(DispBuf,p1->data,DataLen); @@ -197,21 +228,22 @@ DataLen= p1->Params[2]; if (nDataType ==KLDataTypeDT) { pData=KMem.DT+nAddr; } else if (nDataType == KLDataTypeSDT) { pData=KMem.SDT+nAddr; } + else if (nDataType == KLDataTypeWSR) { pData=KMem.WSR+nAddr; } else if (nDataType == KLDataTypeWX) { pData=KMem.WX+nAddr; } else if (nDataType == KLDataTypeWY) { pData=KMem.WY+nAddr; } else if (nDataType == KLDataTypeWR) { pData=KMem.WR+nAddr; } else if (nDataType == KLDataTypeWLX) { pData=KMem.WLX+nAddr; } else if (nDataType == KLDataTypeWLY) { pData=KMem.WLY+nAddr; } - else if (nDataType == KLDataTypeSV) { pData=KMem.SDT+nAddr; DataLen=0;} - else if (nDataType == KLDataTypeEV) { pData=KMem.SDT+nAddr; DataLen=0;} + else if (nDataType == KLDataTypeSV) { pData=KMem.SV+nAddr; DataLen=0;} + else if (nDataType == KLDataTypeEV) { pData=KMem.EV+nAddr; DataLen=0;} else if (nDataType == KLDataTypeTest) { pData=KMem.SDT+nAddr; DataLen=0;} - else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&KMSysCfg + nAddr;} + else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg + nAddr;} else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;} else { pData=KLBufferOut+nAddr; DataLen=0; } memcpy(pData,p1->Params+4,DataLen); - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,0,0); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); break; // case KLCmdWriteReply: // break; @@ -219,17 +251,19 @@ case KLCmdRead1Bit: nAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen = 1; - if (nDataType == KLCoilTypeX) { rData[0] = ((KMem.WX[nAddr>>4]&bitMasks[nAddr&0x0f])>0);} - else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nAddr>>4]&bitMasks[nAddr&0x0f])>0);} - else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nAddr>>4]&bitMasks[nAddr&0x0f])>0);} - else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nAddr>>4]&bitMasks[nAddr&0x0f])>0);} - else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nAddr>>4]&bitMasks[nAddr&0x0f])>0);} - else if (nDataType == KLCoilTypeT) { rData[0] = PLCMem.Timers[nAddr].bTon;} - else if (nDataType == KLCoilTypeC) { rData[0] = PLCMem.Timers[nAddr].bTon;} + if (nDataType == KLCoilTypeX) { rData[0] = ((KMem.WX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nAddr].bTon;} + else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nAddr].bTon;} + else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nAddr>>4]&(1<<(nAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeLR) { rData[0] = 0;} else {rData[0]=0;} - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData); + SendPacket(nChn, p2, PacketLen); break; case KLCmdWrite1Bit: nAddr=p1->Params[0]+ (p1->Params[1]<<8); @@ -238,82 +272,145 @@ else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nAddr>>4],nAddr&0x0f,p1->Params[2]);} else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nAddr>>4],nAddr&0x0f,p1->Params[2]);} else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nAddr>>4],nAddr&0x0f,p1->Params[2]);} - else if (nDataType == KLCoilTypeT) { PLCMem.Timers[nAddr].bTon = p1->Params[2];} - else if (nDataType == KLCoilTypeC) { PLCMem.Timers[nAddr].bTon = p1->Params[2];} - else if (nDataType == KLCoilTypeLR) { ;} + else if (nDataType == KLCoilTypeT) { KMem.Timers[nAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeC) { KMem.Timers[nAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nAddr>>4],nAddr&0x0f,p1->Params[2]);;} else {rData[0]=0;} DataLen=0; - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData); + SendPacket(nChn, p2, PacketLen); break; case KLCmdReadBits: break; case KLCmdWriteBits: break; + case KLCmdChgMode: + + break; + case KLCmdReadProgram: + nAddr=p1->Params[0]+ (p1->Params[1]<<8); + DataLen= p1->Params[2]; + if (nDataType==0){ + pData = (unsigned short *)STORE_PRG_BASE + nAddr; + } else if (nDataType==1){ + pData = (unsigned short *)ALT_PRG_BASE + nAddr; + } else if (KMRunStat.nBinProgBank == 0) { + pData = (unsigned short *)STORE_PRG_BASE + nAddr; + }else { + pData = (unsigned short *)ALT_PRG_BASE + nAddr; + } + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); + + break; + case KLCmdStartProgram: + DataLen=nDataType; +// KLBufferIn[0]=p1->Params[0]; +// p1->Params[0]=KLBufferOut[0]; + if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0; + + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartProgram,0,0); + SendPacket(nChn, p2, PacketLen); + + break; + case KLCmdWriteProgram: + if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0; + nAddr=p1->Params[0]+ (p1->Params[1]<<8); + DataLen= p1->Params[2]; + + //DataLen=16; + for (int i=0;i<DataLen;i++) + {KLPacketBuf2[i]=p1->Params[4+i];} + WriteProgram(nAddr, KLPacketBuf2, DataLen,nDataType); + DataLen=4; + *((int *)(&rData[0]))=(long)(p1->Params+4); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdFinishProgram: + nAddr=p1->Params[0]+ (p1->Params[1]<<8); //Program Size; + DataLen=nDataType; + KMRunStat.nBinProgSize=nAddr; + if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;} + else {KMRunStat.nBinProgBank=0;} + SaveRunStat(&KMRunStat); + PLCMem.bPLCRunning=1; +// KLBufferIn[0]=p1->Params[0]; +// p1->Params[0]=KLBufferOut[0]; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdReadRunStat: + DataLen= sizeof(stRunStat); + pData=&KMRunStat; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); + break; case KLCmdSaveSysCfg: - WriteSysCfgToFlash(&KMSysCfg); - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0); - SendPacket1(p2,PacketLen); + WriteSysCfgToFlash(&storedKMSysCfg); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0); + SendPacket(nChn, p2, PacketLen); break; case KLCmdSaveRunStat: SaveRunStat(&KMRunStat); - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdSaveRunStat,0,0); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0); + SendPacket(nChn, p2, PacketLen); break; case KLCmdGetEventLogCount: DataLen= 4; pData=&KMem.nEventCount; - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); break; case KLCmdGetEventLog: nAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen= p1->Params[2] * sizeof(stEventLog); pData=GetEventLogAddr(nAddr); - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,p1->nCMD,DataLen,pData); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); break; case KLCmdClearEventLog: ClearEventLog(); - PacketLen=KLMakeRplyPacket(p2,nDstAddr,nKLStatus.StatByte,KLCmdClearEventLog,0,0); - SendPacket1(p2,PacketLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0); + SendPacket(nChn, p2, PacketLen); break; default: DataLen=1; rData[0]=KL_UNKNOWN; PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); - SendPacket1(p2,PacketLen); + SendPacket(nChn, p2, PacketLen); break; } } return 0; } -int KLParsePacket(void * pBuf,int Len1) +int KLParsePacket(int nChn, void * pBuf, int Len1) { - pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf; - KLThisuS=GetuS(); - pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2; +// pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf; +// KLThisuS=GetuS(); +// pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2; int PacketLen=Len1; - int Result=KLCheckPacket(p1,Len1); + int Result=KLCheckPacket(nChn, pBuf, Len1); if (Result != KL_OK) { int DataLen=4; char rData[4]; rData[0]=Result; - PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); + PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); // PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData); - SendPacket1(p2,PacketLen); + SendPacket(nChn, KLPacketBuf2, PacketLen); return Result; } -// if (bMaster) Result=ParseMasterPacket(p1,Len1); +// if (bKBusMaster) Result=ParseMasterPacket(p1,Len1); // memcpy(KLPacketBuf2,pBuf,Len1); -// SendPacket1(p2,PacketLen); +// SendPacket(nChn, KLPacketBuf2, PacketLen); - Result=KLParseReqPacket(p1,Len1); + Result=KLParseReqPacket(nChn, pBuf, Len1); //int len1=p1->PacketLen; -// if (p1->DstAddr!=255&&p1->DstAddr!=2) return -3; +// if (p1->DstHost!=255&&p1->DstHost!=2) return -3; // pPacket p2=(pPacket)PacketBuf2; // Uart2Stat.OKPacket++; -- Gitblit v1.9.1