| | |
| | | unsigned char Datas[128];
|
| | | volatile int PacketLength = 0;
|
| | |
|
| | | unsigned char BCC(void * pData, int nSize)
|
| | | unsigned char KBusBCC(void * pData, int nSize)
|
| | | {
|
| | | unsigned char k;
|
| | | k=0;
|
| | |
| | | return k;
|
| | | }
|
| | |
|
| | | int MakePacket(pPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
|
| | | int KBusMakePacket(pPacket p1,unsigned char src, uchar dst, uchar nType,unsigned char nSEQ, unsigned char DataLen,void * pData )
|
| | | {
|
| | | p1->Sign=StartSign;
|
| | | p1->DstHost=dst;
|
| | |
| | | case cmdPing:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | |
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | |
| | | case cmdPingReply:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | |
| | | case cmdWriteReply:
|
| | | p1->PacketLen=DataLen;
|
| | | if (DataLen !=0 ) memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdGetVersion:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdVerInfo:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdExChgData:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdExChgDataReply:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | |
| | | case cmdSyncRead:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdSyncWrite:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | | case cmdSequenRead:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | |
| | | case cmdSyncTime:
|
| | | p1->PacketLen=DataLen;
|
| | | memcpy(p1->data,pData,DataLen);
|
| | | p1->data[DataLen]=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen]=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | p1->data[DataLen+1]=EndSign;
|
| | | PacketLenth=sizeof(stPacket)+DataLen+1;
|
| | | break;
|
| | |
| | | return PacketLenth;
|
| | | }
|
| | |
|
| | | int CheckPacket(int nChn, pPacket p1, int nLen1)
|
| | | int KBusCheckPacket(int nChn, pPacket p1, int nLen1)
|
| | | {
|
| | | if (p1->Sign != StartSign)
|
| | | {
|
| | |
| | | // Uart2Stat.LengthErr++;
|
| | | // return -2;
|
| | | // }
|
| | | unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | unsigned char thisBCC=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | if (thisBCC != p1->data[DataLen])
|
| | | {//BCC Error;
|
| | | Uart2Stat.BCCerr++;
|
| | |
| | | return 0;
|
| | | }
|
| | |
|
| | | int SlaveCheckPacket(int nChn, pPacket p1, int nLen1)
|
| | | int KBusSlaveCheckPacket(int nChn, pPacket p1, int nLen1)
|
| | | {
|
| | | if (p1->Sign != StartSign)
|
| | | {
|
| | |
| | | // Uart2Stat.LengthErr++;
|
| | | // return -2;
|
| | | // }
|
| | | unsigned char thisBCC=BCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | unsigned char thisBCC=KBusBCC(p1,sizeof(stPacket)+DataLen-1);
|
| | | if (thisBCC != p1->data[DataLen])
|
| | | {//BCC Error;
|
| | | Uart2Stat.BCCerr++;
|
| | |
| | | return 0;
|
| | | }
|
| | |
|
| | | int MasterParsePacket(int nChn, pPacket p1, int Len1)
|
| | | int KBusMasterParsePacket(int nChn, pPacket p1, int Len1)
|
| | | {
|
| | |
|
| | | int DataLen=p1->PacketLen;
|
| | |
| | | break;
|
| | | case cmdPing:
|
| | | BufferIn[ChildId]=p1->data[0];
|
| | | PacketLen=MakePacket(p2,0,nCurPollId,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
| | | PacketLen=KBusMakePacket(p2,0,nCurPollId,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
| | | SendPacket(nChn, p2, PacketLen);
|
| | | break;
|
| | | case cmdPingReply:
|
| | |
| | | //PutOutput(outputvalue);
|
| | | //memcpy(DispBuf,p1->data+2,8);
|
| | | p1->data[0]=BufferOut[0];
|
| | | PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
| | | PacketLen=KBusMakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
| | | SendPacket(nChn, p2, PacketLen);
|
| | | break;
|
| | | case cmdExChgDataReply:
|
| | |
| | | return 0;
|
| | | }
|
| | | unsigned char nClientDataIndex=0;
|
| | | int SlaveParsePacket(int nChn, pPacket p1, int Len1)
|
| | | int KBusSlaveParsePacket(int nChn, pPacket p1, int Len1)
|
| | | {
|
| | | Uart2Stat.OKPacket++;
|
| | | int DataLen=p1->PacketLen;
|
| | |
| | | //memcpy(DispBuf,p1->data+2,8);
|
| | | p1->data[0]=BufferOut[0];
|
| | | RecvTimeuS=ThisuS;
|
| | | PacketLen=MakePacket(p2,nAddr,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
| | | PacketLen=KBusMakePacket(p2,nAddr,0,cmdPingReply,p1->nSEQ,DataLen,p1->data);
|
| | | ChnStats[0].ClientSendPkts++;
|
| | | SendPacket(nChn, p2, PacketLen);
|
| | | break;
|
| | |
| | | break;
|
| | | case cmdWrite:
|
| | | //memcpy(DispBuf,p1->data,DataLen);
|
| | | PacketLen=MakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0);
|
| | | PacketLen=KBusMakePacket(p2,1,0,cmdWriteReply,p1->nSEQ,0,0);
|
| | | ChnStats[0].ClientSendPkts++;
|
| | | SendPacket(nChn, p2, PacketLen);
|
| | | break;
|
| | |
| | | p1->data[7]=ChnStats[0].ClientDatas[nIndex]>>24;
|
| | | nClientDataIndex++;
|
| | | if (nClientDataIndex >= 10) { nClientDataIndex=0;}
|
| | | PacketLen=MakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
| | | PacketLen=KBusMakePacket(p2,nAddr,0,cmdExChgDataReply,p1->nSEQ,DataLen,p1->data);
|
| | | ChnStats[0].ClientSendPkts++;
|
| | | SendPacket(nChn, p2, PacketLen);
|
| | | break;
|
| | |
| | | }
|
| | | return 0;
|
| | | }
|
| | | int ParsePacket(int nChn, pPacket p1, int Len1)
|
| | | int KBusParsePacket(int nChn, pPacket p1, int Len1)
|
| | | {
|
| | | ThisuS=GetuS();
|
| | | int Result=0;
|
| | | if (bKBusMaster)
|
| | | {
|
| | | MasterRecved=1;
|
| | | Result=CheckPacket(nChn, p1, Len1);
|
| | | Result=KBusCheckPacket(nChn, p1, Len1);
|
| | | if (Result != S_OK)
|
| | | {
|
| | | return Result;
|
| | | }
|
| | | MasterRecvOK=1;
|
| | | Result=MasterParsePacket(nChn, p1, Len1); |
| | | Result=KBusMasterParsePacket(nChn, p1, Len1); |
| | | return Result;
|
| | | }
|
| | | if (bKBusSlave)
|
| | | {
|
| | | ChnStats[0].ClientRecvPkts++;
|
| | | Result=SlaveCheckPacket(nChn, p1, Len1);
|
| | | Result=KBusSlaveCheckPacket(nChn, p1, Len1);
|
| | | if (Result != S_OK)
|
| | | {
|
| | | return Result;
|
| | | }
|
| | | ChnStats[0].ClientTimeOutErr=KMem.RunStat;
|
| | | Result=SlaveParsePacket(nChn, p1, Len1);
|
| | | Result=KBusSlaveParsePacket(nChn, p1, Len1);
|
| | | return Result;
|
| | | }
|
| | | //int len1=p1->PacketLen;
|
| | |
| | |
|
| | | return Result;
|
| | | }
|
| | |
|
| | | /*
|
| | | int InitMachine(stMachineConfig * pConfig)
|
| | | {
|
| | | return S_OK;
|
| | |
| | | {
|
| | | return S_OK;
|
| | | }
|
| | |
|
| | | */
|
| | | int KBusRepeaterFunc(int nChn)
|
| | | {
|
| | | KMem.WY[0]=KMem.WX[0];
|
| | |
| | | Datas[7]=(tick1>>24)&0xff;
|
| | |
|
| | | SendTimeuS=thisuS;
|
| | | len1=MakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
|
| | | len1=KBusMakePacket((pPacket)PacketBuf1,0,nCurPollId,cmdExChgData,nSeq,8,Datas);
|
| | | SendPacket(nChn, (pPacket)PacketBuf1, len1);
|
| | | ChnStats[nCurPollId].SendPackets++;
|
| | | ChnStats[nCurPollId].SendTimeInterval=SendTimeuS-ChnStats[nCurPollId].LastSentTimeuS;
|