QuakeGod
2023-05-26 db25c9f050a690bbec032ef3f93e244c7ec2dbee
提交 | 用户 | age
bfc108 1 /**
Q 2   ******************************************************************************
3   * @file           : KLink.c
4   * @brief          : K-Link Protocol program body
5   ******************************************************************************
6     */
7 #include "KLink.h"
8 #include "functions.h"
9 #include "string.h"
10 #include "PLCFunctions.h"
11 #include "stm32f0xx_hal.h"
12
13 unsigned char KLPacketBuf1[256];
14 unsigned char KLPacketBuf2[256];
15
16 unsigned char KLBufferIn[16]={0};
17 unsigned char KLBufferOut[16]={0};
18
0fe6b0 19 unsigned  char nKLStationId=1;
bfc108 20 unsigned  char nKLSeq=0;
Q 21
22 unKLStat nKLStatus ={0};
23
24 int KLThisuS=0;
25 int KLRecvTimeuS=0;
26
a7db3c 27 unsigned char KLBCC(void const * pData, int nSize)
bfc108 28 {
Q 29     unsigned char k;
30     k=0;
31     for (int i=0;i<nSize;i++)
32     {
33         k^=((unsigned char *)pData)[i];
34     }
35     return k;    
36 }
a7db3c 37 int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,const void * pData )
bfc108 38 {
Q 39     pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
40     int PacketLenth=0;
41     p1->RplyStSgn=KLSignReply;
a7db3c 42     p1->DstHost=dst;
bfc108 43     p1->nStatus=Status;
Q 44     p1->nRplyCMD=nCmd;
45     p1->nSize1=DataLen;
46     memcpy(p1->Datas,pData,DataLen);
f4f290 47     p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
bfc108 48     PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
Q 49     
50
51     switch (nCmd)
52     {
53         case KLCmdNone:
54             PacketLenth=0;
55             break;
56         case KLCmdPing:
57 //        case KLCmdPingReply:
58             //p1->data[DataLen+1]=KLEndSign;        
59             //PacketLenth=sizeof(stKLPacket)+DataLen+1;        
60             break;
61         case KLCmdRead:
62 //        case KLCmdReadReply:
63             //p1->data[DataLen+1]=KLEndSign;
0fe6b0 64             //PacketLenth=sizeof(stKBPacket)+DataLen+1;
bfc108 65             break;
Q 66         case KLCmdWrite:
67 //        case KLCmdWriteReply:
68         break;
69         case KLCmdErrRply:
70         
71         break;
72         default:
73             break;
74     }
75     
76     return PacketLenth;
77 }
78
a7db3c 79 int KLCheckPacket(int nChn, void * pBuf, int nLen1)
bfc108 80 {
Q 81     pKLReqPktHdr p1 = (pKLReqPktHdr) pBuf;
82     if (p1->ReqStSgn != KLSignStart)
83     {
84         return 1;
85     }
86     
87     int DataLen=0;    //p1->LoadLen;
88     if (DataLen>KLMaxPacketLength) 
89     {
90         return 2;
91     }
92     if (nLen1<DataLen+sizeof(stKLReqPktHdr))
93     {
0fe6b0 94         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
bfc108 95         //PutStr(str3,len4);
Q 96         return 3;    //not long enough                    
97     }
98 //    if (p1->data[DataLen+1] != EndSign)
99 //    {
0fe6b0 100 //        KBusChnStats[nCurPollId].NoEndErr++;
bfc108 101 //        Uart2Stat.LengthErr++;        
Q 102 //        return -2;
103 //    }
104     unsigned char thisBCC=KLBCC(p1,nLen1-1);
105     if (thisBCC != ((uchar *)pBuf)[nLen1-1]) 
106     {//BCC Error;
107         return 4;
108     }         
109     return 0;
110 }
111
112 inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
113 {
a7db3c 114     (*pW)|=1<<(bitAddr&0xf);
bfc108 115 }
Q 116
117 inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
118 {
a7db3c 119     (*pW)&=~(1<<(bitAddr&0xf));
bfc108 120 }
Q 121
122 static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
123 {
124     if (Value)    {    SetAddrBit(pW, bitAddr);}
125     else {ResetBit(pW, bitAddr);}
126 }
a7db3c 127
Q 128 int KLParseReqPacket(int nChn, void * pBuf, int Len1)
bfc108 129 {
Q 130     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
131
0fe6b0 132     unsigned char nDstHost=p1->DstHost;
bfc108 133     KLRecvTimeuS=KLThisuS;
0fe6b0 134     unsigned char nDataType=p1->nType1;
Q 135     unsigned short nWordAddr=0;
136     unsigned short nByteAddr=0;
137     unsigned short nBitAddr=0;
138     unsigned short DataLen=0;    //p1->LoadLen;
bfc108 139 //int nSrcAddr=p1->SrcAddr;
Q 140
141     nKLStatus.nSEQ = ((pKLStat)(&(p1->Stat)))->nSEQ;;
142     
143     void * pData=0;
144     pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
145     char rData[4];
146
147     int PacketLen=0;
148     KLRecvTimeuS=KLThisuS;
0fe6b0 149     if (nDstHost==nKLStationId || nDstHost==0xff)
bfc108 150     {
Q 151         switch (p1->nCMD)
152         {
153             case KLCmdNone:
154                 break;
155             case KLCmdPing:
156                 DataLen=nDataType;
157 //                KLBufferIn[0]=p1->Params[0];
158 //                p1->Params[0]=KLBufferOut[0];
a7db3c 159                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
Q 160                 SendPacket(nChn, p2, PacketLen);
bfc108 161                 break;
Q 162 //            case KLCmdPingReply:
163 //                break;
164             case KLCmdInfo:
165                 //if (nDataType == KLDataTypeDT)
a7db3c 166                 DataLen= sizeof(KMInfoBlock);
Q 167                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&KMInfoBlock);
168                 SendPacket(nChn, p2, PacketLen);                
169                 break;
170             case KLCmdGetUid:
171                 //if (nDataType == KLDataTypeDT)
172                 DataLen= 12;
173                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(uint32_t *)UID_BASE);
174                 SendPacket(nChn, p2, PacketLen);                
175                 break;
176             case KLCmdGetSN:
177                 //if (nDataType == KLDataTypeDT)
bfc108 178                 DataLen= 4;
a7db3c 179                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1);
Q 180                 SendPacket(nChn, p2, PacketLen);                
181                 break;
182             case KLCmdGetFactoryData:
183                 DataLen= sizeof(stFactoryData);
184                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,(pFactoryData)FACTORY_DATA_BASE);
185                 SendPacket(nChn, p2, PacketLen);                
186                 break;
187             case KLCmdWriteFactoryData:
0fe6b0 188                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
a7db3c 189                 DataLen= p1->Params[2];
Q 190                 //DataLen=16;
191                 for (int i=0;i<DataLen;i++)
192                 {KLPacketBuf2[i]=p1->Params[4+i];}
193                 WriteFactoryData(KLPacketBuf2, DataLen);
194                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
195                 SendPacket(nChn, p2, PacketLen);
196                 break;
db25c9 197
bfc108 198             case KLCmdRead:
a7db3c 199             case KLCmdReadDataWord:
bfc108 200             case KLCmdReadDataByte:                
0fe6b0 201                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
bfc108 202                 DataLen= p1->Params[2];
0fe6b0 203                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nWordAddr;        }
Q 204                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
205                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
206                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
207                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
208                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
209                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
210                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
211                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nWordAddr;    }
212                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nWordAddr;    }
213                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    }
214                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    }
215                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)&KBusChnStats  +nWordAddr;    }
216                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;}
217                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
218                 else                {                    pData=KLBufferOut+nWordAddr;                }
bfc108 219                 
a7db3c 220                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 221                 SendPacket(nChn, p2, PacketLen);
bfc108 222                 
Q 223                 break;
224 //            case KLCmdReadReply:
225 //                break;
226             case KLCmdWrite:
a7db3c 227             case KLCmdWriteDataWord:
bfc108 228             case KLCmdWriteDataByte:
Q 229                 
230                 //memcpy(DispBuf,p1->data,DataLen);
0fe6b0 231                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
bfc108 232                 DataLen= p1->Params[2];
0fe6b0 233                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nWordAddr;        }
Q 234                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
235                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
236                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
237                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
238                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
239                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
240                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
241                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nWordAddr;    DataLen=0;}
242                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nWordAddr;    DataLen=0;}
243                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    DataLen=0;}
244                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    DataLen=0;}
245                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)KBusChnStats +nWordAddr;    DataLen=0;}
246                 else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;}
247                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
248                 else                {                    pData=KLBufferOut+nWordAddr;    DataLen=0;            }
bfc108 249                         
Q 250                 memcpy(pData,p1->Params+4,DataLen);                    
a7db3c 251                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
Q 252                 SendPacket(nChn, p2, PacketLen);                    
bfc108 253                 break;
Q 254 //            case KLCmdWriteReply:
255 //                break;
256
257             case KLCmdRead1Bit:
0fe6b0 258                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
bfc108 259                 DataLen = 1;
0fe6b0 260                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
Q 261                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
262                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
263                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
264                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
265                 else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nBitAddr].bTon;}
266                 else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nBitAddr].bTon;}
267                 else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
a7db3c 268                 
bfc108 269                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
Q 270                 else {rData[0]=0;}
a7db3c 271                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
Q 272                 SendPacket(nChn, p2, PacketLen);
bfc108 273                 break;
Q 274             case KLCmdWrite1Bit:
0fe6b0 275                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
Q 276                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
277                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
278                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
279                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
280                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
281                 else if (nDataType == KLCoilTypeT) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
282                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
283                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
284                 else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
bfc108 285                 else {rData[0]=0;}
Q 286                 DataLen=0;
a7db3c 287                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
Q 288                 SendPacket(nChn, p2, PacketLen);
bfc108 289                 break;
Q 290             case KLCmdReadBits:
291                 break;
292             case KLCmdWriteBits:
293                 break;
a7db3c 294             case KLCmdChgMode:
db25c9 295                 if (nDataType ==0) {StopPLC();}
Q 296                 if (nDataType == 1) {StartPLC();}
297                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
298                 SendPacket(nChn, p2, PacketLen);                
a7db3c 299                 break;
Q 300             case KLCmdReadProgram:
0fe6b0 301                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
a7db3c 302                 DataLen= p1->Params[2];
Q 303                  if (nDataType==0){
0fe6b0 304                         pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
a7db3c 305                  } else if (nDataType==1){
0fe6b0 306                         pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
a7db3c 307                  } else if (KMRunStat.nBinProgBank == 0) {
0fe6b0 308                         pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
a7db3c 309                  }else {
0fe6b0 310                         pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
a7db3c 311                  }
Q 312                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
313                 SendPacket(nChn, p2, PacketLen);
314                 
315                 break;
316             case KLCmdStartProgram:
317                 DataLen=nDataType;
318 //                KLBufferIn[0]=p1->Params[0];
319 //                p1->Params[0]=KLBufferOut[0];
320                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
321             
322                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartProgram,0,0);
323                 SendPacket(nChn, p2, PacketLen);
324                 
325                 break;
326             case KLCmdWriteProgram:
327                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
0fe6b0 328                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
a7db3c 329                 DataLen= p1->Params[2];
Q 330
331                 //DataLen=16;
332                 for (int i=0;i<DataLen;i++)
333                 {KLPacketBuf2[i]=p1->Params[4+i];}
0fe6b0 334                 WriteProgram(nWordAddr, KLPacketBuf2, DataLen,nDataType);
a7db3c 335                 DataLen=4;
Q 336                 *((int *)(&rData[0]))=(long)(p1->Params+4);
337                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0);
338                 SendPacket(nChn, p2, PacketLen);
339                 break;
340             case KLCmdFinishProgram:
0fe6b0 341                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
a7db3c 342                 DataLen=nDataType;
0fe6b0 343                 KMRunStat.nBinProgSize=nWordAddr;
a7db3c 344                 if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
Q 345                 else {KMRunStat.nBinProgBank=0;}
346                 SaveRunStat(&KMRunStat);                
347                 PLCMem.bPLCRunning=1;
348 //                KLBufferIn[0]=p1->Params[0];
349 //                p1->Params[0]=KLBufferOut[0];
350                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
351                 SendPacket(nChn, p2, PacketLen);
352                 break;
0fe6b0 353                 
Q 354             case KLCmdBlinkLED:
355                 DataLen=nDataType;
356                 KMRunStat.bLEDFlick=DataLen;
357                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
358                 SendPacket(nChn, p2, PacketLen);
359                 break;
360             
361             case KLCmdStopBlinkLED:
362                 DataLen=nDataType;
363                 KMRunStat.bLEDFlick=0;
364                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
365                 SendPacket(nChn, p2, PacketLen);
366                 break;
367             
a7db3c 368             case KLCmdReadRunStat:
Q 369                 DataLen= sizeof(stRunStat);
370                 pData=&KMRunStat;
371                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
372                 SendPacket(nChn, p2, PacketLen);    
0fe6b0 373                 break;
Q 374             case KLCmdReadSysCfg:
375                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
376                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
377                 DataLen = p1->Params[2];
378                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
379                 else {
380                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
381                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
382                 }
383                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
384                 SendPacket(nChn, p2, PacketLen);    
385                 break;
386             case KLCmdWriteSysCfg:
387                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
388                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
389                 DataLen = p1->Params[2];
390                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
391                 else {
392                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
393                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
394                 }
395                 memcpy(pData,p1->Params+4,DataLen);                    
396                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
397                 SendPacket(nChn, p2, PacketLen);                
a7db3c 398                 break;
bfc108 399             case KLCmdSaveSysCfg:
a7db3c 400                 WriteSysCfgToFlash(&storedKMSysCfg);
Q 401                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
402                 SendPacket(nChn, p2, PacketLen);
bfc108 403                 break;
Q 404             case     KLCmdSaveRunStat:
405                 SaveRunStat(&KMRunStat);
a7db3c 406                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
Q 407                 SendPacket(nChn, p2, PacketLen);
bfc108 408                 break;
0fe6b0 409             case KLCmdClearRunStat:
Q 410                 memset(KBusChnStats,0,sizeof(KBusChnStats));
411                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
412                 SendPacket(nChn, p2, PacketLen);                    
413                 break;
bfc108 414             case KLCmdGetEventLogCount:
Q 415                 DataLen= 4;
416                 pData=&KMem.nEventCount;
a7db3c 417                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 418                 SendPacket(nChn, p2, PacketLen);
bfc108 419                 break;
Q 420             case KLCmdGetEventLog:
0fe6b0 421                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
bfc108 422                 DataLen= p1->Params[2] * sizeof(stEventLog);
0fe6b0 423                 pData=GetEventLogAddr(nWordAddr);
a7db3c 424                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
Q 425                 SendPacket(nChn, p2, PacketLen);
bfc108 426                 break;
Q 427             case KLCmdClearEventLog:
428                 ClearEventLog();
a7db3c 429                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
Q 430                 SendPacket(nChn, p2, PacketLen);
bfc108 431                 break;            
Q 432             default:
433             DataLen=1;
434             rData[0]=KL_UNKNOWN;
0fe6b0 435             PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
a7db3c 436             SendPacket(nChn, p2, PacketLen);            
bfc108 437                 break;        
Q 438         }
439     }    
440     return 0;
441 }
a7db3c 442 int KLParsePacket(int nChn, void * pBuf, int Len1)
bfc108 443 {
a7db3c 444 //    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
Q 445 //    KLThisuS=GetuS();
446 //    pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
bfc108 447     int PacketLen=Len1;
a7db3c 448     int Result=KLCheckPacket(nChn, pBuf, Len1);
bfc108 449     if (Result != KL_OK)
Q 450     {
451         int DataLen=4;
452         char rData[4];
453         rData[0]=Result;
454         
0fe6b0 455         PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
bfc108 456 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
a7db3c 457         SendPacket(nChn, KLPacketBuf2, PacketLen);
bfc108 458         return Result;
Q 459     }
a7db3c 460 //    if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
bfc108 461 //    memcpy(KLPacketBuf2,pBuf,Len1);
a7db3c 462 //    SendPacket(nChn, KLPacketBuf2, PacketLen);
bfc108 463     
a7db3c 464     Result=KLParseReqPacket(nChn, pBuf, Len1);
bfc108 465     
Q 466     //int len1=p1->PacketLen;
a7db3c 467 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
0fe6b0 468 //    pKBPacket p2=(pKBPacket)PacketBuf2;
bfc108 469 //            Uart2Stat.OKPacket++;
Q 470
471     return Result;
472 }
473