From db25c9f050a690bbec032ef3f93e244c7ec2dbee Mon Sep 17 00:00:00 2001 From: QuakeGod <quakegod@sina.com> Date: 星期五, 26 五月 2023 09:30:59 +0800 Subject: [PATCH] PLCFunction fix2 --- Src/KLink.c | 204 ++++++++++++++++++++++++++++++++------------------ 1 files changed, 129 insertions(+), 75 deletions(-) diff --git a/Src/KLink.c b/Src/KLink.c index da4fd03..c2ad037 100644 --- a/Src/KLink.c +++ b/Src/KLink.c @@ -16,7 +16,7 @@ unsigned char KLBufferIn[16]={0}; unsigned char KLBufferOut[16]={0}; -unsigned char nKLAddr=1; +unsigned char nKLStationId=1; unsigned char nKLSeq=0; unKLStat nKLStatus ={0}; @@ -61,7 +61,7 @@ case KLCmdRead: // case KLCmdReadReply: //p1->data[DataLen+1]=KLEndSign; - //PacketLenth=sizeof(stPacket)+DataLen+1; + //PacketLenth=sizeof(stKBPacket)+DataLen+1; break; case KLCmdWrite: // case KLCmdWriteReply: @@ -91,13 +91,13 @@ } if (nLen1<DataLen+sizeof(stKLReqPktHdr)) { - //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stPacket)); + //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket)); //PutStr(str3,len4); return 3; //not long enough } // if (p1->data[DataLen+1] != EndSign) // { -// ChnStats[nCurPollId].NoEndErr++; +// KBusChnStats[nCurPollId].NoEndErr++; // Uart2Stat.LengthErr++; // return -2; // } @@ -129,11 +129,13 @@ { pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf; - int nDstHost=p1->DstHost; + unsigned char nDstHost=p1->DstHost; KLRecvTimeuS=KLThisuS; - int nDataType=p1->nType1; - int nAddr=0; - int DataLen=0; //p1->LoadLen; + unsigned char nDataType=p1->nType1; + unsigned short nWordAddr=0; + unsigned short nByteAddr=0; + unsigned short nBitAddr=0; + unsigned short DataLen=0; //p1->LoadLen; //int nSrcAddr=p1->SrcAddr; nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;; @@ -144,7 +146,7 @@ int PacketLen=0; KLRecvTimeuS=KLThisuS; - if (nDstHost==nKLAddr || nDstHost==0xff) + if (nDstHost==nKLStationId || nDstHost==0xff) { switch (p1->nCMD) { @@ -183,7 +185,7 @@ SendPacket(nChn, p2, PacketLen); break; case KLCmdWriteFactoryData: - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nWordAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen= p1->Params[2]; //DataLen=16; for (int i=0;i<DataLen;i++) @@ -192,26 +194,28 @@ 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); + nWordAddr=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.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 *)&storedKMSysCfg + nAddr;} - else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;} - else { pData=KLBufferOut+nAddr; } + if (nDataType ==KLDataTypeDT) { pData=KMem.DT+nWordAddr; } + else if (nDataType == KLDataTypeSDT) { pData=KMem.SDT+nWordAddr; } + else if (nDataType == KLDataTypeWSR) { pData=KMem.WSR+nWordAddr; } + else if (nDataType == KLDataTypeWX) { pData=KMem.WX+nWordAddr; } + else if (nDataType == KLDataTypeWY) { pData=KMem.WY+nWordAddr; } + else if (nDataType == KLDataTypeWR) { pData=KMem.WR+nWordAddr; } + else if (nDataType == KLDataTypeWLX) { pData=KMem.WLX+nWordAddr; } + else if (nDataType == KLDataTypeWLY) { pData=KMem.WLY+nWordAddr; } + else if (nDataType == KLDataTypeSV) { pData=KMem.SV+nWordAddr; } + else if (nDataType == KLDataTypeEV) { pData=KMem.EV+nWordAddr; } + else if (nDataType == KLDataTypeTest) { pData=KMem.SDT+nWordAddr; } + else if (nDataType == KLDataTypeWDT) { pData=KMem.WDT+nWordAddr; } + else if (nDataType == KLDataTypeKBD) { pData=(unsigned short *)&KBusChnStats +nWordAddr; } + else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;} + else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;} + else { pData=KLBufferOut+nWordAddr; } PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); SendPacket(nChn, p2, PacketLen); @@ -224,22 +228,24 @@ case KLCmdWriteDataByte: //memcpy(DispBuf,p1->data,DataLen); - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nWordAddr=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.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 *)&storedKMSysCfg + nAddr;} - else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nAddr;} - else { pData=KLBufferOut+nAddr; DataLen=0; } + if (nDataType ==KLDataTypeDT) { pData=KMem.DT+nWordAddr; } + else if (nDataType == KLDataTypeSDT) { pData=KMem.SDT+nWordAddr; } + else if (nDataType == KLDataTypeWSR) { pData=KMem.WSR+nWordAddr; } + else if (nDataType == KLDataTypeWX) { pData=KMem.WX+nWordAddr; } + else if (nDataType == KLDataTypeWY) { pData=KMem.WY+nWordAddr; } + else if (nDataType == KLDataTypeWR) { pData=KMem.WR+nWordAddr; } + else if (nDataType == KLDataTypeWLX) { pData=KMem.WLX+nWordAddr; } + else if (nDataType == KLDataTypeWLY) { pData=KMem.WLY+nWordAddr; } + else if (nDataType == KLDataTypeSV) { pData=KMem.SV+nWordAddr; DataLen=0;} + else if (nDataType == KLDataTypeEV) { pData=KMem.EV+nWordAddr; DataLen=0;} + else if (nDataType == KLDataTypeTest) { pData=KMem.SDT+nWordAddr; DataLen=0;} + else if (nDataType == KLDataTypeWDT) { pData=KMem.WDT+nWordAddr; DataLen=0;} + else if (nDataType == KLDataTypeKBD) { pData=(unsigned short *)KBusChnStats +nWordAddr; DataLen=0;} + else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;} + else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;} + else { pData=KLBufferOut+nWordAddr; DataLen=0; } memcpy(pData,p1->Params+4,DataLen); PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); @@ -249,16 +255,16 @@ // break; case KLCmdRead1Bit: - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nBitAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen = 1; - 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);} + if (nDataType == KLCoilTypeX) { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} + else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nBitAddr].bTon;} + else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nBitAddr].bTon;} + else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);} else if (nDataType == KLCoilTypeLR) { rData[0] = 0;} else {rData[0]=0;} @@ -266,16 +272,16 @@ SendPacket(nChn, p2, PacketLen); break; case KLCmdWrite1Bit: - nAddr=p1->Params[0]+ (p1->Params[1]<<8); - if (nDataType == KLCoilTypeX) { SetBitValue( &KMem.WX[nAddr>>4],nAddr&0x0f,p1->Params[2]);} - else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nAddr>>4],nAddr&0x0f,p1->Params[2]);} - 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) { 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]);;} + nBitAddr=p1->Params[0]+ (p1->Params[1]<<8); + if (nDataType == KLCoilTypeX) { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);} + else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);} + else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);} + else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);} + else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);} + else if (nDataType == KLCoilTypeT) { KMem.Timers[nBitAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];} + else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;} else {rData[0]=0;} DataLen=0; PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData); @@ -286,19 +292,22 @@ case KLCmdWriteBits: break; case KLCmdChgMode: - + if (nDataType ==0) {StopPLC();} + if (nDataType == 1) {StartPLC();} + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); break; case KLCmdReadProgram: - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nWordAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen= p1->Params[2]; if (nDataType==0){ - pData = (unsigned short *)STORE_PRG_BASE + nAddr; + pData = (unsigned short *)STORE_PRG_BASE + nWordAddr; } else if (nDataType==1){ - pData = (unsigned short *)ALT_PRG_BASE + nAddr; + pData = (unsigned short *)ALT_PRG_BASE + nWordAddr; } else if (KMRunStat.nBinProgBank == 0) { - pData = (unsigned short *)STORE_PRG_BASE + nAddr; + pData = (unsigned short *)STORE_PRG_BASE + nWordAddr; }else { - pData = (unsigned short *)ALT_PRG_BASE + nAddr; + pData = (unsigned short *)ALT_PRG_BASE + nWordAddr; } PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); SendPacket(nChn, p2, PacketLen); @@ -316,22 +325,22 @@ break; case KLCmdWriteProgram: if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0; - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nWordAddr=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); + WriteProgram(nWordAddr, 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; + nWordAddr=p1->Params[0]+ (p1->Params[1]<<8); //Program Size; DataLen=nDataType; - KMRunStat.nBinProgSize=nAddr; + KMRunStat.nBinProgSize=nWordAddr; if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;} else {KMRunStat.nBinProgBank=0;} SaveRunStat(&KMRunStat); @@ -341,11 +350,51 @@ PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0); SendPacket(nChn, p2, PacketLen); break; + + case KLCmdBlinkLED: + DataLen=nDataType; + KMRunStat.bLEDFlick=DataLen; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); + break; + + case KLCmdStopBlinkLED: + DataLen=nDataType; + KMRunStat.bLEDFlick=0; + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,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 KLCmdReadSysCfg: + nByteAddr=p1->Params[0]+ (p1->Params[1]<<8); + pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr; + DataLen = p1->Params[2]; + if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;} + else { + if (DataLen + nByteAddr >sizeof(stKMSysCfg)) + DataLen= sizeof(stKMSysCfg) - nByteAddr; + } + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); + SendPacket(nChn, p2, PacketLen); + break; + case KLCmdWriteSysCfg: + nByteAddr=p1->Params[0]+ (p1->Params[1]<<8); + pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr; + DataLen = p1->Params[2]; + if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;} + else { + if (DataLen + nByteAddr >sizeof(stKMSysCfg)) + DataLen= sizeof(stKMSysCfg) - nByteAddr; + } + memcpy(pData,p1->Params+4,DataLen); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); break; case KLCmdSaveSysCfg: WriteSysCfgToFlash(&storedKMSysCfg); @@ -357,6 +406,11 @@ PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0); SendPacket(nChn, p2, PacketLen); break; + case KLCmdClearRunStat: + memset(KBusChnStats,0,sizeof(KBusChnStats)); + PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0); + SendPacket(nChn, p2, PacketLen); + break; case KLCmdGetEventLogCount: DataLen= 4; pData=&KMem.nEventCount; @@ -364,9 +418,9 @@ SendPacket(nChn, p2, PacketLen); break; case KLCmdGetEventLog: - nAddr=p1->Params[0]+ (p1->Params[1]<<8); + nWordAddr=p1->Params[0]+ (p1->Params[1]<<8); DataLen= p1->Params[2] * sizeof(stEventLog); - pData=GetEventLogAddr(nAddr); + pData=GetEventLogAddr(nWordAddr); PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData); SendPacket(nChn, p2, PacketLen); break; @@ -378,7 +432,7 @@ default: DataLen=1; rData[0]=KL_UNKNOWN; - PacketLen=KLMakeRplyPacket(p2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); + PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); SendPacket(nChn, p2, PacketLen); break; } @@ -398,7 +452,7 @@ char rData[4]; rData[0]=Result; - PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLAddr,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); + PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData); // PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData); SendPacket(nChn, KLPacketBuf2, PacketLen); return Result; @@ -411,7 +465,7 @@ //int len1=p1->PacketLen; // if (p1->DstHost!=255&&p1->DstHost!=2) return -3; -// pPacket p2=(pPacket)PacketBuf2; +// pKBPacket p2=(pKBPacket)PacketBuf2; // Uart2Stat.OKPacket++; return Result; -- Gitblit v1.9.1