提交 | 用户 | age
|
8b51c7
|
1 |
|
Q |
2 |
#include "SLP.h" |
|
3 |
|
|
4 |
void SLPSendPacket(char * buf, uchar len1); |
|
5 |
uchar bSLPMaster; |
|
6 |
uchar nStation; |
|
7 |
uchar SLPinputB; |
|
8 |
uchar SLPoutputB; |
|
9 |
uchar nCurStation; |
|
10 |
uchar inputBuf[5]; |
|
11 |
uchar outputBuf[5]; |
|
12 |
|
|
13 |
uchar SendBuf[8]; |
|
14 |
|
|
15 |
uchar SLPMasterRecved; //主机收到子机回复 |
|
16 |
unsigned int SLPSlaveCountOut; |
|
17 |
|
|
18 |
unsigned int SLPLostPkt=0; // 丢包数量 |
|
19 |
|
|
20 |
uchar SLPOKSign; |
|
21 |
uchar SLPErrSign; |
|
22 |
|
|
23 |
uchar SLPBCC(char * pBuf, uchar len1) |
|
24 |
{ |
|
25 |
uchar i; |
|
26 |
uchar BCC=0; |
|
27 |
for (i=0;i<len1;i++) |
|
28 |
{ |
|
29 |
BCC += pBuf[i]; |
|
30 |
} |
|
31 |
return BCC; |
|
32 |
} |
|
33 |
|
|
34 |
void SLPparsePacket(char * pRecvBuf, uchar len1) |
|
35 |
{ |
|
36 |
|
|
37 |
stSLPPacket * pPacket = (stSLPPacket *)pRecvBuf; |
|
38 |
if (len1 != sizeof(stSLPPacket)) return; |
|
39 |
// if (pPacket->ED != EDsign) return; |
|
40 |
if (pPacket->BCC != SLPBCC(pRecvBuf,len1-1)) return; |
|
41 |
if (bSLPMaster) //master |
|
42 |
{ |
|
43 |
if (pPacket->ST ==ST_S) |
|
44 |
{ |
|
45 |
//check |
|
46 |
if (pPacket->Dst == nCurStation) { |
|
47 |
SLPMasterRecved=1; |
|
48 |
SLPLostPkt=0; |
|
49 |
inputBuf[nCurStation] = pPacket->Data; |
|
50 |
} |
|
51 |
} |
|
52 |
// SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4); |
|
53 |
SLPoutputB = inputBuf[1]; |
|
54 |
}else |
|
55 |
{ //slave |
|
56 |
if (pPacket->ST==ST_M) |
|
57 |
{ |
|
58 |
//check |
|
59 |
stSLPPacket * pRplyPkt = (stSLPPacket *)SendBuf; |
|
60 |
if (pPacket->Dst == nStation) { |
|
61 |
SLPoutputB = pPacket->Data; |
|
62 |
SLPSlaveCountOut=0; |
|
63 |
|
|
64 |
pRplyPkt->ST = ST_S; |
|
65 |
pRplyPkt->Dst = nStation; |
|
66 |
pRplyPkt->Data = SLPinputB; |
|
67 |
pRplyPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); |
|
68 |
// pRplyPkt->ED = EDsign; |
|
69 |
|
|
70 |
SLPSendPacket(SendBuf,sizeof(stSLPPacket)); |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
void SLPMasterSendPacket(void) |
|
77 |
{ |
|
78 |
|
|
79 |
stSLPPacket * pReqPkt = (stSLPPacket *)SendBuf; |
|
80 |
outputBuf[1]=SLPinputB ;//&0xf; |
|
81 |
// outputBuf[2] = (SLPinputB & 0xf0) >> 4; |
|
82 |
pReqPkt->ST = ST_M; |
|
83 |
pReqPkt->Dst = nCurStation; |
|
84 |
pReqPkt->Data = outputBuf[nCurStation]; ; |
|
85 |
pReqPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); |
|
86 |
// pReqPkt->ED = EDsign; |
|
87 |
|
|
88 |
SLPSendPacket(SendBuf,sizeof(stSLPPacket)); |
|
89 |
} |
|
90 |
void SLPProcess(void) |
|
91 |
{ |
|
92 |
static int nCount =0; |
|
93 |
if (bSLPMaster) //master |
|
94 |
{ |
|
95 |
if ( (nCount & 0x3f) == 0 ) |
|
96 |
{ //time up |
|
97 |
if (SLPMasterRecved) { |
|
98 |
// SLPMasterRecved=0; |
|
99 |
SLPOKSign = 1; |
|
100 |
if (SLPErrSign) SLPErrSign--; |
|
101 |
|
|
102 |
}else { |
|
103 |
SLPLostPkt++; |
|
104 |
if (SLPLostPkt > 10) { |
|
105 |
SLPErrSign=20; |
|
106 |
SLPOKSign = 0; |
|
107 |
} |
|
108 |
} |
|
109 |
if (nStation >0) { |
|
110 |
nCurStation++; |
|
111 |
if (nCurStation > nStation) { |
|
112 |
nCurStation =1; |
|
113 |
} |
|
114 |
SLPMasterRecved=0; |
|
115 |
SLPMasterSendPacket(); |
|
116 |
} |
|
117 |
} |
|
118 |
}else |
|
119 |
{ |
|
120 |
SLPSlaveCountOut ++; |
|
121 |
if (SLPSlaveCountOut >200) // 20mS |
|
122 |
{ |
|
123 |
SLPErrSign=100; |
|
124 |
}else { |
|
125 |
if (SLPErrSign) SLPErrSign--; |
|
126 |
} |
|
127 |
} |
|
128 |
nCount++; |
|
129 |
} |