Exemplo n.º 1
0
def init(excel_name,sheet_name):
    # 第一次开始行数变量
    start_line_number = 11
    # 连接redis数据库
    pool = redis.ConnectionPool(host = '127.0.0.1',port=6379,db=0)
    try:
        r = redis.StrictRedis(connection_pool = pool)
    except:
        print("redis数据库连接错误")
        sys.exit(1)

    # 初始化消息
    r.set("test","stop")# 发送线程开始消息
    # r.set("status","start11")# 线程同步消息,线程交叉执行
    r.set("status","%s%s"%("start",str(start_line_number)))# 线程同步消息,线程交叉执行
    r.set("print_message_status","start")
    r.set("receive_flow_control_flag",0)
    # 判断数据库中有没有line_number数据,若有,先删除
    if r.get("line_number") != None:
        r.delete("line_number")

    if r.get("service_number") != None:
        r.delete("service_number")
    
    if r.get("service_subfunction") != None:
        r.delete("service_subfunction")

    try:
        # 实例化excel文件处理对象
        wb = xw.Book(excel_name)
        # 实例化具体的表格对象
        sht = wb.sheets(sheet_name)
        # 获取该表格的设置的用例数
        # number = int(sht.range("Q2").value)
    except:
        print("初始化读取excel表格错误")
        sys.exit(1)
    # 获取当天日期
    date_today = datetime.date.today()

    # print(number)
    # 初始化can通讯和运行can通讯
    try:
        ch = can.init()
        ch = can.run(ch)
    except:
        print("can卡启动错误")
        sys.exit(1)

    return [r,wb,sht,date_today,ch,start_line_number]
