| | |
| | | |
| | | #include "SLP.h" |
| | | |
| | | void SLPSendPacket(char * buf, uchar len1); |
| | | uchar bSLPMaster; |
| | | uchar nStation; |
| | | uchar SLPinputB; |
| | | uchar SLPoutputB; |
| | | uchar nCurStation; |
| | | uchar inputBuf[5]; |
| | | uchar outputBuf[5]; |
| | | |
| | | uchar SendBuf[8]; |
| | | |
| | | uchar SLPMasterRecved; //主机收到子机回复 |
| | | unsigned int SLPSlaveCountOut; |
| | | |
| | | unsigned int SLPLostPkt=0; // 丢包数量 |
| | | |
| | | uchar SLPOKSign; |
| | | uchar SLPErrSign; |
| | | //void SLPSendPacket(char * buf, uchar len1); |
| | | |
| | | uchar SLPBCC(char * pBuf, uchar len1) |
| | | { |
| | |
| | | return BCC; |
| | | } |
| | | |
| | | void SLPparsePacket(char * pRecvBuf, uchar len1) |
| | | void SLPInit(stSLPDef * pSLP, SLPSendPktDef pFunc1) |
| | | { |
| | | pSLP->SLPLostPkt = 0; |
| | | pSLP->nCount = 0; |
| | | pSLP->SLPSendPktFunc = pFunc1; |
| | | |
| | | } |
| | | |
| | | void SLPSetCallBack(stSLPDef * pSLP, SLPSendPktDef pFunc1) |
| | | { |
| | | pSLP->SLPSendPktFunc = pFunc1; |
| | | } |
| | | |
| | | void SLPparsePacket(stSLPDef * pSLP, char * pRecvBuf, uchar len1) |
| | | { |
| | | |
| | | stSLPPacket * pPacket = (stSLPPacket *)pRecvBuf; |
| | | if (len1 != sizeof(stSLPPacket)) return; |
| | | // if (pPacket->ED != EDsign) return; |
| | | if (pPacket->BCC != SLPBCC(pRecvBuf,len1-1)) return; |
| | | if (bSLPMaster) //master |
| | | if (pSLP->bSLPMaster) //master |
| | | { |
| | | if (pPacket->ST ==ST_S) |
| | | { |
| | | //check |
| | | if (pPacket->Dst == nCurStation) { |
| | | SLPMasterRecved=1; |
| | | SLPLostPkt=0; |
| | | inputBuf[nCurStation] = pPacket->Data; |
| | | if (pPacket->Dst == pSLP->nCurStation) { |
| | | pSLP->SLPMasterRecved=1; |
| | | pSLP->SLPLostPkt=0; |
| | | pSLP->inputBuf[pSLP->nCurStation] = pPacket->Data; |
| | | } |
| | | } |
| | | // SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4); |
| | | SLPoutputB = inputBuf[1]; |
| | | pSLP->SLPoutputB = pSLP->inputBuf[1]; |
| | | }else |
| | | { //slave |
| | | if (pPacket->ST==ST_M) |
| | | { |
| | | //check |
| | | stSLPPacket * pRplyPkt = (stSLPPacket *)SendBuf; |
| | | if (pPacket->Dst == nStation) { |
| | | SLPoutputB = pPacket->Data; |
| | | SLPSlaveCountOut=0; |
| | | stSLPPacket * pRplyPkt = (stSLPPacket *)pSLP->SendBuf; |
| | | if (pPacket->Dst == pSLP->nStation) { |
| | | pSLP->SLPoutputB = pPacket->Data; |
| | | pSLP->SLPSlaveCountOut=0; |
| | | |
| | | pRplyPkt->ST = ST_S; |
| | | pRplyPkt->Dst = nStation; |
| | | pRplyPkt->Data = SLPinputB; |
| | | pRplyPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); |
| | | pRplyPkt->Dst = pSLP->nStation; |
| | | pRplyPkt->Data = pSLP->SLPinputB; |
| | | pRplyPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); |
| | | // pRplyPkt->ED = EDsign; |
| | | |
| | | SLPSendPacket(SendBuf,sizeof(stSLPPacket)); |
| | | pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | void SLPMasterSendPacket(void) |
| | | void SLPMasterSendPacket(stSLPDef * pSLP) |
| | | { |
| | | |
| | | stSLPPacket * pReqPkt = (stSLPPacket *)SendBuf; |
| | | outputBuf[1]=SLPinputB ;//&0xf; |
| | | stSLPPacket * pReqPkt = (stSLPPacket *)pSLP->SendBuf; |
| | | pSLP->outputBuf[1]=pSLP->SLPinputB ;//&0xf; |
| | | // outputBuf[2] = (SLPinputB & 0xf0) >> 4; |
| | | pReqPkt->ST = ST_M; |
| | | pReqPkt->Dst = nCurStation; |
| | | pReqPkt->Data = outputBuf[nCurStation]; ; |
| | | pReqPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); |
| | | pReqPkt->Dst = pSLP->nCurStation; |
| | | pReqPkt->Data = pSLP->outputBuf[pSLP->nCurStation]; ; |
| | | pReqPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); |
| | | // pReqPkt->ED = EDsign; |
| | | |
| | | SLPSendPacket(SendBuf,sizeof(stSLPPacket)); |
| | | pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); |
| | | } |
| | | void SLPProcess(void) |
| | | |
| | | void SLPProcess(stSLPDef * pSLP) |
| | | { |
| | | static int nCount =0; |
| | | if (bSLPMaster) //master |
| | | if (pSLP->bSLPMaster) //master |
| | | { |
| | | if ( (nCount & 0xf) == 0 ) |
| | | if ( (pSLP->nCount & 0xf) == 0 ) |
| | | { //time up |
| | | if (SLPMasterRecved) { |
| | | if (pSLP->SLPMasterRecved) { |
| | | // SLPMasterRecved=0; |
| | | SLPOKSign = 1; |
| | | if (SLPErrSign) SLPErrSign--; |
| | | pSLP->SLPOKSign = 1; |
| | | if (pSLP->SLPErrSign) pSLP->SLPErrSign--; |
| | | |
| | | }else { |
| | | SLPLostPkt++; |
| | | if (SLPLostPkt > 10) { |
| | | SLPErrSign=20; |
| | | SLPOKSign = 0; |
| | | pSLP->SLPLostPkt++; |
| | | if (pSLP->SLPLostPkt > 10) { |
| | | pSLP->SLPErrSign=20; |
| | | pSLP->SLPOKSign = 0; |
| | | } |
| | | } |
| | | if (nStation >0) { |
| | | nCurStation++; |
| | | if (nCurStation > nStation) { |
| | | nCurStation =1; |
| | | if (pSLP->nStation >0) { |
| | | pSLP->nCurStation++; |
| | | if (pSLP->nCurStation > pSLP->nStation) { |
| | | pSLP->nCurStation =1; |
| | | } |
| | | SLPMasterRecved=0; |
| | | SLPMasterSendPacket(); |
| | | pSLP->SLPMasterRecved=0; |
| | | SLPMasterSendPacket(pSLP); |
| | | } |
| | | } |
| | | }else |
| | | { |
| | | SLPSlaveCountOut ++; |
| | | if (SLPSlaveCountOut >200) // 20mS |
| | | pSLP->SLPSlaveCountOut ++; |
| | | if (pSLP->SLPSlaveCountOut >200) // 20mS |
| | | { |
| | | SLPErrSign=100; |
| | | pSLP->SLPErrSign=100; |
| | | }else { |
| | | if (SLPErrSign) SLPErrSign--; |
| | | if (pSLP->SLPErrSign) pSLP->SLPErrSign--; |
| | | } |
| | | } |
| | | nCount++; |
| | | pSLP->nCount++; |
| | | } |