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