Python+Robot Framework實現UDS診斷自動化測試


一、環境搭建

1.概述

由於項目需要進行UDS診斷測試,所以對這方面進行了研究學習,網上很少能查詢到相關資料,故記錄一下UDS自動化測試開發過程,由於保密原則,案例都是Demo,希望能幫到感興趣的朋友。

 

2.硬件環境

上位機:PCAN

PCAN-USB驅動:https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip

下位機:ECM(發動機控制模塊)

 

3.Python環境

下載地址:https://www.python.org/ftp/python/3.7.9/python-3.7.9-amd64.exe

pip3 install robotframework==3.2.2

pip3 install robotframework-ride==1.7.4.2

pip3 install xlrd==1.2.0

pip3 install udsoncan==1.14

pip3 install python-can==3.3.4

pip3 install can-isotp==1.7

 

二、項目介紹

1.文件目錄

 

  $10--$3E:L2層robot測試用例

  Public.robot:L1層關鍵字方法

  UDS_TestReport.zip:自動化測試報告

  udstest.py:python封裝自定義uds測試方法

  UDSTestcase.xlsx:UDS診斷測試用例

 

2.udstest.py

# _*_ coding:utf-8 _*_

from can.interfaces.pcan.pcan import PcanBus
from udsoncan.connections import PythonIsoTpConnection
import xlrd, os, udsoncan, isotp, sys, binascii


class udstest(object):
    def __init__(self):
        udsoncan.setup_logging()  # udslog

    def get_xlsx(self, sheet):
        "獲取指定Excel數據"
        excel = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'UDSTestcase.xlsx')  # 獲取用例文件路徑
        file = xlrd.open_workbook(excel)
        list = []
        sheet = file.sheet_by_name(sheet)  # 獲得指定sheet數據
        row_value1 = sheet.row_values(0)  # 獲取第1行的標題
        nrows = sheet.nrows  # 獲取當前sheet行數
        ncols = sheet.ncols  # 獲取當前sheet列數
        for i in range(1, nrows):  # 從第2行遍歷當前sheet
            row = sheet.row_values(i)  # 獲取行數據
            dict = {}  # 創建空字典
            for j in range(0, ncols):  # 遍歷sheet列,組成字典
                if row_value1[j] == 'NO.':
                    dict[row_value1[j]] = int(row[j])
                else:
                    dict[row_value1[j]] = row[j]  # 從第一列開始,將每一列的數據與第1行的數據組成一個鍵值對,形成字典
            list.append(dict)  # 將字典添加list中
        return list

    def set_can(self, txid, rxid):
        """can總線相關配置"""
        if isinstance(txid, str) or isinstance(rxid, str):
            txid = eval(txid)
            rxid = eval(rxid)
        isotp_params = {
            'stmin': 5,  # 流控幀間隔時間,0-127ms 或 100-900ns 值從 0xF1-0xF9
            'blocksize': 0,  # 流控幀單包大小,0表示不限制
            'tx_padding': 0,  # 當 notNone表示用於填充發送的消息的字節。
            'rx_flowcontrol_timeout': 1000,  # 在停止接收和觸發之前等待流控制幀的毫秒數
            'rx_consecutive_frame_timeout': 1000,  # 在停止接收和觸發 a 之前等待連續幀的毫秒數
        }
        try:
            self.canbus = PcanBus(channel='PCAN_USBBUS1', bitrate=500000)  # CAN總線初始化
            self.tp_addr = isotp.Address(isotp.AddressingMode.Normal_29bits, txid=txid, rxid=rxid)  # 網絡層尋址方法
            tp_stack = isotp.CanStack(bus=self.canbus, address=self.tp_addr, params=isotp_params)  # 網絡/傳輸層(IsoTP 協議)
            self.conn = PythonIsoTpConnection(tp_stack)  # 應用層和傳輸層之間建立連接

        except:
            print(sys.exc_info()[1])
        else:
            print('CAN配置成功')

    def uds_request_respond(self, request_command):
        """發送uds請求和接收uds響應"""
        if not isinstance(request_command, str):  # 判斷request_command數據類型
            request_command = str(int(request_command))
        requestPdu = binascii.a2b_hex(request_command.replace(' ', ''))  # 處理request_command
        if not self.conn.is_open():
            self.conn.open()  # 打開連接
        try:
            self.conn.specific_send(requestPdu)  # 發送uds請求
        except:
            print("發送請求失敗")
        else:
            print('UDS發送請求:%s' % request_command)

        try:
            respPdu = self.conn.specific_wait_frame(timeout=3)  # 接收uds響應
        except:
            print('響應數據失敗')
        else:
            res = respPdu.hex().upper()
            respond = ''
            for i in range(len(res)):
                if i % 2 == 0:
                    respond += res[i]
                else:
                    respond += res[i] + ' '
            print('UDS響應結果:%s' % respond)
            self.conn.close()  # 關閉連接
            self.canbus.shutdown()  # 關閉總線
            return respond.strip()

 

3.UDSTestcase.xlsx

 

 

3.UDS_TestReport

 

三、源碼地址

GitHub:https://github.com/xiongye105554598/UDS_AutoTest.git

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM