修改CAN DBC新增CAN报文返回
This commit is contained in:
parent
5b6839cecf
commit
a677693d5d
@ -6,10 +6,9 @@
|
||||
# @Time : 2025-03-26 10:55:41
|
||||
# @version : python3
|
||||
# @Update time :
|
||||
# @Description : CAN DBC 编解码代码示例
|
||||
# @Description : 使用 DBC 编解码
|
||||
'''
|
||||
|
||||
|
||||
from pathlib import Path
|
||||
import cantools
|
||||
import can
|
||||
@ -24,15 +23,15 @@ class CANCodec:
|
||||
def _load_dbc(self, dbc_file):
|
||||
"""加载 DBC 文件,并进行异常处理"""
|
||||
if not Path(dbc_file).exists():
|
||||
print(f"[ERROR] DBC 文件不存在: {dbc_file}")
|
||||
raise FileNotFoundError(f"DBC 文件不存在: {dbc_file}")
|
||||
print("[ERROR] DBC 文件不存在: {dbc_file}".format(Path(dbc_file).name))
|
||||
raise FileNotFoundError("DBC 文件不存在: {dbc_file}".format(Path(dbc_file).name))
|
||||
|
||||
try:
|
||||
db = cantools.database.load_file(dbc_file)
|
||||
print(f"成功加载 DBC 文件: {dbc_file}")
|
||||
print("[INFO] 成功加载 DBC 文件: {}".format(Path(dbc_file).name))
|
||||
return db
|
||||
except Exception as e:
|
||||
print(f"加载 DBC 文件失败: {e}")
|
||||
print("[ERROR] 加载 DBC 文件失败: {}".format(e))
|
||||
raise
|
||||
|
||||
def encode_message(self, message_name, signal_values=None):
|
||||
@ -71,6 +70,15 @@ class CANCodec:
|
||||
decoded_signals = self.db.decode_message(can_id, data)
|
||||
return decoded_signals
|
||||
|
||||
def return_encoded_info(self, can_message, encoded_data, signal_data):
|
||||
"""返回编码后的 CAN 报文信息"""
|
||||
formatted_data = ', '.join([f"0x{byte:02X}" for byte in encoded_data])
|
||||
return {
|
||||
"can_id": hex(can_message.arbitration_id),
|
||||
"decimal": [byte for byte in encoded_data],
|
||||
"can_msg": '[{}]'.format(formatted_data),
|
||||
}
|
||||
|
||||
def print_encoded_info(self, can_message, encoded_data, signal_data):
|
||||
"""打印编码后的 CAN 报文信息"""
|
||||
formatted_data = ', '.join([f"0x{byte:02X}" for byte in encoded_data])
|
||||
Loading…
Reference in New Issue
Block a user