读取/检查DTC封装函数,覆盖了很多测试场景,也许对你有所帮助

Posted 蚂蚁小兵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读取/检查DTC封装函数,覆盖了很多测试场景,也许对你有所帮助相关的知识,希望对你有一定的参考价值。

系列用的CANoe演示工程我放在了Git上,不定时根据博客更新。

CANoe-Demn

相对路径:CANoe-Demo\\TestModule\\DtcFuncTest


前言


文章目录


工程配置说明

  • 本节演示代码基于XML Node 测试,源码会放在博客开头的Git 链接上

检测诊断响应中是否只有某个DTC(唯一性),且检测DTC状态


  • 以下是xml_test_test.xml中的代码,只有一个case
  • 检查 是否只有 DTC 0xD43581 且状态码是0x2B

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="DTC_Func_Test" version="">
		<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Status is 0x2B">
			<caplparam type = "string" name = "TestType">OnlyDTC</caplparam>
		</capltestcase>
</testmodule>


  • 以下是XmlCaseTest.can 中的代码,上面的xml代码会调用这个case
  • TestStep_ReadDTC() 函数在 ArrayFuncTest.cin 文件中,这是检测DTC的核心函数

/*@!Encoding:936*/
includes
 
 #include "ArrayFuncTest.cin"


testcase ReadDTC_Test(char TestType[])  
  
  int TStep_i ;
  char Tstep[10];
  long wait_time = 100;//unit :ms
  diagRequest FaultMemory_Clear FaultMemory_Clear;
  diagSetTarget("FCM"); // should set your diagnostic target first 
  
  DiagSendRequest(FaultMemory_Clear);
  testWaitForTimeout(2000);
  
  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F5); //trigger dct 0xD43581  
  //setRawSignal(RLCR_1_SysSt ,0x03);//trigger dct 0x540F00
  testWaitForTimeout(2000);
  
  TStep_i  = 1;
	snprintf(Tstep, elcount(Tstep), "%d", TStep_i);
   
  if (strncmp(TestType,"OnlyDTC",elCount(TestType)) == 0)
  
    TestStep_ReadDTC(Tstep,0xD43581,0x2B,gcTrue,gcUnique);
  
  
  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F4); //untrigger dct 0xD43581  
  //setRawSignal(RLCR_1_SysSt ,0x01);//untrigger dct 0x540F00
  testWaitForTimeout(2000);


  • 以下是 ArrayFuncTest.cin 文件中的主要代码,其中TestStep_ReadDTC 是主要函数。
  • 由于代码占用过长,全部源码去git下载,
  • 后面的演示,将不再贴出这个函数的源码


/***************************************************************************************************
----------------------------------------------------------------------------------------------------                                                                                                 
 Function name:   TestStep_ReadDTC_Only

检测是否有且仅有指定的DTC出现在Response中
----------------------------------------------------------------------------------------------------                                                                                                 
 Parameters:      
----------------------------------------------------------------------------------------------------                                                                                                 
Author:   
  CSDN - MaYiXiaoBing
  https://blog.csdn.net/qq_34414530?type=blog 
 ----------------------------------------------------------------------------------------------------                                                                                                  
Date: 2022/05/01            
----------------------------------------------------------------------------------------------------                                                                                                  
***************************************************************************************************/

