提交 | 用户 | 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;
|
|
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:
|
Q |
295 |
|
|
296 |
break;
|
|
297 |
case KLCmdReadProgram:
|
0fe6b0
|
298 |
nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
|
a7db3c
|
299 |
DataLen= p1->Params[2];
|
Q |
300 |
if (nDataType==0){
|
0fe6b0
|
301 |
pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
|
a7db3c
|
302 |
} else if (nDataType==1){
|
0fe6b0
|
303 |
pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
|
a7db3c
|
304 |
} else if (KMRunStat.nBinProgBank == 0) {
|
0fe6b0
|
305 |
pData = (unsigned short *)STORE_PRG_BASE + nWordAddr;
|
a7db3c
|
306 |
}else {
|
0fe6b0
|
307 |
pData = (unsigned short *)ALT_PRG_BASE + nWordAddr;
|
a7db3c
|
308 |
}
|
Q |
309 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
|
|
310 |
SendPacket(nChn, p2, PacketLen);
|
|
311 |
|
|
312 |
break;
|
|
313 |
case KLCmdStartProgram:
|
|
314 |
DataLen=nDataType;
|
|
315 |
// KLBufferIn[0]=p1->Params[0];
|
|
316 |
// p1->Params[0]=KLBufferOut[0];
|
|
317 |
if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
|
|
318 |
|
|
319 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdStartProgram,0,0);
|
|
320 |
SendPacket(nChn, p2, PacketLen);
|
|
321 |
|
|
322 |
break;
|
|
323 |
case KLCmdWriteProgram:
|
|
324 |
if (PLCMem.bPLCRunning) PLCMem.bPLCRunning=0;
|
0fe6b0
|
325 |
nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
|
a7db3c
|
326 |
DataLen= p1->Params[2];
|
Q |
327 |
|
|
328 |
//DataLen=16;
|
|
329 |
for (int i=0;i<DataLen;i++)
|
|
330 |
{KLPacketBuf2[i]=p1->Params[4+i];}
|
0fe6b0
|
331 |
WriteProgram(nWordAddr, KLPacketBuf2, DataLen,nDataType);
|
a7db3c
|
332 |
DataLen=4;
|
Q |
333 |
*((int *)(&rData[0]))=(long)(p1->Params+4);
|
|
334 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdWriteProgram,0,0);
|
|
335 |
SendPacket(nChn, p2, PacketLen);
|
|
336 |
break;
|
|
337 |
case KLCmdFinishProgram:
|
0fe6b0
|
338 |
nWordAddr=p1->Params[0]+ (p1->Params[1]<<8); //Program Size;
|
a7db3c
|
339 |
DataLen=nDataType;
|
0fe6b0
|
340 |
KMRunStat.nBinProgSize=nWordAddr;
|
a7db3c
|
341 |
if (KMRunStat.nBinProgBank ==0) {KMRunStat.nBinProgBank=1;}
|
Q |
342 |
else {KMRunStat.nBinProgBank=0;}
|
|
343 |
SaveRunStat(&KMRunStat);
|
|
344 |
PLCMem.bPLCRunning=1;
|
|
345 |
// KLBufferIn[0]=p1->Params[0];
|
|
346 |
// p1->Params[0]=KLBufferOut[0];
|
|
347 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdFinishProgram,0,0);
|
|
348 |
SendPacket(nChn, p2, PacketLen);
|
|
349 |
break;
|
0fe6b0
|
350 |
|
Q |
351 |
case KLCmdBlinkLED:
|
|
352 |
DataLen=nDataType;
|
|
353 |
KMRunStat.bLEDFlick=DataLen;
|
|
354 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
|
|
355 |
SendPacket(nChn, p2, PacketLen);
|
|
356 |
break;
|
|
357 |
|
|
358 |
case KLCmdStopBlinkLED:
|
|
359 |
DataLen=nDataType;
|
|
360 |
KMRunStat.bLEDFlick=0;
|
|
361 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
|
|
362 |
SendPacket(nChn, p2, PacketLen);
|
|
363 |
break;
|
|
364 |
|
a7db3c
|
365 |
case KLCmdReadRunStat:
|
Q |
366 |
DataLen= sizeof(stRunStat);
|
|
367 |
pData=&KMRunStat;
|
|
368 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
|
|
369 |
SendPacket(nChn, p2, PacketLen);
|
0fe6b0
|
370 |
break;
|
Q |
371 |
case KLCmdReadSysCfg:
|
|
372 |
nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
|
|
373 |
pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
|
|
374 |
DataLen = p1->Params[2];
|
|
375 |
if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
|
|
376 |
else {
|
|
377 |
if (DataLen + nByteAddr >sizeof(stKMSysCfg))
|
|
378 |
DataLen= sizeof(stKMSysCfg) - nByteAddr;
|
|
379 |
}
|
|
380 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
|
|
381 |
SendPacket(nChn, p2, PacketLen);
|
|
382 |
break;
|
|
383 |
case KLCmdWriteSysCfg:
|
|
384 |
nByteAddr=p1->Params[0]+ (p1->Params[1]<<8);
|
|
385 |
pData=(UCHAR *)(&storedKMSysCfg.theKMSysCfg)+nByteAddr;
|
|
386 |
DataLen = p1->Params[2];
|
|
387 |
if (nByteAddr >= sizeof(stKMSysCfg)) {DataLen=0;}
|
|
388 |
else {
|
|
389 |
if (DataLen + nByteAddr >sizeof(stKMSysCfg))
|
|
390 |
DataLen= sizeof(stKMSysCfg) - nByteAddr;
|
|
391 |
}
|
|
392 |
memcpy(pData,p1->Params+4,DataLen);
|
|
393 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
|
|
394 |
SendPacket(nChn, p2, PacketLen);
|
a7db3c
|
395 |
break;
|
bfc108
|
396 |
case KLCmdSaveSysCfg:
|
a7db3c
|
397 |
WriteSysCfgToFlash(&storedKMSysCfg);
|
Q |
398 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveSysCfg,0,0);
|
|
399 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
400 |
break;
|
Q |
401 |
case KLCmdSaveRunStat:
|
|
402 |
SaveRunStat(&KMRunStat);
|
a7db3c
|
403 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdSaveRunStat,0,0);
|
Q |
404 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
405 |
break;
|
0fe6b0
|
406 |
case KLCmdClearRunStat:
|
Q |
407 |
memset(KBusChnStats,0,sizeof(KBusChnStats));
|
|
408 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,0,0);
|
|
409 |
SendPacket(nChn, p2, PacketLen);
|
|
410 |
break;
|
bfc108
|
411 |
case KLCmdGetEventLogCount:
|
Q |
412 |
DataLen= 4;
|
|
413 |
pData=&KMem.nEventCount;
|
a7db3c
|
414 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
|
Q |
415 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
416 |
break;
|
Q |
417 |
case KLCmdGetEventLog:
|
0fe6b0
|
418 |
nWordAddr=p1->Params[0]+ (p1->Params[1]<<8);
|
bfc108
|
419 |
DataLen= p1->Params[2] * sizeof(stEventLog);
|
0fe6b0
|
420 |
pData=GetEventLogAddr(nWordAddr);
|
a7db3c
|
421 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,p1->nCMD,DataLen,pData);
|
Q |
422 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
423 |
break;
|
Q |
424 |
case KLCmdClearEventLog:
|
|
425 |
ClearEventLog();
|
a7db3c
|
426 |
PacketLen=KLMakeRplyPacket(p2,nDstHost,nKLStatus.StatByte,KLCmdClearEventLog,0,0);
|
Q |
427 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
428 |
break;
|
Q |
429 |
default:
|
|
430 |
DataLen=1;
|
|
431 |
rData[0]=KL_UNKNOWN;
|
0fe6b0
|
432 |
PacketLen=KLMakeRplyPacket(p2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
|
a7db3c
|
433 |
SendPacket(nChn, p2, PacketLen);
|
bfc108
|
434 |
break;
|
Q |
435 |
}
|
|
436 |
}
|
|
437 |
return 0;
|
|
438 |
}
|
a7db3c
|
439 |
int KLParsePacket(int nChn, void * pBuf, int Len1)
|
bfc108
|
440 |
{
|
a7db3c
|
441 |
// pKLReqPktHdr p1 = (pKLReqPktHdr)pBuf;
|
Q |
442 |
// KLThisuS=GetuS();
|
|
443 |
// pKLRplyPktHdr p2=(pKLRplyPktHdr)KLPacketBuf2;
|
bfc108
|
444 |
int PacketLen=Len1;
|
a7db3c
|
445 |
int Result=KLCheckPacket(nChn, pBuf, Len1);
|
bfc108
|
446 |
if (Result != KL_OK)
|
Q |
447 |
{
|
|
448 |
int DataLen=4;
|
|
449 |
char rData[4];
|
|
450 |
rData[0]=Result;
|
|
451 |
|
0fe6b0
|
452 |
PacketLen=KLMakeRplyPacket(KLPacketBuf2,nKLStationId,nKLStatus.StatByte,KLCmdErrRply,DataLen,rData);
|
bfc108
|
453 |
// PacketLen=KLMakePacket(p2,0,nAddr,cmdKLPing,p1->nSEQ,DataLen,rData);
|
a7db3c
|
454 |
SendPacket(nChn, KLPacketBuf2, PacketLen);
|
bfc108
|
455 |
return Result;
|
Q |
456 |
}
|
a7db3c
|
457 |
// if (bKBusMaster) Result=ParseMasterPacket(p1,Len1);
|
bfc108
|
458 |
// memcpy(KLPacketBuf2,pBuf,Len1);
|
a7db3c
|
459 |
// SendPacket(nChn, KLPacketBuf2, PacketLen);
|
bfc108
|
460 |
|
a7db3c
|
461 |
Result=KLParseReqPacket(nChn, pBuf, Len1);
|
bfc108
|
462 |
|
Q |
463 |
//int len1=p1->PacketLen;
|
a7db3c
|
464 |
// if (p1->DstHost!=255&&p1->DstHost!=2) return -3;
|
0fe6b0
|
465 |
// pKBPacket p2=(pKBPacket)PacketBuf2;
|
bfc108
|
466 |
// Uart2Stat.OKPacket++;
|
Q |
467 |
|
|
468 |
return Result;
|
|
469 |
}
|
|
470 |
|