python-can-hirain 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- can_hirain/BasicStructure.py +1607 -0
- can_hirain/TestBaseVCI.py +386 -0
- can_hirain/__init__.py +3 -0
- can_hirain/config.py +52 -0
- can_hirain/hrcan.py +581 -0
- demo/Test_Canfd.py +112 -0
- demo/Test_can.py +81 -0
- demo/Test_diag.py +87 -0
- demo/Test_env.py +2 -0
- python_can_hirain-0.1.0.dist-info/METADATA +81 -0
- python_can_hirain-0.1.0.dist-info/RECORD +15 -0
- python_can_hirain-0.1.0.dist-info/WHEEL +5 -0
- python_can_hirain-0.1.0.dist-info/entry_points.txt +2 -0
- python_can_hirain-0.1.0.dist-info/licenses/LICENSE +26 -0
- python_can_hirain-0.1.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from . import BasicStructure
|
|
5
|
+
|
|
6
|
+
'''
|
|
7
|
+
接口调用顺序为:
|
|
8
|
+
①H3GetDriverConfig --获取mask和channel
|
|
9
|
+
②H3OpenPort -- 打开端口获得句柄unport_handle
|
|
10
|
+
③定义通道参数 -- channel_bus_params
|
|
11
|
+
④H3ActiveChannel -- 根据以上获取到unport_handle, channel_index, channel_bus_params,使通道生效
|
|
12
|
+
⑤set_can_resistor ---打开电阻
|
|
13
|
+
⑥H3StartStopBusTrace --监听句柄对应通道上的总线数据
|
|
14
|
+
'''
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_dll():
|
|
18
|
+
dll_path = r"C:\Windows\System32\himetal3api.dll"
|
|
19
|
+
if os.path.exists(dll_path):
|
|
20
|
+
dll = ctypes.windll.LoadLibrary(dll_path)
|
|
21
|
+
else:
|
|
22
|
+
print("未安装TestBaseVCI设备驱动,无法获取TestBaseVCI设备! ")
|
|
23
|
+
dll = None
|
|
24
|
+
return dll
|
|
25
|
+
dll = load_dll()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_driver_config():
|
|
29
|
+
driver_config = BasicStructure.H3DriverConfig()
|
|
30
|
+
ip_config = ctypes.c_char_p()
|
|
31
|
+
if dll != None:
|
|
32
|
+
n_rst = dll.H3GetDriverConfig(ctypes.pointer(driver_config), ip_config)
|
|
33
|
+
else:
|
|
34
|
+
n_rst = 0x3004
|
|
35
|
+
if n_rst != 0:
|
|
36
|
+
print('driver信息获取失败!', '错误码', n_rst)
|
|
37
|
+
return driver_config
|
|
38
|
+
|
|
39
|
+
def channel_mask(channel):
|
|
40
|
+
driver_confi = get_driver_config()
|
|
41
|
+
mask = ctypes.c_uint64(driver_confi.h3Channels[channel].unChannelMask)
|
|
42
|
+
channel_index = ctypes.c_ubyte(driver_confi.h3Channels[channel].ucChannelIndex) #修改
|
|
43
|
+
return mask,channel_index
|
|
44
|
+
|
|
45
|
+
def open_port(mask):
|
|
46
|
+
unport_handle = ctypes.c_size_t()
|
|
47
|
+
n_rst = dll.H3OpenPort(ctypes.pointer(unport_handle), ctypes.c_uint64(mask))
|
|
48
|
+
if n_rst != 0:
|
|
49
|
+
print('端口打开失败!', '错误码', n_rst)
|
|
50
|
+
return unport_handle
|
|
51
|
+
|
|
52
|
+
def get_device_ver(port_handle,mask):
|
|
53
|
+
dev_version = BasicStructure.DeviceVersion() # 获取结构体out输出参数
|
|
54
|
+
n_rst = dll.H3GetDeviceVersion(port_handle, ctypes.pointer(dev_version)) # 指针输出参数获取
|
|
55
|
+
if n_rst != 0:
|
|
56
|
+
print('device信息获取失败!', '错误码', n_rst)
|
|
57
|
+
return dev_version,n_rst
|
|
58
|
+
|
|
59
|
+
def active_can_channel(port_handle,channel_index, ListenFlag = 0, Baudrate = 500000, SamplePoint = 0):
|
|
60
|
+
channel_bus_params = BasicStructure.CHANNEL_BUS_PARAMS()
|
|
61
|
+
time_stamp = ctypes.create_string_buffer(10)
|
|
62
|
+
channel_bus_params.ucBusType = 0
|
|
63
|
+
channel_bus_params.busParam.can.ucListenFlag = ListenFlag
|
|
64
|
+
channel_bus_params.busParam.can.ucBaudrate = Baudrate
|
|
65
|
+
channel_bus_params.busParam.can.ucSamplePoint = SamplePoint
|
|
66
|
+
if Baudrate == 500_000 and SamplePoint == 0:
|
|
67
|
+
channel_bus_params.ucBusType = 8
|
|
68
|
+
channel_bus_params.busParam.canCanfdEx.ucCanMode = 0
|
|
69
|
+
channel_bus_params.busParam.canCanfdEx.ucListenFlag = 0
|
|
70
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtBrp = 4
|
|
71
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtSjw = 4
|
|
72
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg1 = 14
|
|
73
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg2 = 5
|
|
74
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtBrp = 0
|
|
75
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtSjw = 0
|
|
76
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg1 = 0
|
|
77
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg2 = 0
|
|
78
|
+
channel_bus_params.busParam.canCanfdEx.unAbrBaudrate = 500000
|
|
79
|
+
channel_bus_params.busParam.canCanfdEx.ucAbrSamplePoint = 0
|
|
80
|
+
channel_bus_params.busParam.canCanfdEx.unDbrBaudrate = 0
|
|
81
|
+
channel_bus_params.busParam.canCanfdEx.ucDbrSamplePoint = 0
|
|
82
|
+
n_rst = dll.H3ActiveChannel(port_handle, channel_index, channel_bus_params, ctypes.byref(time_stamp))
|
|
83
|
+
if n_rst == 0x120e:
|
|
84
|
+
print('此通道已被其他端口激活,将沿用上次设置的总线参数!')
|
|
85
|
+
n_rst = 0
|
|
86
|
+
elif n_rst != 0:
|
|
87
|
+
print('通道激活失败!', '错误码', n_rst)
|
|
88
|
+
return n_rst
|
|
89
|
+
|
|
90
|
+
def active_canfd_channel(port_handle,channel_index, ListenFlag = 0, abrBaudrate = 500000, dbrBaudrate = 2000000, abrSamplePoint = 0, dbrSamplePoint = 0):
|
|
91
|
+
channel_bus_params = BasicStructure.CHANNEL_BUS_PARAMS()
|
|
92
|
+
time_stamp = ctypes.c_uint()
|
|
93
|
+
channel_bus_params.ucBusType = 3
|
|
94
|
+
channel_bus_params.busParam.canFd.ucListenFlag = ListenFlag
|
|
95
|
+
channel_bus_params.busParam.canFd.abrBaudrate = abrBaudrate # 设置波特率为BAUD_2M
|
|
96
|
+
channel_bus_params.busParam.canFd.dbrBaudrate = dbrBaudrate # 设置波特率为BAUD_500K
|
|
97
|
+
channel_bus_params.busParam.canFd.abrSamplePoint = abrSamplePoint
|
|
98
|
+
channel_bus_params.busParam.canFd.dbrSamplePoint = dbrSamplePoint
|
|
99
|
+
if abrBaudrate == 500_000 and dbrBaudrate == 2000_000 and abrSamplePoint == 0 and dbrSamplePoint == 0:
|
|
100
|
+
channel_bus_params.ucBusType = 8
|
|
101
|
+
channel_bus_params.busParam.canCanfdEx.ucCanMode = 1
|
|
102
|
+
channel_bus_params.busParam.canCanfdEx.ucListenFlag = 0
|
|
103
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtBrp = 1
|
|
104
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtSjw = 4
|
|
105
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg1 = 59
|
|
106
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg2 = 20
|
|
107
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtBrp = 1
|
|
108
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtSjw = 4
|
|
109
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg1 = 14
|
|
110
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg2 = 5
|
|
111
|
+
channel_bus_params.busParam.canCanfdEx.unAbrBaudrate = 500000
|
|
112
|
+
channel_bus_params.busParam.canCanfdEx.ucAbrSamplePoint = 0
|
|
113
|
+
channel_bus_params.busParam.canCanfdEx.unDbrBaudrate = 2000000
|
|
114
|
+
channel_bus_params.busParam.canCanfdEx.ucDbrSamplePoint = 0
|
|
115
|
+
elif abrBaudrate == 500_000 and dbrBaudrate == 2000_000 and abrSamplePoint == 0 and dbrSamplePoint == 21:
|
|
116
|
+
channel_bus_params.ucBusType = 8
|
|
117
|
+
channel_bus_params.busParam.canCanfdEx.ucCanMode = 1
|
|
118
|
+
channel_bus_params.busParam.canCanfdEx.ucListenFlag = 0
|
|
119
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtBrp = 1
|
|
120
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtSjw = 4
|
|
121
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg1 = 59
|
|
122
|
+
channel_bus_params.busParam.canCanfdEx.ucCanBrtTseg2 = 20
|
|
123
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtBrp = 1
|
|
124
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtSjw = 4
|
|
125
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg1 = 15
|
|
126
|
+
channel_bus_params.busParam.canCanfdEx.ucCanfdBrtTseg2 = 4
|
|
127
|
+
channel_bus_params.busParam.canCanfdEx.unAbrBaudrate = 500000
|
|
128
|
+
channel_bus_params.busParam.canCanfdEx.ucAbrSamplePoint = 0
|
|
129
|
+
channel_bus_params.busParam.canCanfdEx.unDbrBaudrate = 2000000
|
|
130
|
+
channel_bus_params.busParam.canCanfdEx.ucDbrSamplePoint = 21
|
|
131
|
+
n_rst = dll.H3ActiveChannel(port_handle, channel_index, channel_bus_params, time_stamp)
|
|
132
|
+
if n_rst == 0x120e:
|
|
133
|
+
print('此通道已被其他端口激活,将沿用上次设置的总线参数!')
|
|
134
|
+
n_rst = 0
|
|
135
|
+
elif n_rst != 0:
|
|
136
|
+
print('通道激活失败!', '错误码', n_rst)
|
|
137
|
+
return n_rst
|
|
138
|
+
|
|
139
|
+
def active_lin_channel(port_handle,channel_index):
|
|
140
|
+
# 激活LIN通道
|
|
141
|
+
channel_bus_params = BasicStructure.CHANNEL_BUS_PARAMS()
|
|
142
|
+
time_stamp = ctypes.create_string_buffer(10)
|
|
143
|
+
channel_bus_params.ucBusType = 0x2
|
|
144
|
+
channel_bus_params.busParam.lin.unBaudrate = 9600
|
|
145
|
+
channel_bus_params.busParam.lin.ucLinVersion = 0
|
|
146
|
+
n_rst = dll.H3ActiveChannel(port_handle,channel_index, channel_bus_params, time_stamp)
|
|
147
|
+
if n_rst != 0:
|
|
148
|
+
print('LIN通道激活失败!', n_rst)
|
|
149
|
+
return n_rst
|
|
150
|
+
|
|
151
|
+
def active_ethernet_channel(port_handle, channel_index):
|
|
152
|
+
# 激活通道
|
|
153
|
+
channel_bus_param = BasicStructure.CHANNEL_BUS_PARAMS()
|
|
154
|
+
channel_bus_param.ucBusType = 0x4
|
|
155
|
+
channel_bus_param.busParam.eth.szFilter = b''
|
|
156
|
+
channel_bus_param.busParam.eth.nMaxSize = 256
|
|
157
|
+
channel_bus_param.busParam.eth.nMaxPacks = 1024
|
|
158
|
+
channel_bus_param.busParam.eth.szDumpFileDir = br'C:\test_Ethernet'
|
|
159
|
+
time_stamp = ctypes.c_uint()
|
|
160
|
+
n_rst = dll.H3ActiveChannel(port_handle, channel_index, channel_bus_param, ctypes.byref(time_stamp))
|
|
161
|
+
if n_rst != 0:
|
|
162
|
+
print('以太网通道激活失败!', n_rst)
|
|
163
|
+
|
|
164
|
+
def can_set_resistor(unport_handle, channel_index,value):
|
|
165
|
+
#value为true启用电阻,false关闭电阻
|
|
166
|
+
n_rst = dll.H3CANSetResistor(unport_handle, channel_index,value)
|
|
167
|
+
if n_rst != 0:
|
|
168
|
+
print('CAN终端电阻操作失败!', '错误码', n_rst)
|
|
169
|
+
return n_rst
|
|
170
|
+
|
|
171
|
+
def lin_set_resistor(unport_handle, channel_index,value):
|
|
172
|
+
n_rst = dll.H3LINSetResistor(unport_handle, channel_index, value)
|
|
173
|
+
if n_rst != 0:
|
|
174
|
+
print('终端电阻打开失败!')
|
|
175
|
+
return n_rst
|
|
176
|
+
|
|
177
|
+
def set_lin_resistor(port_handle, channel_index, value):
|
|
178
|
+
n_rst = dll.H3LINSetResistor(port_handle, channel_index, value)
|
|
179
|
+
if n_rst != 0:
|
|
180
|
+
print('LIN上拉/下拉电阻设置失败!','错误码',n_rst)
|
|
181
|
+
return n_rst
|
|
182
|
+
|
|
183
|
+
def set_lin_txMsg(port_handle, channel_index, txMsg_Count,linMsg): #发送响应报文
|
|
184
|
+
n_rst = dll.H3LINSetTxMsgs(port_handle, channel_index, txMsg_Count, ctypes.byref(linMsg))
|
|
185
|
+
if n_rst != 0:
|
|
186
|
+
print('LIN响应报文发送失败!', '错误码', n_rst)
|
|
187
|
+
return n_rst
|
|
188
|
+
|
|
189
|
+
def set_lin_schedule(port_handle, channel_index,lin_Count,node): #发送头部报文
|
|
190
|
+
n_rst = dll.H3LINSetScheduleTable(port_handle, channel_index, lin_Count, ctypes.byref(node))
|
|
191
|
+
if n_rst != 0:
|
|
192
|
+
print('LIN首部报文发送失败!', '错误码', n_rst)
|
|
193
|
+
return n_rst
|
|
194
|
+
|
|
195
|
+
def lin_clear(port_handle, channel_index, value):
|
|
196
|
+
n_rst = dll.H3LINClear(port_handle, channel_index, value)
|
|
197
|
+
if n_rst != 0:
|
|
198
|
+
print('LIN数据清除失败!','错误码',n_rst)
|
|
199
|
+
return n_rst
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def start_stop_trace(unport_handle, channel_index, value):
|
|
203
|
+
#value为True或False
|
|
204
|
+
# time_stamp = ctypes.c_longlong()
|
|
205
|
+
time_stamp = ctypes.c_void_p()
|
|
206
|
+
n_rst = dll.H3StartStopBusTrace(unport_handle, channel_index, value, ctypes.pointer(time_stamp))
|
|
207
|
+
# n_rst = dll.H3StartStopBusTrace(unport_handle, channel_index, value)
|
|
208
|
+
if n_rst != 0:
|
|
209
|
+
print('启用/停止监控设置失败!', '错误码', n_rst)
|
|
210
|
+
return n_rst
|
|
211
|
+
|
|
212
|
+
def close_port(unport_handle):
|
|
213
|
+
n_rst = dll.H3ClosePort(unport_handle)
|
|
214
|
+
# if n_rst != 0:
|
|
215
|
+
# print('关闭端口失败!',unport_handle, '错误码', n_rst)
|
|
216
|
+
return n_rst
|
|
217
|
+
|
|
218
|
+
def deactive_channel(unport_handle):
|
|
219
|
+
n_rst = dll.H3DeactiveChannel(unport_handle)
|
|
220
|
+
if n_rst != 0:
|
|
221
|
+
print('通道取消失败!','错误码',n_rst)
|
|
222
|
+
return n_rst
|
|
223
|
+
|
|
224
|
+
def get_device_time(unport_handle):
|
|
225
|
+
time_stamp = ctypes.c_longlong()
|
|
226
|
+
n_rst = dll.H3GetDeviceTimeStamp(unport_handle, ctypes.pointer(time_stamp))
|
|
227
|
+
if n_rst != 0:
|
|
228
|
+
print('时间戳获取失败!','错误码',n_rst)
|
|
229
|
+
return time_stamp
|
|
230
|
+
|
|
231
|
+
def replay_send_packs(port_handle, Msg_PackCount,Packs):
|
|
232
|
+
n_rst = dll.H3ReplaySendPacks(port_handle, Msg_PackCount, ctypes.byref(Packs))
|
|
233
|
+
if n_rst != 0:
|
|
234
|
+
print('回放报文发送失败!','错误码',n_rst)
|
|
235
|
+
return n_rst
|
|
236
|
+
|
|
237
|
+
def can_set_periodic_tx_msgs(port_handle, channel_index, ptxinfo):
|
|
238
|
+
# ptxinfo = BasicStructure.CanPeriodTxInfo()
|
|
239
|
+
# canMsg = BasicStructure.GET_CAN_TRACE()
|
|
240
|
+
# canMsg.unCanId = unCanId # 定义发送方ID
|
|
241
|
+
# canMsg.ucDLC = 8
|
|
242
|
+
# canMsg.ucData = data
|
|
243
|
+
# canMsg.ucEDL = 1 # CANfd
|
|
244
|
+
# ptxinfo.canMsg = can_msg
|
|
245
|
+
# ptxinfo.usPeriod = ctypes.c_ushort(period)
|
|
246
|
+
ucTxInfoCount = 1
|
|
247
|
+
time_stamp = ctypes.create_string_buffer(4)
|
|
248
|
+
n_rst = dll.H3CANSetPeriodicTxMsgs(port_handle, channel_index, ucTxInfoCount, ctypes.byref(ptxinfo), ctypes.byref(time_stamp))
|
|
249
|
+
if n_rst != 0:
|
|
250
|
+
print('周期报文发送失败!', '错误码', n_rst)
|
|
251
|
+
return n_rst
|
|
252
|
+
|
|
253
|
+
def can_set_one_tx_msgs(port_handle, channel_index, tx_data, msg_id = 0x000, dlc = 8):
|
|
254
|
+
time_stamp = ctypes.create_string_buffer(4)
|
|
255
|
+
canMsg = (BasicStructure.GET_CAN_TRACE * 1)()
|
|
256
|
+
canMsg[0].unCanId = msg_id # 定义发送方ID
|
|
257
|
+
canMsg[0].ucDLC = dlc
|
|
258
|
+
for i in range(min(dlc, 64)): # 确保不越界
|
|
259
|
+
canMsg[0].ucData[i] = tx_data[i]
|
|
260
|
+
txMsg_Count = 1
|
|
261
|
+
n_rst = dll.H3CANSetOneTimeTxMsgs(port_handle, channel_index, txMsg_Count, ctypes.byref(canMsg), time_stamp)
|
|
262
|
+
if n_rst != 0:
|
|
263
|
+
print('单帧报文发送失败!', '错误码', n_rst)
|
|
264
|
+
return n_rst
|
|
265
|
+
|
|
266
|
+
def can_set_one_tx_msg_no_resp(port_handle, channel_index, can_msg):
|
|
267
|
+
n_rst = dll.H3CANSetOneTimeTxMsgNoResp(port_handle, channel_index, can_msg)
|
|
268
|
+
if n_rst != 0:
|
|
269
|
+
print('单帧无响应报文发送失败!', '错误码', n_rst)
|
|
270
|
+
return n_rst
|
|
271
|
+
|
|
272
|
+
def ethternet_set_one_time_tx_msg(port_handle, channel_index, data, data_len=82):
|
|
273
|
+
"""
|
|
274
|
+
调用H3EthernetSetOneTimeTxMsgNoResp接口发送报文
|
|
275
|
+
:param port_handle:
|
|
276
|
+
:param channel_index:
|
|
277
|
+
:param data_len: unDataLength参数
|
|
278
|
+
:param ethernet_msg: ucData参数
|
|
279
|
+
:return: 函数返回状态码
|
|
280
|
+
"""
|
|
281
|
+
ethernet_msg = BasicStructure.EthernetMsg()
|
|
282
|
+
ethernet_msg.unDataLength = data_len
|
|
283
|
+
ethernet_msg.ucData = data
|
|
284
|
+
n_rst = dll.H3EthernetSetOneTimeTxMsgNoResp(port_handle, channel_index, ethernet_msg)
|
|
285
|
+
if n_rst != 0:
|
|
286
|
+
print('以太网报文发送失败!', '错误码', n_rst)
|
|
287
|
+
return n_rst
|
|
288
|
+
|
|
289
|
+
def can_set_diagnostic_config(port_handle, channel_index, ReqId=0x111, RespId=0x222):
|
|
290
|
+
# 设置ECU信息
|
|
291
|
+
ecu_info = (BasicStructure.EcuInfo * 64)()
|
|
292
|
+
ecu_info[0].unReqID = ReqId
|
|
293
|
+
ecu_info[0].unUSDTRespID = RespId
|
|
294
|
+
diagnostic_config = BasicStructure.CAN_DIAGNOSTIC_CONFIG()
|
|
295
|
+
diagnostic_config.ucDataPadding = 0X00
|
|
296
|
+
diagnostic_config.unFuncReqID = 0X7FF
|
|
297
|
+
diagnostic_config.ucEcuCount = 1
|
|
298
|
+
diagnostic_config.ecuInfo = ecu_info
|
|
299
|
+
n_rst = dll.H3CANSetDiagnosticConfig(port_handle, channel_index, diagnostic_config)
|
|
300
|
+
if n_rst != 0:
|
|
301
|
+
print('诊断报文设置失败!', '错误码', n_rst)
|
|
302
|
+
return n_rst
|
|
303
|
+
|
|
304
|
+
def can_forward_15765_2(port_handle, channel_index, ReqId=0x111, MType=0b00010001, DataLen=2, data=(0x10, 0x01)):
|
|
305
|
+
# 发送报文
|
|
306
|
+
can_request = BasicStructure.CanRequest()
|
|
307
|
+
can_request.unReqID = ReqId
|
|
308
|
+
can_request.ucMType = MType
|
|
309
|
+
can_request.usDataLen = DataLen
|
|
310
|
+
can_request.ucData = data
|
|
311
|
+
time_out = ctypes.c_ushort()
|
|
312
|
+
n_rst = dll.H3CANForward15765_2(port_handle, channel_index, can_request, time_out)
|
|
313
|
+
if n_rst != 0:
|
|
314
|
+
print('诊断报文发送失败!', '错误码', n_rst)
|
|
315
|
+
return n_rst
|
|
316
|
+
|
|
317
|
+
def rx_msg(port_handle, count):
|
|
318
|
+
"""
|
|
319
|
+
调用H3GetRxService函数获取接收队列报文
|
|
320
|
+
:param count: pRxSrvCount 要获取的RX_SERVICE个数和实际接收到的个数
|
|
321
|
+
:return:获取到的报文服务列表{1:{canId: ,dlc: ,data:()},...}
|
|
322
|
+
"""
|
|
323
|
+
msg = {}
|
|
324
|
+
can_sum = canfd_sum = lin_sum = error_sum = eth_sum = 0
|
|
325
|
+
rx_msg = (BasicStructure.RX_SERVICE * count)()
|
|
326
|
+
rx_count = ctypes.c_uint(count)
|
|
327
|
+
n_rst = dll.H3GetRxService(port_handle, ctypes.pointer(rx_msg), ctypes.pointer(rx_count))
|
|
328
|
+
# print('接口返回值', n_rst)
|
|
329
|
+
rx_count = rx_count.value
|
|
330
|
+
# print('接口本次获取数量', rx_count)
|
|
331
|
+
for i in range(rx_count):
|
|
332
|
+
msg[i] = {}
|
|
333
|
+
|
|
334
|
+
if rx_msg[i].nServiceType == 0x1:
|
|
335
|
+
msg[i]['ErrorCode'] = rx_msg[i].service.getDiagnosticError.ucErrorCode
|
|
336
|
+
msg[i]['DataSize'] = rx_msg[i].service.getDiagnosticError.nDataSize
|
|
337
|
+
data = rx_msg[i].service.getDiagnosticError.ucData[0:64]
|
|
338
|
+
msg[i]['data'] = data
|
|
339
|
+
elif rx_msg[i].nServiceType == 0x2:
|
|
340
|
+
msg[i]['EcuReqId'] = rx_msg[i].service.get15765_2_Confirm.unEcuReqId
|
|
341
|
+
msg[i]['NResult'] = rx_msg[i].service.get15765_2_Confirm.ucNResult
|
|
342
|
+
msg[i]['MType'] = rx_msg[i].service.get15765_2_Confirm.ucMType
|
|
343
|
+
elif rx_msg[i].nServiceType == 0x3:
|
|
344
|
+
msg[i]['EcuRespId'] = rx_msg[i].service.get15765_2_FFInd.unEcuRespId
|
|
345
|
+
msg[i]['MType'] = rx_msg[i].service.get15765_2_FFInd.ucMType
|
|
346
|
+
msg[i]['Length'] = rx_msg[i].service.get15765_2_FFInd.usLength
|
|
347
|
+
elif rx_msg[i].nServiceType == 0x4:
|
|
348
|
+
msg[i]['EcuRespId'] = rx_msg[i].service.get15765_2_RespInd.unEcuRespId
|
|
349
|
+
msg[i]['NResult'] = rx_msg[i].service.get15765_2_RespInd.ucNResult
|
|
350
|
+
msg[i]['Length'] = rx_msg[i].service.get15765_2_RespInd.usLength
|
|
351
|
+
msg[i]['data'] = rx_msg[i].service.get15765_2_RespInd.ucData[0:64]
|
|
352
|
+
elif rx_msg[i].nServiceType == 0x8:
|
|
353
|
+
msg[i]['timeStamp'] = rx_msg[i].service.getCanTrace.unTimestamp
|
|
354
|
+
msg[i]['canId'] = rx_msg[i].service.getCanTrace.unCanId
|
|
355
|
+
msg[i]['dlc'] = rx_msg[i].service.getCanTrace.ucDLC
|
|
356
|
+
data = rx_msg[i].service.getCanTrace.ucData
|
|
357
|
+
msg[i]['data'] = data[0:64]
|
|
358
|
+
msg[i]['brs'] = rx_msg[i].service.getCanTrace.ucBRS
|
|
359
|
+
msg[i]['edl'] = rx_msg[i].service.getCanTrace.ucEDL
|
|
360
|
+
msg[i]['tag'] = rx_msg[i].service.getCanTrace.ucTag
|
|
361
|
+
can_sum += 1
|
|
362
|
+
elif rx_msg[i].nServiceType == 0xa:
|
|
363
|
+
lin_sum += 1
|
|
364
|
+
msg[i]['linId'] = (rx_msg[i].service.getLinTrace.ucLinId)&63
|
|
365
|
+
msg[i]['startTimeStamp'] = rx_msg[i].service.getLinTrace.unStartTimestamp
|
|
366
|
+
msg[i]['endTimeStamp'] = rx_msg[i].service.getLinTrace.unEndTimestamp
|
|
367
|
+
msg[i]['ctlByte'] = rx_msg[i].service.getLinTrace.ucCtlByte
|
|
368
|
+
msg[i]['dataFiledLen'] = rx_msg[i].service.getLinTrace.ucDataFiledLen
|
|
369
|
+
msg[i]['dataFiled'] = rx_msg[i].service.getLinTrace.ucDataFiled
|
|
370
|
+
elif rx_msg[i].nServiceType == 0xc:
|
|
371
|
+
msg[i]['timeStamp'] = rx_msg[i].service.getEthernetTrace.unTimestamp
|
|
372
|
+
data_len = rx_msg[i].service.getEthernetTrace.ethRawData.unDataLen
|
|
373
|
+
msg[i]['data_len'] = rx_msg[i].service.getEthernetTrace.ethRawData.unDataLen
|
|
374
|
+
msg[i]['data'] = rx_msg[i].service.getEthernetTrace.ethRawData.ucData[0:data_len]
|
|
375
|
+
elif rx_msg[i].nServiceType == 0x0:
|
|
376
|
+
error_sum += 1
|
|
377
|
+
msg[i]['ErrorCode'] = rx_msg[i].service.getBusError.ucErrorCode
|
|
378
|
+
msg[i]['FrameType'] = rx_msg[i].service.getBusError.ucFrameType
|
|
379
|
+
msg[i]['timestamp'] = rx_msg[i].service.getBusError.unTimestamp
|
|
380
|
+
return rx_count, msg, can_sum, error_sum, lin_sum
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
if __name__ == '__main__':
|
|
384
|
+
driver = get_driver_config()
|
|
385
|
+
|
|
386
|
+
|
can_hirain/__init__.py
ADDED
can_hirain/config.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
class TestBaseVCI_ChlMode:
|
|
2
|
+
CAN = 0
|
|
3
|
+
CANFD = 1
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TestBaseVCI_SAMPLEPOINT:
|
|
7
|
+
SAMPLE_75 = 0
|
|
8
|
+
SAMPLE_50 = 1
|
|
9
|
+
SAMPLE_52_5 = 2
|
|
10
|
+
SAMPLE_55 = 3
|
|
11
|
+
SAMPLE_57_5 = 4
|
|
12
|
+
SAMPLE_60 = 5
|
|
13
|
+
SAMPLE_61_25 = 6
|
|
14
|
+
SAMPLE_62_5 = 7
|
|
15
|
+
SAMPLE_63_75 = 8
|
|
16
|
+
SAMPLE_65 = 9
|
|
17
|
+
SAMPLE_66_25 = 10
|
|
18
|
+
SAMPLE_67_5 = 11
|
|
19
|
+
SAMPLE_68 = 12
|
|
20
|
+
SAMPLE_68_75 = 13
|
|
21
|
+
SAMPLE_70 = 14
|
|
22
|
+
SAMPLE_71_25 = 15
|
|
23
|
+
SAMPLE_72_5 = 16
|
|
24
|
+
SAMPLE_73_75 = 17
|
|
25
|
+
SAMPLE_76_25 = 18
|
|
26
|
+
SAMPLE_77_5 = 19
|
|
27
|
+
SAMPLE_78_75 = 20
|
|
28
|
+
SAMPLE_80 = 21
|
|
29
|
+
SAMPLE_81_25 = 22
|
|
30
|
+
SAMPLE_82_5 = 23
|
|
31
|
+
SAMPLE_85 = 24
|
|
32
|
+
SAMPLE_87_5 = 25
|
|
33
|
+
SAMPLE_90 = 26
|
|
34
|
+
SAMPLE_92_5 = 27
|
|
35
|
+
SAMPLE_95 = 28
|
|
36
|
+
|
|
37
|
+
class CAN_LISTEN_MODE:
|
|
38
|
+
NORMAL_TRANSMIT = 0
|
|
39
|
+
BUS_LISTEN = 1
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class N_Parameter:
|
|
43
|
+
N_STmin = 0x01
|
|
44
|
+
N_BS = 0x02
|
|
45
|
+
N_Cs = 0x03
|
|
46
|
+
N_Bs = 0x04
|
|
47
|
+
N_Cr = 0x05
|
|
48
|
+
N_Opt = 0x06
|
|
49
|
+
|
|
50
|
+
class CANMType:
|
|
51
|
+
MTYPE_LOCAL = 0x01
|
|
52
|
+
MTYPE_REMOTE = 0x02
|