int TestStep_ReadDTC(char Tstep[],dword expDTC,byte expStatus,byte isExpected, byte unique)

    byte subStepId;
    char tempText[255];
    char signalName[100];
    int retVal;    
    long retSize ,DTC_Num;
    int i ;
    dword Byte_H,Byte_M,Byte_L,ResDTC[100];
    byte ResDTC_Status[100];
    diagRequest  *diagReq;
    
  
    // 初始化数组为0
    InitArray(ResDTC);
    InitArray(ResDTC_Status);
  
    if (isExpected == gcTrue)
       
      if(unique == gcUnique)
        snprintf(tempText,elcount(tempText),"Send $19 02 %X ,Except Only DTC 0x%X finded In Response", dtc_mask,expDTC);
      else
        snprintf(tempText,elcount(tempText),"Send $19 02 %X ,Except DTC 0x%X finded In Response", dtc_mask,expDTC);
      
      if (expStatus != 0x00)
        snprintf(tempText,elcount(tempText),"%s And Status Is 0x%x.", tempText,expStatus);
      else
        snprintf(tempText,elcount(tempText),"%s And Status Not Care.", tempText);   
    
    
    else
         
       snprintf(tempText,elcount(tempText),"Send $19 02 %X ,Except DTC 0x%X Not finded In Response.", dtc_mask,expDTC);
    
            
    subStepId = 0;
    snprintf(Tstep,elcount(Tstep),"%s.%d",Tstep,subStepId);
    TestStep(Tstep, tempText);
    
    // 发送诊断读取DTC
    retVal = SendReadDTCDiagnostic(ResDTC, ResDTC_Status,DTC_Num ,WaitForResopnse);
      
    if(retVal != 1)
    
      snprintf(tempText,elcount(tempText),"Response Code is %d,Not Expected.", retVal);
      testStepFail(Tstep, tempText);
      return gcNok;
    
    
    /********************期望DTC存在***************************/  
    if (isExpected == gcTrue) 
             
      retVal = SeachValueInArrary(expDTC,ResDTC);      
      if(retVal != gcNok)// Find value
         
        snprintf(tempText,elcount(tempText),"DTC 0x%X Finded In Response,It is Excepted.", expDTC);
        /********************是否需要检查status******************************/ 
        if (expStatus != 0x00)  //需要检查status ,00为不需要检查status
        
            if(expStatus == ResDTC_Status[retVal])
          
            snprintf(tempText,elcount(tempText),"%s\\n And Status is 0x%X,It is Excepted.", tempText ,expStatus);
            testStepPass(Tstep, tempText);
          
          else
          
            snprintf(tempText,elcount(tempText),"%s\\n And Status is 0x%X,But Expect It's 0x%x.",tempText,ResDTC_Status[retVal], expStatus);
            testStepFail(Tstep, tempText);
            return gcNok;
          
        
         if(unique == gcUnique)
        
          if(DTC_Num != 1)
          
             snprintf(tempText,elcount(tempText),"DTC 0x%X In Response,But It's Not Unique.", expDTC);
             testStepFail(Tstep, tempText);
             return gcNok;
          
                                      
      
      else
       
        snprintf(tempText,elcount(tempText),"DTC 0x%X Not Finded In Response,It is Unexcepted.", expDTC);
        testStepFail(Tstep, tempText);
        return gcNok;          
      
    

    /********************不期望DTC存在******************************/
    else   
         
      retVal = SeachValueInArrary(expDTC,ResDTC);
      if(retVal == gcNok)//Not Find
      
        snprintf(tempText,elcount(tempText),"DTC 0x%X Not In Response,It is Excepted.", expDTC);
        testStepPass(Tstep, tempText);
      
      else
      
        snprintf(tempText,elcount(tempText),"Excepted DTC 0x%X Not In Response,But Actual In.", expDTC);
        testStepFail(Tstep, tempText);
        return gcNok;
      
        
   return gcOk;

测试结果如下图:


检测诊断响应中是否只有某个DTC(唯一性),不检测DTC状态


  • 以下是xml_test_test.xml中的代码,
  • 我们在上一个演示代码中继续添加代码

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="DTC_Func_Test" version="">
		<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Status is 0x2B">
			<caplparam type = "string" name = "TestType">OnlyDTC</caplparam>
		</capltestcase>
		<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Not Care Status">
			<caplparam type = "string" name = "TestType">OnlyDTC_NoStatus</caplparam>
		</capltestcase>	
</testmodule>


  • 以下是XmlCaseTest.can 中的代码,上面的xml代码会调用这个case
  • 我们在上一个演示代码中继续添加代码
  • TestStep_ReadDTC 函数的 expStatus 参数 为0 ,则我们默认为不需要检测这个DTCStatus
  • ArrayFuncTest.cin 文件代码不再参考上面已经贴出来的。

/*@!Encoding:936*/
includes

 
  #include "ArrayFuncTest.cin"


testcase ReadDTC_Test(char TestType[])  
  
  int TStep_i ;
  char Tstep[10];
  long wait_time = 100;//unit :ms
  diagRequest FaultMemory_Clear FaultMemory_Clear;
  diagSetTarget("FCM"); // should set your diagnostic target first 
  
  DiagSendRequest(FaultMemory_Clear);
	testWaitForTimeout(2000);
  
  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F5); //trigger dct 0xD43581  
  //setRawSignal(RLCR_1_SysSt ,0x03);//trigger dct 0x540F00
  testWaitForTimeout(2000);
  
  TStep_i  = 1;
	snprintf(Tstep, elcount(Tstep), "%d", TStep_i);
   
  if (strncmp(TestType,"OnlyDTC",elCount(TestType)) == 0)
  
    TestStep_ReadDTC(Tstep,0xD43581,0x2B,gcTrue,gcUnique);
  
  else if(strncmp(TestType,"OnlyDTC_NoStatus",elCount(TestType)) == 0)
  
     TestStep_ReadDTC(Tstep,0xD43581,NotCareStatus,gcTrue,gcUnique);
  

  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F4); //untrigger dct 0xD43581  
  //setRawSignal(RLCR_1_SysSt ,0x01);//untrigger dct 0x540F00
  testWaitForTimeout(2000);


