def init(excel_name, sheet_name): # 第一次开始行数变量 start_line_number = 11 # 初始化消息 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 [wb, sht, date_today, ch, start_line_number]
def socket(message): print("接受到消息:", message["data"]) if message["data"] == "start": r.set("message", "start") r.set("diagnostic_message", "stop") ch = can.init() ch = can.run(ch) # if message["data"] == "start": while True: if str(r.get("message"), encoding="utf8") == "stop": ch.busOff() ch.close() print(str(r.get("message"))) # socketio.emit("stop",{"data":"结束"},namespace="/Socket") break elif str(r.get("message"), encoding="utf8") == "start": result = can.getMessage(ch) socketio.emit("response", {"data": result}, namespace="/Socket") # socketio.emit("response",{"data":str(r.get("message"),encoding="utf8")},namespace="/Socket") elif str(r.get("message"), encoding="utf8") == "send": r.set("message", "start") can.sendMessage(ch) if str(r.get("diagnostic_message"), encoding="utf8") == "start": pass print("结束") elif message["data"] == "stop": #通过redis存储消息 r.set("message", "stop") elif message["data"] == "send": #通过redis存储消息 r.set("message", "send") elif message["data"] == "start_diagnostic": #通过redis存储开始诊断消息 # r.set("diagnostic_message","start") while True: if r.get("diagnostic_status") != None: if str(r.get("diagnostic_status"), "utf-8") == "finish": socketio.emit("diagnostic_status", {"data": "diagnostic_stop"}, namespace="/Socket") r.delete("diagnostic_status") break
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
def start(): ch = can.init() _thread.start_new_thread(can.run(ch)) return render_template("views/login.html")
def login(): ch = can.init() can.run(ch) return render_template("views/login.html")