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