You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将CAPL Node的评估结果写入行列受限的矩阵替代write窗口输出

CAPL评估结果写入固定行列矩阵的实现方案

方案1:CAPL脚本内定义本地矩阵存储

这是最直接的实现方式,不需要修改CANoe工程配置,只需要在原有代码基础上新增矩阵定义和写入逻辑即可。

步骤1:全局区定义固定大小矩阵

在脚本顶部全局变量区新增如下定义,可根据你的实际需求调整最大行数、列数:

/* 矩阵参数配置 */
const long MAX_EVAL_ROW = 10000; // 矩阵最大存储行数,按需调整
const long EVAL_COL_NUM = 3;     // 数值列数:对应A值、A_TEMP值、时间差3个数值字段
matrix<long, MAX_EVAL_ROW, EVAL_COL_NUM> g_evalValueMatrix; // 数值存储矩阵
matrix<char[24], MAX_EVAL_ROW> g_evalTimeMatrix; // 时间字符串存储矩阵
long g_currentWriteRow = 0; // 当前写入行索引

步骤2:修改消息回调逻辑,新增矩阵写入

原有write输出逻辑保留,新增矩阵写入和超限处理逻辑,修改后的代码如下:

on message 0x4490 
{
  char timeBuffer[24];
  if(this.A == 2 && A_TEMP == 3)
  {
    diff3_0_A = timeNowint64();  
    addTimeToMeasurementStartTime(timeNowNS(), time);
    // 格式化时间字符串
    snprintf(timeBuffer, elcount(timeBuffer), "%02d/%02d/%02d %02d:%02d:%02d.%-3d",
             time[5]+1, time[4], time[6]-100, time[3], time[2], time[1], time[0]);
    // 原有write窗口输出保留
    write(" %s               Aa                               %d                               %d                            %d ms",
          timeBuffer, this.A, A_TEMP, diff3_0_A - diff0_3_A);
    
    /* 矩阵写入逻辑 */
    if(g_currentWriteRow < MAX_EVAL_ROW)
    {
      // 未超行限制直接写入
      strncpy(g_evalTimeMatrix[g_currentWriteRow], timeBuffer, elcount(g_evalTimeMatrix[g_currentWriteRow]));
      g_evalValueMatrix[g_currentWriteRow][0] = this.A;
      g_evalValueMatrix[g_currentWriteRow][1] = A_TEMP;
      g_evalValueMatrix[g_currentWriteRow][2] = (long)(diff3_0_A - diff0_3_A);
      g_currentWriteRow++;
    }
    else
    {
      // 超行限制处理:二选一即可
      // 选项1:循环覆盖最早的行,始终保留最新的10000条数据
      for(long i = 0; i < MAX_EVAL_ROW - 1; i++)
      {
        strncpy(g_evalTimeMatrix[i], g_evalTimeMatrix[i+1], elcount(g_evalTimeMatrix[i]));
        g_evalValueMatrix[i][0] = g_evalValueMatrix[i+1][0];
        g_evalValueMatrix[i][1] = g_evalValueMatrix[i+1][1];
        g_evalValueMatrix[i][2] = g_evalValueMatrix[i+1][2];
      }
      strncpy(g_evalTimeMatrix[MAX_EVAL_ROW - 1], timeBuffer, elcount(g_evalTimeMatrix[MAX_EVAL_ROW - 1]));
      g_evalValueMatrix[MAX_EVAL_ROW - 1][0] = this.A;
      g_evalValueMatrix[MAX_EVAL_ROW - 1][1] = A_TEMP;
      g_evalValueMatrix[MAX_EVAL_ROW - 1][2] = (long)(diff3_0_A - diff0_3_A);

      // 选项2:停止写入,仅需注释掉选项1的代码,打开下一行注释即可
      // writeWarning("评估矩阵已达最大行数,停止写入新数据");
    }
  }
}

步骤3:可选新增数据持久化导出

如果需要把矩阵数据永久保存,可新增测量停止时的导出逻辑,自动把数据写入CSV文件:

on stopMeasurement
{
  long fileHandle;
  fileHandle = openFileWrite("评估结果导出.csv", 0);
  if(fileHandle != 0)
  {
    // 写入表头
    fileWriteString(fileHandle, "时间,A值,A_TEMP,时间差(ms)\n");
    // 遍历所有有效数据行写入
    for(long i = 0; i < g_currentWriteRow; i++)
    {
      fileWriteString(fileHandle, "%s,%d,%d,%d\n",
        g_evalTimeMatrix[i],
        g_evalValueMatrix[i][0],
        g_evalValueMatrix[i][1],
        g_evalValueMatrix[i][2]);
    }
    closeFile(fileHandle);
    write("矩阵数据已导出到工程目录下的评估结果导出.csv");
  }
}

方案2:CANoe系统变量矩阵存储

如果需要在CANoe面板上实时展示矩阵数据、或者其他CAPL节点也需要读取评估数据,可以选择用系统变量矩阵存储:

  1. 在CANoe工程的System Variables界面新建矩阵类型的系统变量,设置对应行列数、数据类型
  2. 在CAPL中调用SysSetVariableMatrixValue()接口写入对应位置的数据即可,使用逻辑和本地矩阵基本一致。

注意事项

  • CAPL中matrix定义时的行列数必须是常量,不能用动态变量声明
  • 若存储的字段包含非数值类型,需要单独定义对应类型的矩阵存储
  • 超限处理逻辑可根据业务需求选择,需要保留最新数据选环形覆盖,需要保留最早数据选停止写入

内容的提问来源于stack exchange,提问作者MDM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 07:15:02