Exemplo n.º 2
0
def printMessage(threadName,
                 ch,
                 physical_id=0x79D,
                 function_id=0x760,
                 excel_name=None,
                 sheet_name=None):
    # 这个对象需要重新实例化
    wb = xw.Book(excel_name)
    sht = wb.sheets(sheet_name)
    # 保存接收到的不同的包地址的集合
    different_can_message_address_by_receive = set()
    # 初始化一个空字典,用于保存间隔时间计算结果
    init_dic = {}
    # 实例化uds服务对象
    uds = UdsLib(ch=ch, id=physical_id)
    while True:
        # 启动信号
        if str(r.get("print_message_status"), encoding="utf-8") == "start":
            try:
                frame = ch.read(timeout=500)
            except:
                # print("can通讯异常")
                ch = can.init()
                ch = can.run(ch)
                continue
                # break
            # print("{id:02x}  {dlc}  {data}  {timestamp}".format(
            #             id=frame.id,
            #             dlc=frame.dlc,
            #             data=' '.join('%02x' % i for i in frame.data),
            #             timestamp=frame.timestamp
            #         ))
            # 接收到响应报文
            if frame.id == response_id:
                # 向发送线程设置发送等待消息
                r.set("status", "wait")
                line_number = str(r.get("line_number"), encoding="utf-8")
                # print("{id:02x}  {dlc}  {data}  {timestamp}".format(
                #         id=frame.id,
                #         dlc=frame.dlc,
                #         data=' '.join('%02x' % i for i in frame.data),
                #         timestamp=frame.timestamp
                #     ))
                # time.sleep(1)
                # 对接收到的数据data转化成数据串
                data = str("{data}".format(data=' '.join('%02x' % i
                                                         for i in frame.data)))
                # 表格单元格赋值
                sht.range("%s%s" % (current_sheet_table_title_dic["响应数据"],
                                    line_number)).value = data
                # 分割刚转换的字符串数据,方便后期判断
                data = data.split(" ")
                # uds服务
                check_result = uds.check_message_status(
                    int(str(r.get("service_number"), encoding="utf-8")), data)
                if check_result:
                    if int(data[1], 16) == 0x67 and int(data[2], 16) % 2 == 1:
                        # 判断接收到了27服务请求种子的肯定响应
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["响应方式"],
                                   line_number)).value = "肯定响应"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["故障码类型"],
                                   line_number)).value = "-"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["OK/NG"],
                                   line_number)).value = "OK"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试人员"],
                                   line_number)).value = "huzhicheng"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试时间"],
                                   line_number)).value = date_today
                        # 将数据保存到表格
                        wb.save()
                        # 种子数组
                        seed = []
                        seed.append(int(data[3], 16))
                        seed.append(int(data[4], 16))
                        seed.append(int(data[5], 16))
                        seed.append(int(data[6], 16))
                        print("seed:", seed)
                        ch.write(
                            uds.security_access_calculate_key(id=physical_id,
                                                              seed=seed))
                    elif int(data[1],
                             16) == 0x67 and int(data[2], 16) % 2 == 0:
                        # 判断接收到了解锁肯定响应
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["备注"],
                                   line_number)).value = "解锁成功"
                        # 将数据保存到表格
                        wb.save()
                        # 向发送线程设置下一个报文发送开始消息
                        r.set("status",
                              "%s%s" % ("start", str(int(line_number) + 1)))
                    elif uds.continuous_status == True and int(
                            data[0],
                            16) & 0x0F == uds.TP.continuous_frame_number:
                        # 判断检测到连续帧的最后一帧,执行不同的保存数据内容
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["响应方式"],
                                   line_number)).value = "肯定响应"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["故障码类型"],
                                   line_number)).value = "-"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["OK/NG"],
                                   line_number)).value = "OK"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试人员"],
                                   line_number)).value = "huzhicheng"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试时间"],
                                   line_number)).value = date_today
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["响应数据"],
                                   line_number)).value = str(
                                       uds.TP.continuous_data)
                        print("continuous_data:", uds.TP.continuous_data)
                        print("continuous_data_number",
                              uds.TP.continuous_data_number)
                        print("continuous_frame_number",
                              uds.TP.continuous_frame_number)
                        # 将数据保存到表格
                        wb.save()
                        # 向发送线程设置下一个报文发送开始消息
                        r.set("status",
                              "%s%s" % ("start", str(int(line_number) + 1)))
                        # 重置连续帧标志
                        uds.continuous_status = False
                        # 重置uds.TP.continuous_data
                        uds.TP.continuous_data = []
                        pass
                    elif not uds.continuous_status:
                        # 一般的肯定响应
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["响应方式"],
                                   line_number)).value = "肯定响应"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["故障码类型"],
                                   line_number)).value = "-"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["OK/NG"],
                                   line_number)).value = "OK"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试人员"],
                                   line_number)).value = "huzhicheng"
                        sht.range("%s%s" %
                                  (current_sheet_table_title_dic["测试时间"],
                                   line_number)).value = date_today
                        # 将数据保存到表格
                        wb.save()
                        # 向发送线程设置下一个报文发送开始消息
                        r.set("status",
                              "%s%s" % ("start", str(int(line_number) + 1)))
                elif uds.NRC == 0x78:
                    uds.NRC = None
                    time.sleep(0.01)  # 等待10ms
                elif int(data[1], 16) == 0x7f:
                    sht.range("%s%s" % (current_sheet_table_title_dic["响应方式"],
                                        line_number)).value = "否定响应"
                    sht.range("%s%s" % (current_sheet_table_title_dic["故障码类型"],
                                        line_number)).value = "-"
                    sht.range("%s%s" % (current_sheet_table_title_dic["OK/NG"],
                                        line_number)).value = "OK"
                    sht.range("%s%s" % (current_sheet_table_title_dic["测试人员"],
                                        line_number)).value = "huzhicheng"
                    sht.range("%s%s" % (current_sheet_table_title_dic["测试时间"],
                                        line_number)).value = date_today
                    sht.range("%s%s" %
                              (current_sheet_table_title_dic["故障码类型"],
                               line_number)).value = uds.NRC_table[uds.NRC]
                    # 将数据保存到表格
                    wb.save()
                    # 向发送线程设置下一个报文发送开始消息
                    r.set("status",
                          "%s%s" % ("start", str(int(line_number) + 1)))
                else:  #既不是肯定响应也不是否定响应
                    # 判断s是否接收到ECU发来的流控帧
                    [flow_control_flag, flow_state, block_size,
                     STmin] = uds.TP.check_flow_control_frame(data)
                    if flow_control_flag:  # 判断检测到流控帧
                        r.set("receive_flow_control_flag", 1)
                # 打印响应报文
                print("{id:02x}  {dlc}  {data}  {timestamp}".format(
                    id=frame.id,
                    dlc=frame.dlc,
                    data=' '.join('%02x' % i for i in frame.data),
                    timestamp=frame.timestamp))
                # wb.close()
            # print("执行中")
            else:
                """
                第一种使用time库计算报文间隔时间的方法,该方法不怎么准
                """
                # 以下代码用于计算包间隔时间
                # id = "{id:02x}".format(id = frame.id)
                # if id not in different_can_message_address_by_receive:
                #     # 将包id字符串添加入different_can_message_address_by_receive集合中
                #     different_can_message_address_by_receive.add(id)
                #     begin_time = time.time()
                #     dic = {"begin_time":begin_time,"run_time":0.0}
                #     init_dic[id] = dic
                # # elif float(str(r.hget(id,"run_time"),encoding="utf8")) == 0.0:
                # elif init_dic[id]["run_time"] == 0.0:
                #     end_time = time.time()
                #     run_time = end_time - init_dic[id]["begin_time"]
                #     init_dic[id]["run_time"] = run_time
                # print(init_dic)
                """
                第二种使用接收到包的时间戳计算报文间隔时间的方法,建议使用该方法
                """
                id = "{id:02x}".format(id=frame.id)
                if id not in different_can_message_address_by_receive:
                    # 将包id字符串添加入different_can_message_address_by_receive集合中
                    different_can_message_address_by_receive.add(id)
                    dic = {
                        "begin_time":
                        float("{timestamp}".format(timestamp=frame.timestamp)),
                        "run_time":
                        0.0
                    }
                    init_dic[id] = dic
                elif init_dic[id]["run_time"] == 0.0:
                    run_time = float(
                        "{timestamp}".format(timestamp=frame.timestamp
                                             )) - init_dic[id]["begin_time"]
                    init_dic[id]["run_time"] = run_time
                    print(init_dic)
                # print("{id:02x}  {dlc}  {data}  {timestamp}".format(
                #         id=frame.id,
                #         dlc=frame.dlc,
                #         data=' '.join('%02x' % i for i in frame.data),
                #         timestamp=frame.timestamp
                #     ))
        else:
            break
