QuakeGod
2023-10-08 483170e190a0dd4666b2a63e5d31466052ba0c6a
提交 | 用户 | 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"
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
19 unsigned  char nKLStationId=1;
20 unsigned  char nKLSeq=0;
21
22 unKLStat nKLStatus ={0};
23
24 int KLThisuS=0;
25 int KLRecvTimeuS=0;
26
27 unsigned char KLBCC(void const * pData, int nSize)
28 {
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 }
37 int KLMakeRplyPacket(void * pBuf, uchar dst, uchar Status, uchar nCmd, uchar DataLen,const void * pData )
38 {
39     pKLRplyPktHdr p1 = (pKLRplyPktHdr)pBuf;
40     int PacketLenth=0;
41     p1->RplyStSgn=KLSignReply;
42     p1->DstHost=dst;
43     p1->nStatus=Status;
44     p1->nRplyCMD=nCmd;
45     p1->nSize1=DataLen;
46     memcpy(p1->Datas,pData,DataLen);
47     p1->Datas[DataLen]=KLBCC(p1,sizeof(stKLRplyPktHdr)+DataLen-1);
48     PacketLenth=sizeof(stKLRplyPktHdr)+DataLen;    
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;
64             //PacketLenth=sizeof(stKBPacket)+DataLen+1;
65             break;
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
79 int KLCheckPacket(int nChn, void * pBuf, int nLen1)
80 {
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     {
94         //len4=sprintf(str3,"%d < %d + %d \r\n",len2,DataLen,sizeof(stKBPacket));
95         //PutStr(str3,len4);
96         return 3;    //not long enough                    
97     }
98 //    if (p1->data[DataLen+1] != EndSign)
99 //    {
100 //        KBusChnStats[nCurPollId].NoEndErr++;
101 //        Uart2Stat.LengthErr++;        
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 {
114     (*pW)|=1<<(bitAddr&0xf);
115 }
116
117 inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
118 {
119     (*pW)&=~(1<<(bitAddr&0xf));
120 }
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 }
127
128 int KLParseReqPacket(int nChn, void * pBuf, int Len1)
129 {
130     pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
131
132     unsigned char nDstHost=p1->DstHost;
133     KLRecvTimeuS=KLThisuS;
134     unsigned char nDataType=p1->nType1;
135     unsigned short nWordAddr=0;
136     unsigned short nByteAddr=0;
137     unsigned short nBitAddr=0;
138     unsigned short DataLen=0;    //p1->LoadLen;
139 //int nSrcAddr=p1->SrcAddr;
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;
149     if (nDstHost==nKLStationId || nDstHost==0xff)
150     {
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];
159                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdPing,DataLen,p1->Params);
160                 SendPacket(nChn, p2, PacketLen);
161                 break;
162 //            case KLCmdPingReply:
163 //                break;
164             case KLCmdInfo:
165                 //if (nDataType == KLDataTypeDT)
166                 DataLen= sizeof(KMInfoBlock);
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)
178                 DataLen= 4;
179                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,&((pFactoryData)FACTORY_DATA_BASE)->SN1);
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:
188                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
189                 DataLen= p1->Params[2];
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;
197
198             case KLCmdRead:
199             case KLCmdReadDataWord:
200             case KLCmdReadDataByte:                
201                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
202                 DataLen= p1->Params[2];
203                 if (nDataType ==KLDataTypeDT)                {            pData=KMem.DT+nWordAddr;        }
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 == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
217                 else if (nDataType == KLDataSysCfg) { pData = (unsigned short *)&storedKMSysCfg.theKMSysCfg + nWordAddr;}
218                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
219                 else                {                    pData=KLBufferOut+nWordAddr;                }
220                 
221                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
222                 SendPacket(nChn, p2, PacketLen);
223                 
224                 break;
225 //            case KLCmdReadReply:
226 //                break;
227             case KLCmdWrite:
228             case KLCmdWriteDataWord:
229             case KLCmdWriteDataByte:
230                 
231                 //memcpy(DispBuf,p1->data,DataLen);
232                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
233                 DataLen= p1->Params[2];
234                     if (nDataType ==KLDataTypeDT)                {        pData=KMem.DT+nWordAddr;        }
235                 else if (nDataType == KLDataTypeSDT)    {        pData=KMem.SDT+nWordAddr;    }
236                 else if (nDataType == KLDataTypeWSR)    {        pData=KMem.WSR+nWordAddr;    }
237                 else if (nDataType == KLDataTypeWX)        {        pData=KMem.WX+nWordAddr;        }
238                 else if (nDataType == KLDataTypeWY)        {        pData=KMem.WY+nWordAddr;        }
239                 else if (nDataType == KLDataTypeWR)        {        pData=KMem.WR+nWordAddr;        }
240                 else if (nDataType == KLDataTypeWLX)        {        pData=KMem.WLX+nWordAddr;        }
241                 else if (nDataType == KLDataTypeWLY)        {        pData=KMem.WLY+nWordAddr;        }
242                 else if (nDataType == KLDataTypeSV)        {        pData=KMem.SV+nWordAddr;    DataLen=0;}
243                 else if (nDataType == KLDataTypeEV)        {        pData=KMem.EV+nWordAddr;    DataLen=0;}
244                 else if (nDataType == KLDataTypeTest)    {        pData=KMem.SDT+nWordAddr;    DataLen=0;}
245                 else if (nDataType == KLDataTypeWDT)    {        pData=KMem.WDT+nWordAddr;    DataLen=0;}
246                 else if (nDataType == KLDataTypeKBD)    {        pData=(unsigned short *)KBusChnStats +nWordAddr;    DataLen=0;}
247 //                else if (nDataType == KLDataTypeKWLD)    {        pData=(unsigned short *)&KwRunStat  +nWordAddr;    }
248                 else if (nDataType == KLDataSysCfg)        { pData = (unsigned short *)&(storedKMSysCfg) + nWordAddr;}
249                 else if (nDataType == KLDataTypeFlash) { pData = (unsigned short *)FLASH_BASE + nWordAddr;}
250                 else                {                    pData=KLBufferOut+nWordAddr;    DataLen=0;            }
251                         
252                 memcpy(pData,p1->Params+4,DataLen);                    
253                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
254                 SendPacket(nChn, p2, PacketLen);                    
255                 break;
256 //            case KLCmdWriteReply:
257 //                break;
258
259             case KLCmdRead1Bit:
260                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
261                 DataLen = 1;
262                 if (nDataType == KLCoilTypeX)             { rData[0] = ((KMem.WX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
263                 else if (nDataType == KLCoilTypeY) { rData[0] = ((KMem.WY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
264                 else if (nDataType == KLCoilTypeR) { rData[0] = ((KMem.WR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
265                 else if (nDataType == KLCoilTypeLX) { rData[0] = ((KMem.WLX[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
266                 else if (nDataType == KLCoilTypeLY) { rData[0] = ((KMem.WLY[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
267                 else if (nDataType == KLCoilTypeT) { rData[0] = KMem.Timers[nBitAddr].bTon;}
268                 else if (nDataType == KLCoilTypeC) { rData[0] = KMem.Timers[nBitAddr].bTon;}
269                 else if (nDataType == KLCoilTypeSR) {rData[0] = ((KMem.WSR[nBitAddr>>4]&(1<<(nBitAddr&0x0f)))>0);}
270                 
271                 else if (nDataType == KLCoilTypeLR) { rData[0] = 0;}
272                 else {rData[0]=0;}
273                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdRead1Bit,DataLen,rData);
274                 SendPacket(nChn, p2, PacketLen);
275                 break;
276             case KLCmdWrite1Bit:
277                 nBitAddr=p1->Params[0]+ (p1->Params[1]<<8);
278                 if (nDataType == KLCoilTypeX)             { SetBitValue( &KMem.WX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
279                 else if (nDataType == KLCoilTypeY) { SetBitValue( &KMem.WY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
280                 else if (nDataType == KLCoilTypeR) { SetBitValue( &KMem.WR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
281                 else if (nDataType == KLCoilTypeLX) {SetBitValue( &KMem.WLX[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
282                 else if (nDataType == KLCoilTypeLY) {SetBitValue( &KMem.WLY[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);}
283                 else if (nDataType == KLCoilTypeT) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
284                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
285                 else if (nDataType == KLCoilTypeC) { KMem.Timers[nBitAddr].bTon = p1->Params[2];}
286                 else if (nDataType == KLCoilTypeLR) { SetBitValue( &KMem.WSR[nBitAddr>>4],nBitAddr&0x0f,p1->Params[2]);;}
287                 else {rData[0]=0;}
288                 DataLen=0;
289                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWrite1Bit,DataLen,rData);
290                 SendPacket(nChn, p2, PacketLen);
291                 break;
292             case KLCmdReadBits:
293                 break;
294             case KLCmdWriteBits:
295                 break;
296             case KLCmdChgMode:
297                 if (nDataType ==0) {StopPLC();}
298                 if (nDataType == 1) {StartPLC();}
299                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
300                 SendPacket(nChn, p2, PacketLen);                
301                 break;
302             case KLCmdReadProgram:
303                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
304                 DataLen= p1->Params[2];
305                  if (nDataType==0){
306                         pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
307                  } else if (nDataType==1){
308                         pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
309                  } else if (KMRunStat.nBinProgBank == 0) {
310                         pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
311                  }else {
312                         pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
313                  }
314                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
315                 SendPacket(nChn, p2, PacketLen);
316                 
317                 break;
318             case KLCmdStartProgram:
319                 DataLen=nDataType;
320 //                KLBufferIn[0]=p1->Params[0];
321 //                p1->Params[0]=KLBufferOut[0];
322                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
323             
324                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartProgram,0,0);
325                 SendPacket(nChn, p2, PacketLen);
326                 
327                 break;
328             case KLCmdWriteProgram:
329                 if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
330                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
331                 DataLen= p1->Params[2];
332
333                 //DataLen=16;
334                 for (int i=0;i<DataLen;i++)
335                 {KLPacketBuf2[i]=p1->Params[4+i];}
336                 WriteProgram(nWordAddr, KLPacketBuf2, DataLen,nDataType);
337                 DataLen=4;
338                 *((int *)(&rData[0]))=(long)(p1->Params+4);
339                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0);
340                 SendPacket(nChn, p2, PacketLen);
341                 break;
342             case KLCmdFinishProgram:
343                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);        //Program Size;
344                 DataLen=nDataType;
345                 KMRunStat.nBinProgSize=nWordAddr;
346                 if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
347                 else {KMRunStat.nBinProgBank=0;}
348                 SaveRunStat(&KMRunStat);                
349                 //PLCMem.bPLCRunning=1;
350 //                KLBufferIn[0]=p1->Params[0];
351 //                p1->Params[0]=KLBufferOut[0];
352                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
353                 SendPacket(nChn, p2, PacketLen);
354                 break;
355                 
356             case KLCmdBlinkLED:
357                 DataLen=nDataType;
358                 KMRunStat.bLEDFlick=DataLen;
359                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
360                 SendPacket(nChn, p2, PacketLen);
361                 break;
362             
363             case KLCmdStopBlinkLED:
364                 DataLen=nDataType;
365                 KMRunStat.bLEDFlick=0;
366                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
367                 SendPacket(nChn, p2, PacketLen);
368                 break;
369             
370             case KLCmdReadRunStat:
371                 DataLen= sizeof(stRunStat);
372                 pData=&KMRunStat;
373                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
374                 SendPacket(nChn, p2, PacketLen);    
375                 break;
376             case KLCmdReadSysCfg:
377                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
378                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
379                 DataLen = p1->Params[2];
380                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
381                 else {
382                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
383                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
384                 }
385                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
386                 SendPacket(nChn, p2, PacketLen);    
387                 break;
388             case KLCmdWriteSysCfg:
389                 nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
390                 pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
391                 DataLen = p1->Params[2];
392                 if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
393                 else {
394                     if (DataLen + nByteAddr >sizeof(stKMSysCfg))
395                         DataLen= sizeof(stKMSysCfg) - nByteAddr;
396                 }
397                 memcpy(pData,p1->Params+4,DataLen);                    
398                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
399                 SendPacket(nChn, p2, PacketLen);                
400                 WriteSysCfgToFlash(&storedKMSysCfg);
401                 break;
402             case KLCmdSaveSysCfg:
403                 WriteSysCfgToFlash(&storedKMSysCfg);
404                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
405                 SendPacket(nChn, p2, PacketLen);
406                 break;
407             case     KLCmdSaveRunStat:
408                 SaveRunStat(&KMRunStat);
409                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
410                 SendPacket(nChn, p2, PacketLen);
411                 break;
412             case KLCmdClearRunStat:
413                 memset(KBusChnStats,0,sizeof(KBusChnStats));
414                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
415                 SendPacket(nChn, p2, PacketLen);                    
416                 break;
417             case KLCmdGetEventLogCount:
418                 DataLen= 4;
419                 pData=&KMem.nEventCount;
420                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
421                 SendPacket(nChn, p2, PacketLen);
422                 break;
423             case KLCmdGetEventLog:
424                 nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
425                 DataLen= p1->Params[2] * sizeof(stEventLog);
426                 pData=GetEventLogAddr(nWordAddr);
427                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
428                 SendPacket(nChn, p2, PacketLen);
429                 break;
430             case KLCmdClearEventLog:
431                 ClearEventLog();
432                 PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
433                 SendPacket(nChn, p2, PacketLen);
434                 break;            
435             default:
436             DataLen=1;
437             rData[0]=KL_UNKNOWN;
438             PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
439             SendPacket(nChn, p2, PacketLen);            
440                 break;        
441         }
442     }    
443     return 0;
444 }
445 int KLParsePacket(int nChn, void * pBuf, int Len1)
446 {
447 //    pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
448 //    KLThisuS=GetuS();
449 //    pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
450     int PacketLen=Len1;
451     int Result=KLCheckPacket(nChn, pBuf, Len1);
452     if (Result != KL_OK)
453     {
454         int DataLen=4;
455         char rData[4];
456         rData[0]=Result;
457         
458         PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
459 //        PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
460         SendPacket(nChn, KLPacketBuf2, PacketLen);
461         return Result;
462     }
463 //    if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
464 //    memcpy(KLPacketBuf2,pBuf,Len1);
465 //    SendPacket(nChn, KLPacketBuf2, PacketLen);
466     
467     Result=KLParseReqPacket(nChn, pBuf, Len1);
468     
469     //int len1=p1->PacketLen;
470 //    if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
471 //    pKBPacket p2=(pKBPacket)PacketBuf2;
472 //            Uart2Stat.OKPacket++;
473
474     return Result;
475 }
476