/*XW_LT2.c : Demo for forwarding different CAN Data application. Project: XW_LT2.c Detail description: When upac-5001D-CAN2 receives specific CAN-Data (0x11, 0x22, ...) from one CAN channel, it will change CAN data(0xFF, 0xFF....), and then send them out to another CAN channel. Hardware: upac-5001D-CAN2 [May,24,2016] by Ives*/ #include "XWCAN.h" #include "upac5000.h" #include /*If the UserCANInt() function is not used, please don't remove it.*/ void UserCANInt(int CANPort,char CANInt){ if(CANInt) ; /*avoid complier warning*/ } //unsigned char MsgMode=0,MsgRTR=0,MsgDataLen=0,MsgData[8]={0,0,0,0,0,0,0,0},tmpMsgData[8]={0,0,0,0,0,0,0,0}; void main(void) { int ret,i; unsigned char MsgMode=0,MsgRTR=0,MsgDataLen=8,MsgData[8]={0,0,0,0,0,0,0,0}; unsigned long MsgID,MsgUpperTimeStamp,MsgLowerTimeStamp,MsgCnt=0 ; InitLib(); /********************************************************/ /* Initial all LEDs */ /********************************************************/ CL1Off(); CL2Off(); CL3Off(); /*******************************************************/ /* initialize and configure the CAN controller */ /*******************************************************/ ret=XWCANInit(0,3,1000000UL,0,0,0x00000000UL,0xffffffffUL); switch (ret){ /*Check if configuration is OK*/ case CAN_ResetError: Print("Reset Error!\n\r"); return; case CAN_SetACRError: Print("\n\rSet ACR Error!"); return; case CAN_SetAMRError: Print("\n\rSet AMR Error!"); return; case CAN_SetBaudRateError: Print("\n\rSet Baud Rate Error!"); return; case CAN_BaudNotSupport: Print("\n\rBaud Rate Not Support!"); return; case CAN_ConfigError: Print("\n\rConfiguration Failure!"); return; default: if (ret >0){ Print("Default=%d\n\r",ret); return; } } CAN_CreateBuffer(0,0,1000); //Create TX Buffer Port0 CAN_CreateBuffer(0,1,1000); //Create TX Buffer Port1 CAN_InstallIrq(0); ret=XWCANInit(1,3,1000000UL,0,0,0x00000000UL,0xffffffffUL); switch (ret){ //Check if configuration is OK case CAN_ResetError: Print("Reset Error!\n\r"); return; case CAN_SetACRError: Print("\n\rSet ACR Error!"); return; case CAN_SetAMRError: Print("\n\rSet AMR Error!"); return; case CAN_SetBaudRateError: Print("\n\rSet Baud Rate Error!"); return; case CAN_BaudNotSupport: Print("\n\rBaud Rate Not Support!"); return; case CAN_ConfigError: Print("\n\rConfiguration Failure!"); return; default: if (ret >0){ Print("Default=%d\n\r",ret); return; } } CAN_CreateBuffer(1,0,1000); //Create TX Buffer Port1 CAN_CreateBuffer(1,1,1000); //Create RX Buffer Port0 CAN_InstallIrq(1); Print("Press any key to exit the program.\n\r"); MsgData[0]=0x0; MsgData[1]=0x0; MsgData[2]=0x0; MsgData[3]=0x0; MsgData[4]=0x0; MsgData[5]=0x0; MsgData[6]=0x0; MsgData[7]=0x0; //MsgID = 0x12343000; //MsgID = 0x0; MsgID =0x0; while(1){ ret=GetCANMsg(0,&MsgMode,&MsgID,&MsgRTR,&MsgDataLen,MsgData,&MsgUpperTimeStamp,&MsgLowerTimeStamp); if(!ret){ CL1On(); if(MsgData[0]==0x11&&MsgData[1]==0x22&&MsgData[2]==0x33&&MsgData[3]==0x44&&MsgData[4]==0x55&&MsgData[5]==0x66&&MsgData[6]==0x77&&MsgData[7]==0x88) { MsgData[0]=0xff; MsgData[1]=0xff; MsgData[2]=0xff; MsgData[3]=0xff; MsgData[4]=0xff; MsgData[5]=0xff; MsgData[6]=0xff; MsgData[7]=0xff; SendCANMsg(1,MsgMode,MsgID,MsgRTR,MsgDataLen,MsgData); } else SendCANMsg(1,MsgMode,MsgID,MsgRTR,MsgDataLen,MsgData); DelayMs(10); CL1Off(); } ret=GetCANMsg(1,&MsgMode,&MsgID,&MsgRTR,&MsgDataLen,MsgData,&MsgUpperTimeStamp,&MsgLowerTimeStamp); if(!ret){ CL2On(); if(MsgData[0]==0x11&&MsgData[1]==0x22&&MsgData[2]==0x33&&MsgData[3]==0x44&&MsgData[4]==0x55&&MsgData[5]==0x66&&MsgData[6]==0x77&&MsgData[7]==0x88) { MsgData[0]=0xff; MsgData[1]=0xff; MsgData[2]=0xff; MsgData[3]=0xff; MsgData[4]=0xff; MsgData[5]=0xff; MsgData[6]=0xff; MsgData[7]=0xff; SendCANMsg(0,MsgMode,MsgID,MsgRTR,MsgDataLen,MsgData); } else SendCANMsg(0,MsgMode,MsgID,MsgRTR,MsgDataLen,MsgData); DelayMs(10); CL2Off(); } if (Kbhit()){ /*if press any key, exit the program*/ Print("Exit this program!\n"); break; } } Print("-------------------------\n\r"); CAN_Restore(0); CAN_Restore(1); }