
【CAPL】【拿来就能直接用系列】CAN报文路由测试一、前言二、使用方法三、完整代码四、改动五、运行一、前言最近写了几个CAPL程序,可以用来对CAN通信进行简单地测试。这篇文章介绍的是CAN报文路由测试。二、使用方法CANoe→ \to→Simulation→ \to→Simulation Setup→ \to→Insert Network Node点击Edit图标进行CAPL编辑三、完整代码以下为完整代码,进入CAPL编辑界面,直接粘上去就行/*@!Encoding:1252*/includes{}variables{// ========== Multi-task slots (max 10 messages) ==========longgTaskActive[10];// 0 = free, 1 = activelonggTaskTxChannel[10];// transmit channellonggTaskMsgId[10];// message IDbyte gTaskData[10][8];// message datalonggTaskDlc[10];// data length (DLC)longgTaskRxChannel[10];// receive check channellonggTaskChecked[10];// checked in current cycle: 0 = no, 1 = yeslonggTaskResult[10];// task result: 1 = OK, 0 = NG// ========== Batch-level parameters ==========longgBatchActive;// batch running: 0 = no, 1 = yeslonggBatchCycleMs;// batch transmit cycle (ms)longgBatchDurationMs;// batch total duration (ms)// ========== Timers ==========msTimer gCycleTimer;// periodic transmit timermsTimer gDurationTimer;// total duration timermsTimer gCheckTimeoutTimer;// single-check timeout timer}// ============================================================================// Function: SendAndCheckMessage// Description:// Adds a message to the batch task list for periodic transmission and check.// Call multiple times to add multiple messages, then start the batch.//// Parameters:// txChannel - transmit channel (Channel X)// msgId - message ID// msgData[] - message data content (byte array)// dataLength - message DLC (0-8)// rxChannel - receive check channel (Channel Y)// cycleMs - message transmit cycle (ms)// durationMs - total transmit duration (ms)//// Returns:// 0 = failed (invalid parameters or no free slot)// 1 = success, batch auto-started============================================================================longSendAndCheckMessage(longtxChannel,longmsgId,byte msgData[],longdataLength,longrxChannel,longcycleMs,longdurationMs){longi;longslot;// ========== Parameter validation ==========if(dataLength0||dataLength8){write("ERROR: DLC out of range (0-8), got %d",dataLength);return0;}if(txChannel1||rxChannel1){write("ERROR: Channel number must be = 1");return0;}if(cycleMs=0){write("ERROR: Cycle time must be 0 ms");return0;}if(durationMs=0){write("ERROR: Duration must be 0 ms");return0;}// ========== Allocate a free task slot ==========slot=-1;for(i=0;i10;i++){if(gTas