Exemplo n.º 3
0
sht = wb.sheets("1_1_DiagAndCommMgtFuncUnit")

number = int(sht.range("Q2").value)

print(number)

# send_data = sht.range("K11").value
# 字符串分割
# send_data = send_data.split(" ")

# print(send_data)

# for n in range(len(send_data)):
#     send_data[n] = int(send_data[n],16)

ch = can.init()

ch = can.run(ch)

# frame = Frame(id_=0x79D,
#             data=[send_data[0],send_data[1],
#              send_data[2],send_data[3], send_data[4], send_data[5],
#              send_data[6],send_data[7]],
#             dlc=8,
#             flags=0)

# wb.close()

# for j in range(5):
#     # frame = Frame(id_=0x79D,
#     #             data=[0x02,0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00],
Exemplo n.º 4
0
 def open(self):
     self.ch = can.init()
     self.ch = can.run(ch)
Exemplo n.º 5
0
def printMessage(threadName, ch, physical_id=0x79D, function_id=0x760):
    # 这个对象需要重新实例化
    wb = xw.Book("./uds_test2.xlsx")
    sht = wb.sheets("1_1_DiagAndCommMgtFuncUnit")
    # 保存接收到的不同的包地址的集合
    different_can_message_address_by_receive = set()
    # 初始化一个空字典,用于保存间隔时间计算结果
    init_dic = {}
    # 实例化uds服务对象
    uds = UdsLib(ch, physical_id)
    while True:
        # 启动信号
        if str(r.get("print_message_status"), encoding="utf-8") == "start":
            try:
                frame = ch.read(timeout=500)
            except:
                # print("can通讯异常")
                ch = can.init()
                ch = can.run(ch)
                continue
                # break
            # print("{id:02x}  {dlc}  {data}  {timestamp}".format(
            #             id=frame.id,
            #             dlc=frame.dlc,
            #             data=' '.join('%02x' % i for i in frame.data),
            #             timestamp=frame.timestamp
            #         ))
            # 接收到响应报文
            if frame.id == response_id:
                # 向发送线程设置发送等待消息
                r.set("status", "wait")
                line_number = str(r.get("line_number"), encoding="utf-8")
                # print("{id:02x}  {dlc}  {data}  {timestamp}".format(
                #         id=frame.id,
                #         dlc=frame.dlc,
                #         data=' '.join('%02x' % i for i in frame.data),
                #         timestamp=frame.timestamp
                #     ))
                # time.sleep(1)
                # 对接收到的数据data转化成数据串
                data = str("{data}".format(data=' '.join('%02x' % i
                                                         for i in frame.data)))
                # 表格单元格赋值
                sht.range("%s%s" % ("N", line_number)).value = data
                # 分割刚转换的字符串数据,方便后期判断
                data = data.split(" ")
                # uds服务
                check_result = uds.check_message_status(
                    int(str(r.get("service_number"), encoding="utf-8")), data)
                if check_result:
                    if int(data[1], 16) == 0x67 and int(data[2], 16) % 2 == 1:
                        # 判断接收到了27服务请求种子的肯定响应
                        sht.range("%s%s" % ("L", line_number)).value = "肯定响应"
                        sht.range("%s%s" % ("M", line_number)).value = "-"
                        sht.range("%s%s" % ("O", line_number)).value = "OK"
                        sht.range("%s%s" %
                                  ("P", line_number)).value = "huzhicheng"
                        sht.range("%s%s" %
                                  ("Q", line_number)).value = date_today
                        # 将数据保存到表格
                        wb.save()
                        # 种子数组
                        seed = []
                        seed.append(int(data[3], 16))
                        seed.append(int(data[4], 16))
                        seed.append(int(data[5], 16))
                        seed.append(int(data[6], 16))
                        print("seed:", seed)
                        ch.write(
                            uds.security_access_calculate_key(id=physical_id,
                                                              seed=seed))
                    elif int(data[1],
                             16) == 0x67 and int(data[2], 16) % 2 == 0:
                        # 判断接收到了解锁肯定响应
                        sht.range("%s%s" % ("S", line_number)).value = "解锁成功"
                        # 将数据保存到表格
                        wb.save()
                        # 向发送线程设置下一个报文发送开始消息
                        r.set("status",
                              "%s%s" % ("start", str(int(line_number) + 1)))
                    else:
                        # 一般的肯定响应
                        sht.range("%s%s" % ("L", line_number)).value = "肯定响应"
                        sht.range("%s%s" % ("M", line_number)).value = "-"
                        sht.range("%s%s" % ("O", line_number)).value = "OK"
                        sht.range("%s%s" %
                                  ("P", line_number)).value = "huzhicheng"
                        sht.range("%s%s" %
                                  ("Q", line_number)).value = date_today
                        # 将数据保存到表格
                        wb.save()
                        # 向发送线程设置下一个报文发送开始消息
                        r.set("status",
                              "%s%s" % ("start", str(int(line_number) + 1)))
                    # sht.range("%s%s" % ("L",line_number)).value = "肯定响应"
                    # sht.range("%s%s" % ("M",line_number)).value = "-"
                    # sht.range("%s%s" % ("O",line_number)).value = "OK"
                    # sht.range("%s%s" % ("P",line_number)).value = "huzhicheng"
                    # sht.range("%s%s" % ("Q",line_number)).value = date_today
                    # print(data)
                    # time.sleep(1)
                    # 将数据保存到表格
                    # wb.save()
                    # 向发送线程设置下一个报文发送开始消息
                    # r.set("status","%s%s" % ("start",str(int(line_number)+1)))
                elif uds.NRC == 0x78:
                    uds.NRC = None
                    time.sleep(0.01)  # 等待10ms
                else:
                    sht.range("%s%s" % ("L", line_number)).value = "否定响应"
                    sht.range("%s%s" % ("M", line_number)).value = "-"
                    sht.range("%s%s" % ("O", line_number)).value = "OK"
                    sht.range("%s%s" % ("P", line_number)).value = "huzhicheng"
                    sht.range("%s%s" % ("Q", line_number)).value = date_today
                    sht.range(
                        "%s%s" %
                        ("M", line_number)).value = uds.NRC_table[uds.NRC]
                    # 将数据保存到表格
                    wb.save()
                    # 向发送线程设置下一个报文发送开始消息
                    r.set("status",
                          "%s%s" % ("start", str(int(line_number) + 1)))

                # wb.close()
            # print("执行中")
            else:
                """
                第一种使用time库计算报文间隔时间的方法,该方法不怎么准
                """
                # 以下代码用于计算包间隔时间
                # id = "{id:02x}".format(id = frame.id)
                # if id not in different_can_message_address_by_receive:
                #     # 将包id字符串添加入different_can_message_address_by_receive集合中
                #     different_can_message_address_by_receive.add(id)
                #     begin_time = time.time()
                #     dic = {"begin_time":begin_time,"run_time":0.0}
                #     init_dic[id] = dic
                # # elif float(str(r.hget(id,"run_time"),encoding="utf8")) == 0.0:
                # elif init_dic[id]["run_time"] == 0.0:
                #     end_time = time.time()
                #     run_time = end_time - init_dic[id]["begin_time"]
                #     init_dic[id]["run_time"] = run_time
                # print(init_dic)
                """
                第二种使用接收到包的时间戳计算报文间隔时间的方法,建议使用该方法
                """
                id = "{id:02x}".format(id=frame.id)
                if id not in different_can_message_address_by_receive:
                    # 将包id字符串添加入different_can_message_address_by_receive集合中
                    different_can_message_address_by_receive.add(id)
                    dic = {
                        "begin_time":
                        float("{timestamp}".format(timestamp=frame.timestamp)),
                        "run_time":
                        0.0
                    }
                    init_dic[id] = dic
                elif init_dic[id]["run_time"] == 0.0:
                    run_time = float(
                        "{timestamp}".format(timestamp=frame.timestamp
                                             )) - init_dic[id]["begin_time"]
                    init_dic[id]["run_time"] = run_time
                    print(init_dic)
        else:
            break