[python]用python获取EXCEL文件内容并保存到DBC
2023-12-17 23:43:23
关键词
==python、excel、DBC、openpyxl ==
平台说明
项目 | Value |
---|---|
python版本 | 3.6 |
背景
在搭建自动化测试平台的时候经常会提取DBC文件中的信息并保存为excel或者其他文件格式,用于自动化测试。本文介绍了如何用python3.6实现获取EXCEL文件内容并保存到DBC。
所需库
1.canmatrix:需要它库来解析 DBC 文件,它提供了对 Controller Area Network (CAN) 数据的解析功能,包括 DBC 文件的支持。
2.openpyxl :是一个用于读写 Excel 文件的 Python 库。
实现过程
方法1.
1.安装相关库
pip install openpyxl canmatrix
2.代码实现
from openpyxl import load_workbook
import canmatrix
class MyDBC_Obj:
def __init__ (self,dbc_path,excel_path):
self.name = "self"
self.sheet = ""
self.dbc = ""
self.dbc_path = dbc_path
self.excel_path = excel_path
def read_excel(self):
workbook = load_workbook(filename = self.excel_path)
self.sheet = workbook.active
def generate_dbc(self):
self.dbc = canmatrix.CanMatrix()
self.dbc.add_global_defines("BusType","STRING")
self.dbc.add_define_default("BusType","CAN")
# 报文发送类型属性以及默认值
self.dbc.add_frame_defines("GenMsgSendType", 'ENUM "Cyclic", "Event"' )
self.dbc.add_define_default("GenMsgSendType", "Cyclic")
self.dbc.add_frame_defines("GenMsgCycleTime", 'INT 0 65535')
self.dbc.add_define_default("GenMsgCycleTime", "0")
def write_dbc(self):
self.read_excel()
self.generate_dbc()
ecu_obj = canmatrix.Ecu(self.sheet.cell(1,29).value)
ecu_obj.name = self.sheet.cell(1,29).value
self.dbc.add_ecu(ecu_obj)
for i in range(2,self.sheet.max_row+1):
if self.sheet.cell(row=i,column=1).value == None:
self.sheet.cell(row=i,column=1).value = self.sheet.cell(row=i-1,column=1).value
self.sheet.cell(row=i,column=2).value = self.sheet.cell(row=i-1,column=2).value
self.sheet.cell(row=i,column=3).value = self.sheet.cell(row=i-1,column=3).value
self.sheet.cell(row=i,column=4).value = self.sheet.cell(row=i-1,column=4).value
self.sheet.cell(row=i,column=5).value = self.sheet.cell(row=i-1,column=5).value
self.sheet.cell(row=i,column=6).value = self.sheet.cell(row=i-1,column=6).value
message_name = ""
message_id = 0
message_length = 0
message_type_is_fd = False
message_type_is_extended = False
message_cycle = 0
message_recive = ""
frame_obj = canmatrix.Frame()
signal_obj=canmatrix.Signal()
for row in self.sheet.iter_rows(min_row=3,values_only=True):
signal_name = row[6]
if signal_name != None:
signal_obj.name = row[6]
message_name = row[0]
message_id = int(row[2],16)
if row[11] != None:
signal_obj.start_bit = int(row[11])
else:
signal_obj.start_bit = 0
signal_obj.size = int(row[13])
if row[9] == "Motorola":
signal_obj.is_little_endian = False
else:
signal_obj.is_little_endian = True
signal_obj.initial_value = int(row[21])
signal_obj.add_attribute("GenSigStartValue",signal_obj.initial_value)
signale_sendtype = 0
if row[12] == "Cycle":
signale_sendtype = 0
else:
signale_sendtype = 2
signal_obj.add_attribute("GenSigSendType",signale_sendtype)
if row[14] == "unsigned":
signal_obj.is_float = False
signal_obj.is_signed = False
elif row[14] == "signed":
signal_obj.is_float = False
signal_obj.is_signed = True
else:
signal_obj.is_float = True
signal_obj.is_signed = False
signal_obj.min = float(row[17])
signal_obj.max = float(row[18])
signal_obj.factor = float(row[15])
signal_obj.offset = float(row[16])
if row[23] != None:
signal_obj.unit = row[23]
if row[28] == "RX":
signal_obj.add_receiver(ecu_obj.name)
elif row[28] == "TX":
message_recive = ecu_obj.name
message_sig_group_name = row[8]
if message_sig_group_name != None:
signalGroups = frame_obj.signal_group_by_name(message_sig_group_name)
if signalGroups == None:
frame_obj.add_signal_group(message_sig_group_name,1,signal_obj.name)
else:
signalGroups.add_signal(signal_obj)
if row[24] != None:
sigvalue = row[24]
st=sigvalue.split("\n")
for i in st:
if i == "":
st.remove(i)
d = dict(x.split(":") for x in st)
for k, v in d.items():
signal_obj.add_values(k,v)
frame_obj.add_signal(signal_obj)
#*********** frame type set ***************
signal_obj=canmatrix.Signal()
if row[5] != None:
message_length = int(row[5])
else:
message_length = 0
if row[4] != None:
message_cycle = int(row[4])
if row[1] == "CAN standard":
message_type_is_fd = False
message_type_is_extended = False
elif row[1] == "CANFD standard":
message_type_is_fd = True
message_type_is_extended = False
elif row[1] == "CAN extended":
message_type_is_extended = True
message_type_is_fd = False
elif row[1] == "CANFD extended":
message_type_is_extended = True
message_type_is_fd = True
else:
frame_obj.name = message_name
frame_obj.size = message_length
frame_obj.arbitration_id.id = message_id
frame_obj.cycle_time = message_cycle
frame_obj.is_fd = message_type_is_fd
frame_obj.is_j1939 = message_type_is_extended
if message_recive!= "":
frame_obj.add_transmitter(ecu_obj.name)
self.dbc.add_frame(frame_obj)
message_name = ""
message_id = 0
message_length = 0
message_type = False
message_recive = ""
signalGroups = []
frame_obj = canmatrix.Frame()
# 导出到DBC文件
file_out = open(self.dbc_path, "wb")
canmatrix.formats.dbc.dump(self.dbc,file_out,dbcExportEncoding='utf-8')
file_out.close()
if __name__ == "__main__":
dbc_file_path = "excel.dbc" # Replace with the actual DBC file path
excel_file_path = "dbc.xlsx" # Replace with the desired Excel output path
MyDbc = MyDBC_Obj(dbc_file_path,excel_file_path)
MyDbc.write_dbc()
文章来源:https://blog.csdn.net/weixin_42447823/article/details/135051793
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!