测试报告展示:



检测诊断响应中是否有某个DTC(不唯一性)


  • 由于前面两种场景已经展示过 Status的逻辑,下面展示不再关注 DTC Status 的检测(统一需要检测)
  • 以下是xml_test_test.xml中的代码,
  • 我们在上一个演示代码中继续添加代码
  • 不唯一性是说,我们只要在诊断响应中检测到该DTC就OK,并不Care 是否只有这个DTC

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="DTC_Func_Test" version="">
		<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Status is 0x2B">
			<caplparam type = "string" name = "TestType">OnlyDTC</caplparam>
		</capltestcase>
		<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Not Care Status">
			<caplparam type = "string" name = "TestType">OnlyDTC_NoStatus</caplparam>
		</capltestcase>	
		<capltestcase name="ReadDTC_Test" title = "Ckeck DTC 0xD43581 In And Status is 0x2B">
			<caplparam type = "string" name = "TestType">IncludeDTC</caplparam>
		</capltestcase>
		<capltestcase name="ReadDTC_Test" title = "Ckeck DTC 0xD43581 In And Not Care Status">
			<caplparam type = "string" name = "TestType">IncludeDTC_NoStatus</caplparam>
		</capltestcase>											
				
</testmodule>

  • 以下是XmlCaseTest.can 中的代码,上面的xml代码会调用这个case
  • 我们在上一个演示代码中继续添加代码
  • 这里和上面的演示代码注意,这里我们需要触发两个DTC,

/*@!Encoding:936*/
includes

 
  #include "ArrayFuncTest.cin"



testcase ReadDTC_Test(char TestType[])  
  
  int TStep_i ;
  char Tstep[10];
  long wait_time = 100;//unit :ms
  diagRequest FaultMemory_Clear FaultMemory_Clear;
  diagSetTarget("FCM"); // should set your diagnostic target first 
  
  DiagSendRequest(FaultMemory_Clear);
	testWaitForTimeout(2000);
  
  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F5); //trigger dct 0xD43581  
  setRawSignal(RLCR_1_SysSt ,0x03);//trigger dct 0x540F00
  testWaitForTimeout(2000);
  
  TStep_i  = 1;
	snprintf(Tstep, elcount(Tstep), "%d", TStep_i);
   
  if (strncmp(TestType,"OnlyDTC",elCount(TestType)) == 0)
  
    TestStep_ReadDTC(Tstep,0xD43581,0x2B,gcTrue,gcUnique);
  
  else if(strncmp(TestType,"OnlyDTC_NoStatus",elCount(TestType)) == 0)
  
     TestStep_ReadDTC(Tstep,0xD43581,NotCareStatus,gcTrue,gcUnique);
  
   else if(strncmp(TestType,"IncludeDTC",elCount(TestType)) == 0)
  
     TestStep_ReadDTC(Tstep,0xD43581,0x2B,gcTrue,gcNot_Unique);
  
    else if(strncmp(TestType,"IncludeDTC_NoStatus",elCount(TestType)) == 0)
  
     TestStep_ReadDTC(Tstep,0xD43581,NotCareStatus,gcTrue,gcNot_Unique);
  
  
  setRawSignal(RLCR_3_Le1ObjBoxLen ,0x1F4); //untrigger dct 0xD43581  
  setRawSignal(RLCR_1_SysSt ,0x01);//untrigger dct 0x540F00
  testWaitForTimeout(2000);


测试报告展示:

  • 因为我这里触发了两个DTC,如果去哦期望唯一性,则第一个 CASE Failed;如果我不期望唯一性,只要Response中有这个DTC就OK,所以第二个CASE Passed.

检测诊断响应中是否只有某个DTC数组(唯一性),且检测DTC状态


  • 以下是xml_test_test.xml中的代码,我们新建一个分组 DTC Arrary ,在CAPL中新建一个CASE ReadDTC_Arrary_Test

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<testmodule title="DTC_Func_Test" version="">
		<testgroup title="DTC Signal">
			<capltestcase name="ReadDTC_Test" title = "Ckeck Only DTC 0xD43581 In And Status is 0x2B">
				<caplparam type = "string" name = "TestType">OnlyDTC</caplparam>
			</capltestcase>
			<capltestcase name=以上是关于读取/检查DTC封装函数,覆盖了很多测试场景,也许对你有所帮助的主要内容,如果未能解决你的问题,请参考以下文章

19 06-读取DTC扩展数据记录

19 04-读取DTC快照信息

自己封装的poi操作Excel工具类

19 01-通过状态掩码读取DTC数目

利用JaCoCo统计接口测试中代码覆盖率

Linux-设备树编译器DTC