UDS的诊断测试如果采用手动测试,不仅效率低,并且测试结果达不到预期效果,如果采用Vector的的直带的Diva工程,需要购买另外的Linsice,成本太高。本章的测试就是为了脱离Diva来实现UDS诊断的自动化测试。
首先第一步我们需要在CANOE软件中加入CDD文件 然后打开Test Setup创XMLTestModule,加载XML,如图所示;
XML代码如下:
<testmodule title="诊断函数测试" version="1.1">
<externalref type="url" title="CSDN蚂蚁小兵">
https://blog.csdn.net/qq_34414530
</externalref>
<testgroup title="TestGroup_1">
<capltestcase name="TC_001" title="TC_001"/>
<capltestcase name="TC_002" title="TC_002"/>
<capltestcase name="TC_003" title="TC_003"/>
<capltestcase name="TC_0033" title="TC_0033"/>
<capltestcase name="TC_004" title="TC_004"/>
<capltestcase name="TC_005" title="TC_005"/>
<capltestcase name="TC_006" title="TC_006"/>
<capltestcase name="TC_007" title="TC_007"/>
<capltestcase name="TC_008" title="TC_008"/>
<capltestcase name="TC_009" title="TC_009"/>
<capltestcase name="TC_010" title="TC_010"/>
<capltestcase name="TC_011" title="TC_011"/>
<capltestcase name="TC_012" title="TC_012"/>
<capltestcase name="TC_013" title="TC_013"/>
<capltestcase name="TC_014" title="TC_014"/>
</testgroup>
<testgroup title="UDS测试">
<capltestcase name="TC_UDS_001" title="UDS_001 -10服务 会话切换测试"/>
<capltestcase name="TC_UDS_002" title="UDS_002-11服务,物理寻址,请求子功能函数不支持的11服务,期望ECU返回NRC12"/>
<capltestcase name="TC_UDS_003" title="UDS_003-11服务,功能寻址,请求子功能函数不支持的11服务,期望ECU无响应"/>
<capltestcase name="TC_UDS_004" title="UDS_003-3E服务,Suppress_Positive_Response_Suppression_Bit_TesterPresent_Send"/>
</testgroup>
</testmodule>
XML创建成功后就可以创建对应的CAN工程了;
这里先介绍UDS最基本的诊断发送测试;后续再根据ISO规范去规划一个完整的脱离DIva的UDS自动化测试;诊断的发送我们首先先建立一个自定义诊断发送函数,源码如下图所示:
void Send_DiagRequest_diag(diagrequest *DiagReq, long ExpResCode,byte SendType)
{
char tempStr[100];
long retVal,lastCode;
const long SendTimeout = 100;//ms
const long ReceiveTimeout = 1000;//ms
const long No_Response = 0;
const long PostitiveResponse = -1;
const long NrcResponse_12 = 12;
const long NrcResponse_13 = 13;
TestReportWriteDiagObject(DiagReq);
if (SendType == 1)
{
DiagSendFunctional(DiagReq);
}
else
{
DiagSendRequest(DiagReq);
}
if (TestWaitForDiagRequestSent(DiagReq, SendTimeout)== 1)
{
retVal = TestWaitForDiagResponse( DiagReq, ReceiveTimeout);
TestReportWriteDiagResponse(DiagReq);
switch( retVal)
{
case 0: // Timeout: The ECU did not respond .
if(ExpResCode == No_Response)
{
testStepPass(gTestIdStr,"No response received,as expected.");
}
else
{
TestStepFail(gTestIdStr,"Response was received when no response was expected.");
}
break;
case 1: // response received
lastCode = DiagGetLastResponseCode(DiagReq);
switch( lastCode)
{
case -1: // postival response
if(ExpResCode == PostitiveResponse)
{
testStepPass(gTestIdStr,"Positival response received, as expected.");
}
else
{
snprintf(tempStr,elcount(tempStr),"positival response was received when NRC 0x%x was expected.",ExpResCode);
TestStepFail(gTestIdStr,tempStr);
}
break;
default: // NRC response
if(ExpResCode == lastCode)
{
snprintf(tempStr,elcount(tempStr),"NRC 0x%x was received, as expected.",ExpResCode);
testStepPass(gTestIdStr,tempStr);
}
else
{
snprintf(tempStr,elcount(tempStr),"NRC 0x%x was received when Positival response was expected.",lastCode);
TestStepFail(gTestIdStr,tempStr);
}
}
break;
default: // error
snprintf(tempStr,elcount(tempStr),"there a Error code received = %.",retVal);
TestStepFail(gTestIdStr,tempStr);
}
}
else
{
TestStepFail(gTestIdStr,"Request could not be sent!");
}
}
这里的DiagReq指的是诊断请求,ExpResCode是期望结果, SendType指的是物理寻址还是功能寻址;
创建一个TC_001自定义的测试工程,发送诊断进行测试,如图所示;运行并打印测试结果;
testcase TC_001()
{
diagRequest DefaultSession_Start DefaultSession_Start; //1001
diagRequest ProgrammingSession_Start ProgrammingSession_Start; //1002
diagSetTarget(ECU_Name);
Send_DiagRequest_diag(DefaultSession_Start, PostitiveResponse , phy_diagRequest);
Send_DiagRequest_diag(ProgrammingSession_Start,NrcResponse_7E,phy_diagRequest);
}