1. 问题现象描述
使用canmatrix模块解析DBC,pycharm运行程序代码是能够正常实现功能的,但是生成.exe后执行报错如下:
定位到出错代码如下:
python
db = canmatrix.formats.loadp(dbc_path, encoding="utf-8")
报错信息导致的原因是加载DBC文件之后,得到的db是None。
目前还没有找到原因,我采取的解决办法是换cantools模块解析DBC文件,发现生成的.exe文件功能执行正常。
2. cantools解析DBC文件
使用cantools解析DBC,查看cantools模块的.py代码可知Signal和Message包含的字段定义如下。
Message的字段名称如下:
python
db = cantools.database.load_file(dbc_file_path, encoding="utf-8")
# 遍历所有消息(frames)
for message in db.messages:
message_info = dict()
message_info["name"] = message.name
message_info["frame_id"] = message.frame_id
message_info["is_extended"] = message.is_extended_frame
message_info["is_fd"] = message.is_fd
message_info["size"] = message.length
message_info["comment"] = message.comment
Signal的字段名称如下:
python
db = cantools.database.load_file(dbc_file_path, encoding="utf-8")
for signal in message.signals:
signal_info_dict = dict()
signal_info_dict["name"] = signal.name
signal_info_dict["start_bit"] = signal.start
signal_info_dict["size"] = signal.length
if signal.byte_order == "little_endian":
signal_info_dict["is_little_endian"] = True
else:
signal_info_dict["is_little_endian"] = False
signal_info_dict["is_signed"] = signal.is_signed
signal_info_dict["is_float"] = signal.conversion.is_float
signal_info_dict["offset"] = signal.conversion.offset
signal_info_dict["factor"] = signal.conversion.scale
signal_info_dict["min"] = signal.minimum
signal_info_dict["max"] = signal.maximum
signal_info_dict["unit"] = signal.unit
signal_info_dict["comment"] = signal.comment