前期,我已经编写过一篇《CANanlystII 基于linux的二次开发实践》这篇博客承接上一篇博客,所以背景知识和测试场景,就不再赘述。
背景知识和测试场景,可以查阅如下:
CANanlystII 基于linux的二次开发实践_晓翔仔的博客-CSDN博客_can分析仪二次开发
基于python的二次开发实践
设计步骤如下:
step0xf1: wakeup
step0x01: enter extension diagConversation
step0x03: update condition
step0x05: DTC OFF
step0x06: forbid transfer
step0x07: ask seed
step0x71: send key
step0x08: enter codeConversation
step0x09: ask seed
step0x91: send key
之后可以进行0x34 download步骤,也可以进行0x35 upload步骤
OK,步骤已经清楚了,那我们直接上代码:
# python3.8.0 64位(python 32位要用32位的DLL)
#
# 结构体数组类
import ctypes
from ctypes import *
import threading
import time
VCI_USBCAN2 = 4
STATUS_OK = 1
NO_RES_COUNT_MAX = 5 # 等待响应的时间(周期)上限
STEP = 0xf1 # 添加了一个唤醒步骤
CURRENT_STEP = 0X00 # 当前所处的CAN数据发送和接收的步骤,“接收线程“会根据”当前步骤”和“CAN数据内容”判断是否是正确响应
can_id = 0x777 # ECU的CANID,每个ECU不一样
fun_id = 0x7df # 功能寻址的CANID,固定值
seed = 0 # UDS 0X27,ECU返回过来的seed
seed_size = 0 # UDS 0X27,ECU返回过来的seed位数,一般是2或者4
# 全局变量
TestMode = 'true' # 是否是测试模式,测试模式时,CAN1 CAN2直连。非测试模式时CAN2不使用。
# TestMode = 'false'
class VCI_INIT_CONFIG(Structure):
_fields_ = [("AccCode", c_uint),
("AccMask", c_uint),
("Reserved", c_uint),
("Filter", c_ubyte),
("Timing0", c_ubyte),
("Timing1", c_ubyte),
("Mode", c_ubyte)
]
class VCI_CAN_OBJ(Structure):
_fields_ = [("ID", c_uint),
("TimeStamp", c_uint),
("TimeFlag", c_ubyte),
("SendType", c_ubyte),
("RemoteFlag", c_ubyte),
("ExternFlag", c_ubyte),
("DataLen", c_ubyte),
("Data", c_ubyte * 8),
("Reserved", c_ubyte * 3)
]
class VCI_CAN_OBJ_ARRAY(Structure):
_fields_ = [('SIZE', ctypes.c_uint16), ('STRUCT_ARRAY', ctypes.POINTER(VCI_CAN_OBJ))]
def __init__(self, num_of_structs):
# 这个括号不能少
self.STRUCT_ARRAY = ctypes.cast((VCI_CAN_OBJ * num_of_structs)(), ctypes.POINTER(VCI_CAN_OBJ)) # 结构体数组
self.SIZE = num_of_structs # 结构体长度
self.ADDR = self.STRUCT_ARRAY[0] # 结构体数组地址 byref()转c地址
def list_to_hex_string(list_data):
list_str = '[ '
for x in list_data:
list_str += '0x{:02X},'.format(x)
list_str += ' ]'
return list_str
def can_init_and_start():
ret_open_device = canDLL.VCI_OpenDevice(VCI_USBCAN2, 0, 0)
if ret_open_device == STATUS_OK:
print('调用 VCI_OpenDevice成功')
if ret_open_device != STATUS_OK:
print('调用 VCI_OpenDevice出错')
# 初始0通道
vci_init_config = VCI_INIT_CONFIG((can_id + 8) << 21, 0x00000000, 0,
0, 0x00, 0x1C, 0) # 波特率125 Kbps 0x03 0x1C ||| 波特率500 Kbps 0x00 0x1C
ret_can0_init = canDLL.VCI_InitCAN(VCI_USBCAN2, 0, 0, byref(vci_init_config))
if ret_can0_init == STATUS_OK:
print('调用 VCI_InitCAN1成功')
if ret_can0_init != STATUS_OK:
print('调用 VCI_InitCAN1出错')
ret_can0_start = canDLL.VCI_StartCAN(VCI_USBCAN2, 0, 0)
if ret_can0_start == STATUS_OK:
print('调用 VCI_StartCAN1成功')
if ret_can0_start != STATUS_OK:
print('调用 VCI_StartCAN1出错')
if TestMode == 'true':
# 初始1通道
ret_can1_init = canDLL.VCI_InitCAN(VCI_USBCAN2, 0, 1, byref(vci_init_config))
if ret_can1_init == STATUS_OK:
print('调用 VCI_InitCAN2 成功')
if ret_can1_init != STATUS_OK:
print('调用 VCI_InitCAN2 出错')
ret_can1_start = canDLL.VCI_StartCAN(VCI_USBCAN2, 0, 1)
if ret_can1_start == STATUS_OK:
print('调用 VCI_StartCAN2 成功')
if ret_can1_start != STATUS_OK:
print('调用 VCI_StartCAN2 出错')
# 通道1发送数据
def send_can1():
ubyte_array = c_ubyte * 8
ubyte_3array = c_ubyte * 3
NoResCount = 0
global STEP
global CURRENT_STEP
global can_id
global seed_size
global key_k
while 'true':
# step0xf1: wakeup
if STEP == 0xF1:
a = ubyte_array(0x50, 0x14, 0x02, 0x20, 0x44, 0xF1, 0x2F, 0x15)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(0x121, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret1 = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
ret2 = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
ret3 = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
ret4 = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
# 唤醒信号 是各厂家自定义的。这里是随便写的
if ret1 == STATUS_OK and ret2 == STATUS_OK and ret3 == STATUS_OK and ret4 == STATUS_OK:
print('\r\nCAN1通道发送4 wake up messages成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x01
time.sleep(0.3)
continue
# step0x01: enter extension diagConversation
if STEP == 0x01:
a = ubyte_array(0x02, 0x10, 0x03, 0x55, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('\r\nCAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x03: update condition
elif STEP == 0x03:
a = ubyte_array(0x04, 0x31, 0x01, 0x02, 0x03, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
# STEP = 0x05
time.sleep(0.3)
# continue
if ret != STATUS_OK:
print('CAN1通道发送失败')
break
# step0x05: DTC OFF
elif STEP == 0x05:
a = ubyte_array(0x02, 0x85, 0x82, 0x55, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(fun_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x06
time.sleep(0.3)
if ret != STATUS_OK:
print('CAN1通道发送失败')
break
# step0x06: forbid transfer
elif STEP == 0x06:
a = ubyte_array(0x03, 0x28, 0x81, 0x01, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(fun_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x07
time.sleep(0.3)
if ret != STATUS_OK:
print('CAN1通道发送失败')
break
# step0x07: ask seed
elif STEP == 0x07:
a = ubyte_array(0x02, 0x27, 0x01, 0x55, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print('CAN1通道发送失败')
break
# step0x71: send key
elif STEP == 0x71:
if seed_size == 4:
key = seed # 这里是厂商提供的算法
print('after calculate,key is: %#x' % key)
key0 = (key >> 24) % 256
key1 = (key >> 16) % 256
key2 = (key >> 8) % 256
key3 = key % 256
print('key0:%#x' % key0)
print('key1:%#x' % key1)
print('key2:%#x' % key2)
print('key3:%#x' % key3)
a = ubyte_array(0x06, 0x27, 0x04, key0, key1, key2, key3, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
elif seed_size == 2:
key = seed # 这里是厂商提供的算法
print('after calculate,key is: %#x' % key)
key0 = (key >> 8) % 256
key1 = key % 256
print('key0:%#x' % key0)
print('key1:%#x' % key1)
a = ubyte_array(0x06, 0x27, 0x02, key0, key1, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
else:
print("seed size unknown")
STEP = 0x00
break
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x08: enter codeConversation
elif STEP == 0x08:
a = ubyte_array(0x02, 0x10, 0x02, 0x55, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x09: ask seed
elif STEP == 0x09:
a = ubyte_array(0x02, 0x27, 0x01, 0x55, 0x55, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x91: send key
elif STEP == 0x91:
if seed_size == 4:
key = seed # 这里是厂商提供的算法
print('after calculate,key is: %#x' % key)
key0 = (key >> 24) % 256
key1 = (key >> 16) % 256
key2 = (key >> 8) % 256
key3 = key % 256
print('key0:%#x' % key0)
print('key1:%#x' % key1)
print('key2:%#x' % key2)
print('key3:%#x' % key3)
a = ubyte_array(0x06, 0x27, 0x04, key0, key1, key2, key3, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
elif seed_size == 2:
key = seed # 这里是厂商提供的算法
print('after calculate,key is: %#x' % key)
key0 = (key >> 8) % 256
key1 = key % 256
print('key0:%#x' % key0)
print('key1:%#x' % key1)
a = ubyte_array(0x06, 0x27, 0x02, key0, key1, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
else:
print("seed size unknown")
STEP = 0x00
break
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print('CAN1通道发送失败')
break
# step0x10: ask to UPLOAD flash firstframe
elif STEP == 0x10:
a = ubyte_array(0x10, 0x0b, 0x35, 0x00, 0x44, 0x00, 0xc1, 0x00) # 这里有期望upload的起始地址
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x11: ask to UPLOAD flash second frame
elif STEP == 0x11:
a = ubyte_array(0x21, 0x00, 0x00, 0x00, 0x00, 0x40, 0x55, 0x55) # 这里有期望upload的长度信息
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# step0x12: transfer flash traffic control frame
elif STEP == 0x12:
a = ubyte_array(0x30, 0x00, 0x00, 0x03, 0xd2, 0x55, 0x55, 0x55)
b = ubyte_3array(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 0, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('CAN1通道发送成功')
print('STEP is %#x' % STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
CURRENT_STEP = STEP
STEP = 0x00
time.sleep(0.3)
continue
if ret != STATUS_OK:
print(STEP, 'CAN1通道发送失败')
break
# STEP0x00: 还没有收到ECU回复
elif STEP == 0x00:
# print("[WAITING RESPONSE]VCI_Receive have not found corresponding ECU response!\n")
if NoResCount < NO_RES_COUNT_MAX:
NoResCount = NoResCount + 1
time.sleep(1)
else:
print("VCI_Receive have not found corresponding ECU response many times,test failed!!!")
break
# step0xFF: 所有流程结束
elif STEP == 0xFF:
CURRENT_STEP = STEP
time.sleep(1)
print("VCI_Receive have found all corresponding ECU response!\n test SUCESS!!!\n")
break
# 通道1接收数据
def receive_can1():
global STEP
global CURRENT_STEP
global seed
global seed_size
rx_vci_can_obj = VCI_CAN_OBJ_ARRAY(2500) # 结构体数组
while 'true':
time.sleep(0.03)
ret = canDLL.VCI_Receive(VCI_USBCAN2, 0, 0, byref(rx_vci_can_obj.ADDR), 2500, 0)
while ret > 0: # 接收到数目为 ret帧 的数据,一个等待周期的缓存数据可能不止一条
print('\r\nCAN1通道接收成功', ret)
for i in range(0, ret):
print("%#x" % rx_vci_can_obj.STRUCT_ARRAY[i].ID,
list_to_hex_string(rx_vci_can_obj.STRUCT_ARRAY[i].Data))
if CURRENT_STEP == 0x01 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x50 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x03:
print("STEP[0X01] extension diagConversation resp received!\n")
STEP = 0X03
ret = 0
break
elif CURRENT_STEP == 0x03 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x71 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x01:
print("STEP[0X03] update condition resp received!\n")
STEP = 0X05
ret = 0
break
elif CURRENT_STEP == 0x07 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x06 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[
2] == 0x01:
print("STEP[0X07] ask seed resp 4 seeds received!\n")
seed0 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[3]
seed1 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[4]
seed2 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[5]
seed3 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[6]
seed = (seed0 << 24) + (seed1 << 16) + (seed2 << 8) + seed3
print('seed:%#x' % seed)
seed_size = 4
STEP = 0X71
ret = 0
break
elif CURRENT_STEP == 0x07 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x04 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[
2] == 0x01:
print("STEP[0X07] ask seed resp 2 seeds received!\n")
seed0 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[3]
seed1 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[4]
seed = (seed1 << 8) + seed2
print('seed:%#x' % seed)
seed_size = 2
STEP = 0X71
ret = 0
break
elif CURRENT_STEP == 0x71 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x02:
print("STEP[0X71] send key resp received!\n")
STEP = 0X08
ret = 0
break
elif CURRENT_STEP == 0x08 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x50 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x02:
print("STEP[0X08] enter codeConversation resp received!\n")
STEP = 0X09
ret = 0
break
elif CURRENT_STEP == 0x09 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x06 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[
2] == 0x01:
print("STEP[0X09] ask seed resp 4 seeds received!\n")
seed0 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[3]
seed1 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[4]
seed2 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[5]
seed3 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[6]
seed = (seed0 << 24) + (seed1 << 16) + (seed2 << 8) + seed3
print('seed:%#x' % seed)
seed_size = 4
STEP = 0X91
ret = 0
break
elif CURRENT_STEP == 0x09 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x04 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[
2] == 0x01:
print("STEP[0X09] ask seed resp 2 seeds received!\n")
seed0 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[3]
seed1 = rx_vci_can_obj.STRUCT_ARRAY[i].Data[4]
seed = (seed1 << 8) + seed2
print('seed:%#x' % seed)
seed_size = 2
STEP = 0X91
ret = 0
break
elif CURRENT_STEP == 0x91 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[1] == 0x67 and \
rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x02:
print("STEP[0X91] send key resp received!\n")
STEP = 0X10
ret = 0
break
elif CURRENT_STEP == 0x10 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x30:
print("STEP[0X10] ask to UPLOAD flash stream control frame resp received!!\n")
STEP = 0X11
ret = 0
break
elif CURRENT_STEP == 0x11 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[2] == 0x36:
print("STEP[0X11] flash transport resp(flash data 1th frame) received!\n")
STEP = 0X12
ret = 0
break
elif CURRENT_STEP == 0x12 and rx_vci_can_obj.STRUCT_ARRAY[i].Data[0] == 0x21:
print("STEP[0X12]flash transport resp(flash data 2th frame) received!\n")
ret = 0
STEP = 0XFF
break
else:
print("CAN1 received unknown data")
time.sleep(0.2)
ret = ret - 1
# 通道2作为模拟器接收数据
def receive_can2():
global CURRENT_STEP
rx_vci_can_obj = VCI_CAN_OBJ_ARRAY(2500) # 结构体数组
while 'true':
ret = canDLL.VCI_Receive(VCI_USBCAN2, 0, 1, byref(rx_vci_can_obj.ADDR), 2500, 0)
while ret > 0: # 接收到一帧数据
# print('\r\n【模拟器】CAN2通道接收成功')
# print("%#x" % rx_vci_can_obj.STRUCT_ARRAY[0].ID, list_to_hex_string(rx_vci_can_obj.STRUCT_ARRAY[0].Data))
time.sleep(0.1)
ubyte_array1 = c_ubyte * 8
ubyte_3array1 = c_ubyte * 3
if CURRENT_STEP == 0x01:
a = ubyte_array1(0x06, 0x50, 0x03, 0x00, 0x32, 0x01, 0xf4, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0xF1:
break
if CURRENT_STEP == 0x03:
a_pend = ubyte_array1(0x03, 0x7f, 0x31, 0x78, 0xaa, 0xaa, 0xaa, 0xaa)
a = ubyte_array1(0x04, 0x71, 0x01, 0x02, 0x03, 0xaa, 0xaa, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a_pend, b) # 单次发送
canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
time.sleep(0.05)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0x05:
# 7DF消息不需要响应
break
if CURRENT_STEP == 0x06:
# 7DF消息不需要响应
break
if CURRENT_STEP == 0X07:
a = ubyte_array1(0x06, 0x67, 0x01, 0xff, 0xff, 0xff, 0xff, 0xaa)
# a = ubyte_array1(0x04, 0x67, 0x01, 0xff, 0xff, 0xaa, 0xaa, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X71:
a = ubyte_array1(0x02, 0x67, 0x02, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X08:
a = ubyte_array1(0x06, 0x50, 0x02, 0x00, 0x32, 0x01, 0xf4, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X09:
# a = ubyte_array1(0x06, 0x67, 0x01, 0xff, 0xff, 0xff, 0xff, 0xaa)
a = ubyte_array1(0x04, 0x67, 0x01, 0xff, 0xff, 0xaa, 0xaa, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP0X%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X91:
a = ubyte_array1(0x02, 0x67, 0x02, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X10:
a = ubyte_array1(0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0x11:
a = ubyte_array1(0x10, 0x42, 0x36, 0x01, 0xcc, 0xcc, 0xcc, 0xcc)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
if CURRENT_STEP == 0X12:
a = ubyte_array1(0x21, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc)
b = ubyte_3array1(0, 0, 0)
vci_can_obj = VCI_CAN_OBJ(can_id + 8, 0, 0, 0, 0, 0, 8, a, b) # 单次发送
ret = canDLL.VCI_Transmit(VCI_USBCAN2, 0, 1, byref(vci_can_obj), 1)
if ret == STATUS_OK:
# print('【模拟器】CAN2通道发送成功 STEP%#x' % CURRENT_STEP)
# print("%#x" % vci_can_obj.ID, list_to_hex_string(vci_can_obj.Data))
time.sleep(0.3)
break
if ret != STATUS_OK:
print('【模拟器】CAN2通道发送失败')
break
elif CURRENT_STEP == 0XFF:
break
else:
print('【模拟器】CURRENT_STEP unknown!%#x' % CURRENT_STEP)
break
if __name__ == "__main__":
CanDLLName = './ControlCAN.dll' # 把DLL放到对应的目录下
canDLL = windll.LoadLibrary('./ControlCAN.dll')
print(CanDLLName)
can_init_and_start()
t1 = threading.Thread(target=send_can1)
t2 = threading.Thread(target=receive_can1)
t3 = threading.Thread(target=receive_can2)
t1.start()
t2.start()
if TestMode == 'true':
t3.start()
t1.join()
t2.join()
if TestMode == 'true':
t3.join()
# 关闭
canDLL.VCI_CloseDevice(VCI_USBCAN2, 0)
调试结果:
D:\Users\PycharmProjects\CX_CANTEST\venv\Scripts\python.exe D:/Users//PycharmProjects/CX_CANTEST/main.py
./ControlCAN.dll
调用 VCI_OpenDevice成功
调用 VCI_InitCAN1成功
调用 VCI_StartCAN1成功
调用 VCI_InitCAN2 成功
调用 VCI_StartCAN2 成功
CAN1通道发送4 wake up messages成功
STEP is 0xf1
0x12d [ 0x50,0x14,0x02,0x20,0x44,0xF1,0x2F,0x15, ]
CAN1通道发送成功
STEP is 0x1
0x777 [ 0x02,0x10,0x03,0x55,0x55,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x06,0x50,0x03,0x00,0x32,0x01,0xF4,0xAA, ]
STEP[0X01] extension diagConversation resp received!
CAN1通道发送成功
STEP is 0x3
0x777 [ 0x04,0x31,0x01,0x02,0x03,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x03,0x7F,0x31,0x78,0xAA,0xAA,0xAA,0xAA, ]
CAN1 received unknown data
CAN1通道接收成功 1
0x77f [ 0x04,0x71,0x01,0x02,0x03,0xAA,0xAA,0xAA, ]
STEP[0X03] update condition resp received!
CAN1通道发送成功
STEP is 0x5
0x7df [ 0x02,0x85,0x82,0x55,0x55,0x55,0x55,0x55, ]
CAN1通道发送成功
STEP is 0x6
0x7df [ 0x03,0x28,0x81,0x01,0x55,0x55,0x55,0x55, ]
CAN1通道发送成功
STEP is 0x7
0x777 [ 0x02,0x27,0x01,0x55,0x55,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x06,0x67,0x01,0xFF,0xFF,0xFF,0xFF,0xAA, ]
STEP[0X07] ask seed resp 4 seeds received!
seed:0xffffffff
after calculate,key is: 0xffffffff
key0:0xff
key1:0xff
key2:0xff
key3:0xff
CAN1通道发送成功
STEP is 0x71
0x777 [ 0x06,0x27,0x04,0xFF,0xFF,0xFF,0xFF,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x02,0x67,0x02,0xAA,0xAA,0xAA,0xAA,0xAA, ]
STEP[0X71] send key resp received!
CAN1通道发送成功
STEP is 0x8
0x777 [ 0x02,0x10,0x02,0x55,0x55,0x55,0x55,0x55, ]
【模拟器】CAN2通道发送成功 STEP0X0x8
0x77f [ 0x06,0x50,0x02,0x00,0x32,0x01,0xF4,0xAA, ]
CAN1通道接收成功 1
0x77f [ 0x06,0x50,0x02,0x00,0x32,0x01,0xF4,0xAA, ]
STEP[0X08] enter codeConversation resp received!
CAN1通道发送成功
STEP is 0x9
0x777 [ 0x02,0x27,0x01,0x55,0x55,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x04,0x67,0x01,0xFF,0xFF,0xAA,0xAA,0xAA, ]
STEP[0X09] ask seed resp 2 seeds received!
seed:0xffff
after calculate,key is: 0xffff
key0:0xff
key1:0xff
CAN1通道发送成功
STEP is 0x91
0x777 [ 0x06,0x27,0x02,0xFF,0xFF,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x02,0x67,0x02,0xAA,0xAA,0xAA,0xAA,0xAA, ]
STEP[0X91] send key resp received!
CAN1通道发送成功
STEP is 0x10
0x777 [ 0x10,0x0B,0x35,0x00,0x44,0x00,0xC1,0x00, ]
CAN1通道接收成功 1
0x77f [ 0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00, ]
STEP[0X10] ask to UPLOAD flash stream control frame resp received!!
CAN1通道发送成功
STEP is 0x11
0x777 [ 0x21,0x00,0x00,0x00,0x00,0x40,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x10,0x42,0x36,0x01,0xCC,0xCC,0xCC,0xCC, ]
STEP[0X11] flash transport resp(flash data 1th frame) received!
CAN1通道发送成功
STEP is 0x12
0x777 [ 0x30,0x00,0x00,0x03,0xD2,0x55,0x55,0x55, ]
CAN1通道接收成功 1
0x77f [ 0x21,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC, ]
STEP[0X12]flash transport resp(flash data 2th frame) received!
VCI_Receive have found all corresponding ECU response!
test SUCESS!!!
|