Excel生成DBC脚本源文件

Excel制作

新建一个Excel,后缀为".xls"

工作本名称改为"CAN_Matrix"

在首行按照列来起名字,在里面只需要填写必须的内容即可。

|--------|-------------------------------|
| 列数 | 名称 |
| 第0列 | Message Name |
| 第1列 | Message Format |
| 第2列 | Message ID |
| 第3列 | Message Length (byte) |
| 第4列 | Message Transmitter ECU |
| 第5列 | Message Receiver ECU |
| 第6列 | Message Send Method |
| 第7列 | Message Cycle Period(ms) |
| 第8列 | Message Cycle Period Fast(ms) |
| 第9列 | Message Delay Time(ms) |
| 第10列 | Message IL Support |
| 第11列 | Message Num Of Repetition |
| 第12列 | Message Start Delay Time(ms) |
| 第13列 | NM Message |
| 第14列 | CANFD BRS |
| 第15列 | Message Comment |
| 第16列 | Signal Name |
| 第17列 | Signal Length (Bit) |
| 第18列 | Signal Start Bit |
| 第19列 | Singal Byte Order |
| 第20列 | Signal Unit |
| 第21列 | Signal Data Type |
| 第22列 | Signal Resolution |
| 第23列 | Signal OffSet |
| 第24列 | Signal Min Value |
| 第25列 | Signal Max Value |
| 第26列 | Singal Inactive Value |
| 第27列 | Singal Send Method |
| 第28列 | Singal Start Value |
| 第29列 | NWM- Wakeup Allowed |
| 第30列 | Signal Value Descriptions |
| 第31列 | Signal Comment |

代码源码

cpp 复制代码
# encoding='utf-8'
import xlrd
from decimal import Decimal

import sys
global Table        #表格
global RowCount     #行数
global ColumnCount  #列数
global OutputFile   #输出文件

ExcelTable = "CAN_Matrix"   #表格
Note = "xin1"               #节点名称

MessageName_Position = 0 #报文名称序号
MessageFormat_Position = 1 #报文格式序号
MessageID_Position = 2 #报文ID序号
MessageLength_Position = 3 #报文长度序号
MessageTransmitterNote_Position = 4 #报文发送节点序号
MessageReceiverNote_Position = 5 #报文接收节点序号
MessageSendMethod_Position  = 6 #报文发送方式序号
MessagePeriod_Position = 7 #报文周期序号
MessagePeriodFast_Position = 8 #报文快速周期序号
MessageDelayTime_Position = 9 #报文延迟时间序号
MessageILSupport_Position = 10 #报文支持IL序号
MessageNumOfRepetition_Position = 11 #报文重发次数序号
MessageStartDelayTime_Position = 12 #报文开始延迟时间序号
MessageNM_Position = 13 #报文网络管理序号
MessageCANFDBRS_Position = 14 #报文CANFDBRS序号
MessageComment_Position = 15 #报文注释序号

SignalName_Position = 16 #信号名称序号
SignalStartLength_Position = 17 #信号长度序号
SignalStartBit_Position = 18 #信号起始位序号
SignalByteOrder_Position = 19 #信号字节序序号
SignalUnit_Position = 20 #信号单位序号
SignalDataType_Position = 21 #信号数据类型序号
SignalResolution_Position = 22 #信号比率序号
SignalOffset_Position = 23 #信号偏移序号
SignalMinValue_Position = 24 #信号最小值序号
SignalMaxValue_Position = 25 #信号最大值序号
SingalInactiveValue_Position = 26 #信号未激活值序号
SingalSendMethod_Position = 27 #信号发送方式序号
SingalStartValue_Position = 28 #信号开始值序号
SingalNWMWakeupAllowed_Position = 29 #信号网管唤醒序号
SignalValueDescriptions_Position = 30 #信号数值描述序号
SignalComment_Position = 31 #信号标注序号


def remove_exponent(num):  # 去掉读取到的数值的小数点后边的0
    return num.to_integral() if num == num.to_integral() else num.normalize()
  
def Read_Base_Infomation(): # 获取基本信息
    global Table        #表格
    global RowCount     #行数
    global ColumnCount  #列数
    global OutputFile   #输出文件
    
    if len(sys.argv) > 1:
        InputFilePath = sys.argv[1]
    else:
        InputFilePath = "DBCDemo.xls"
    #print(f"InputFilePath:{InputFilePath}\n")
    
    LastIndex = InputFilePath.rfind('\\')
    StartIndex = InputFilePath.find('.xls', LastIndex)

    InputFileSuffix = InputFilePath[LastIndex + 1:]
    #print(f"InputFileSuffix:{InputFileSuffix}\n")
    
    Xlsx = xlrd.open_workbook(InputFileSuffix)    # 找到输入文件
    Table = Xlsx.sheet_by_name(ExcelTable)  # 找到sheet
    RowCount = Table.nrows                  # 提取行数
    ColumnCount = Table.ncols               # 提取列数
    
    InputFileNoSuffix = InputFilePath[LastIndex + 1:StartIndex - 3]#输出文件
    #print(f"InputFileNoSuffix:{InputFileNoSuffix}\n")
    OutputFile = InputFileNoSuffix + ".dbc"
    #print(f"OutputFile:{OutputFile}\n")
    
    
    
def Output_DBC_Head():#打印文件头部
    global OutputFile   #输出文件
    with open(OutputFile, "w",encoding="utf-8") as f:
        f.write("VERSION \"\"")
        f.write("\n\n")
        f.write('''
NS_ : 
	NS_DESC_
	CM_
	BA_DEF_
	BA_
	VAL_
	CAT_DEF_
	CAT_
	FILTER
	BA_DEF_DEF_
	EV_DATA_
	ENVVAR_DATA_
	SGTYPE_
	SGTYPE_VAL_
	BA_DEF_SGTYPE_
	BA_SGTYPE_
	SIG_TYPE_REF_
	VAL_TABLE_
	SIG_GROUP_
	SIG_VALTYPE_
	SIGTYPE_VALTYPE_
	BO_TX_BU_
	BA_DEF_REL_
	BA_REL_
	BA_DEF_DEF_REL_
	BU_SG_REL_
	BU_EV_REL_
	BU_BO_REL_
	SG_MUL_VAL_

BS_:\n\n''')
        f.write(f"BU_: {Note}\n\n\n")
   
def Output_MessageMappingSignal():#报文和信号映射关系
    global Table        #表格
    global RowCount     #行数
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+",encoding="utf-8") as f:
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            MessageName = Table.cell_value(i, MessageName_Position)# 报文名称
            if MessageName == "":   # 本行为空
                print(f"Empty Message exists\n")
                return
            else:                   # 本行有内容
                #MessageID = Table.cell_value(i, MessageID_Position)
                MessageID = int((Table.cell_value(i, MessageID_Position)), 16)# 报文ID十六进制
                #print(f"MessageID:{MessageID}\n")
                MessageLength = remove_exponent(Decimal(Table.cell_value(i, MessageLength_Position)).quantize(Decimal("0")))# 报文长度
                MessageTransmitterNote = Table.cell_value(i, MessageTransmitterNote_Position)# 报文发送节点
                MessageReceiverNote = Table.cell_value(i, MessageReceiverNote_Position)# 报文接收节点
                
                if MessageTransmitterNote == Note :# 写入报文ID信息
                    f.write(f"BO_ {MessageID} {MessageName}: {MessageLength} {Note}\n")  
                else:
                    f.write(f"BO_ {MessageID} {MessageName}: {MessageLength} Vector__XXX\n")               
                
                while 1:#扫描这个报文里面的所有信号
                    if (i == RowCount) or (MessageID != int((Table.cell_value(i, MessageID_Position)), 16)):#后面没有该报文的信号或者已经到尽头
                        f.write("\n")
                        break
                    else:# 后面还有该报文的信号
                        SingleName = Table.cell_value(i, SignalName_Position)#信号名称
                        #print(f"SingleName:{SingleName}\n")
                        
                        SignalStartBit = remove_exponent(Decimal(Table.cell_value(i, SignalStartBit_Position)).quantize(Decimal("0")))#信号起始位,去除小数点后边的0以及保留小数点后O位
                        #print(f"SignalStartBit:{SignalStartBit}\n")
                                    
                        SignalLength = remove_exponent(Decimal(Table.cell_value(i, SignalStartLength_Position)).quantize(Decimal("0"))) #信号长度
                        #print(f"SignalLength:{SignalLength}\n")
                        
                        if Table.cell_value(i, SignalByteOrder_Position) == "Motorola MSB" or Table.cell_value(i, SignalByteOrder_Position) == "Motorola" or Table.cell_value(i, SignalByteOrder_Position) == "":#信号字节序
                            SignalByteOrder = "@0"
                        else:
                            SignalByteOrder = "@1"
                        #print(f"SignalByteOrder:{SignalByteOrder}\n")
                        
                        if Table.cell_value(i, SignalDataType_Position) == "Unsigned" or Table.cell_value(i, SignalDataType_Position) == "unsigned" or Table.cell_value(i, SignalDataType_Position) == "":#信号有无符号
                            SignalDateType = "+"
                        else:
                            SignalDateType = "-"
                        #print(f"SignalDateType:{SignalDateType}\n")
                        
                        if Table.cell_value(i, SignalUnit_Position) == "":#信号单位
                            SignalUnit = ""
                        else:
                            SignalUnit = Table.cell_value(i, SignalUnit_Position)
                        #print(f"SignalUnit:{SignalUnit}\n")
                        
                        if Table.cell_value(i, SignalResolution_Position) == "":#信号精度
                            SignalResolution = 1
                        else:
                            SignalResolution = remove_exponent(Decimal(Table.cell_value(i, SignalResolution_Position)).quantize(Decimal("0.00000")))

                        if Table.cell_value(i, SignalOffset_Position) == "":#信号偏移
                            SignalOffset = 0
                        else:
                            SignalOffset = remove_exponent(Decimal(Table.cell_value(i, SignalOffset_Position)).quantize(Decimal("0.00000")))
                        #print(f"SignalOffset:{SignalOffset}\n")                         

                        if Table.cell_value(i, SignalOffset_Position) == "":
                            SignalMinValue = 0;
                        else:
                            HexValue = str(Table.cell_value(i, SignalMinValue_Position))#信号最小值
                            if HexValue.startswith('0x') or HexValue.startswith('0X'):
                                SignalMinValue = int(HexValue, 16)  # 将字符串转换为10进制整数
                            else:
                                SignalMinValue = remove_exponent(Decimal(Table.cell_value(i, SignalMinValue_Position)).quantize(Decimal("0")))
                            #print(f"SignalMinValue:{SignalMinValue}\n") 

                        if Table.cell_value(i, SignalMaxValue_Position) == "":
                            SignalMaxValue = 0xFF;
                        else:           
                            HexValue = str(Table.cell_value(i, SignalMaxValue_Position))#信号最大值
                            if HexValue.startswith('0x') or HexValue.startswith('0X'):
                                SignalMaxValue = int(HexValue, 16)  # 将字符串转换为10进制整数
                            else:
                                SignalMaxValue = remove_exponent(Decimal(Table.cell_value(i, SignalMaxValue_Position)).quantize(Decimal("0")))        
                            #print(f"SignalMaxValue:{SignalMaxValue}\n") 
                        
                        if MessageTransmitterNote == Note :
                            f.write(f" SG_ {SingleName} : {SignalStartBit}|{SignalLength}{SignalByteOrder}{SignalDateType} ({SignalResolution},{SignalOffset}) [{SignalMinValue}|{SignalMaxValue}] \"{SignalUnit}\" Vector__XXX\n")
                        else :
                            f.write(f" SG_ {SingleName} : {SignalStartBit}|{SignalLength}{SignalByteOrder}{SignalDateType} ({SignalResolution},{SignalOffset}) [{SignalMinValue}|{SignalMaxValue}] \"{SignalUnit}\" {Note} \n")    
 
                        i = i + 1 #扫描下一行
                        #print(f"i:{i}\n")
        f.write("\n")

            
def Output_DBC_Comment(): 
    global Note
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+", encoding="GB2312") as f:     
        f.write("""
BA_DEF_ SG_  "GenSigStartValue" FLOAT 0 100000000000;
BA_DEF_ SG_  "GenSigSendType" ENUM  "Cyclic","OnWrite","OnWriteWithRepetition","OnChange","OnChangeWithRepetition","IfActive","IfActiveWithRepetition","NoSigSendType","NotUsed","NotUsed","NotUsed","NotUsed","NotUsed";
BA_DEF_ SG_  "GenSigInactiveValue" INT 0 100000;
BA_DEF_ SG_  "NWM-WakeupAllowed" ENUM  "No","Yes";
BA_DEF_ BO_  "GenMsgSendType" ENUM  "Cyclic","NotUsed","NotUsed","NotUsed","NotUsed","Cyclic","NotUsed","IfActive","NoMsgSendType","NotUsed","vector_leerstring";
BA_DEF_ BO_  "GenMsgNrOfRepetition" INT 0 999999;
BA_DEF_ BO_  "GenMsgDelayTime" INT 0 1000;
BA_DEF_ BO_  "GenMsgCycleTime" INT 0 50000;
BA_DEF_ BO_  "GenMsgCycleTimeFast" INT 0 50000;
BA_DEF_ BO_  "GenMsgILSupport" ENUM  "No","Yes";
BA_DEF_ BO_  "NmMessage" ENUM  "no","yes";
BA_DEF_ BO_  "GenMsgStartDelayTime" INT 0 65535;
BA_DEF_ BU_  "NmStationAddress" INT 0 63;
BA_DEF_ BU_  "NmNode" ENUM  "no","yes";
BA_DEF_  "NmBaseAddress" HEX 1024 1087;
BA_DEF_ BO_  "CANFD_BRS" ENUM  "0","1";
BA_DEF_  "DBName" STRING ;
BA_DEF_  "BusType" STRING ;
BA_DEF_ BU_  "NodeLayerModules" STRING ;
BA_DEF_ BU_  "ECU" STRING ;
BA_DEF_ BU_  "CANoeJitterMax" INT 0 0;
BA_DEF_ BU_  "CANoeJitterMin" INT 0 0;
BA_DEF_ BU_  "CANoeDrift" INT 0 0;
BA_DEF_ BU_  "CANoeStartDelay" INT 0 0;
BA_DEF_ BO_  "VFrameFormat" ENUM  "StandardCAN","ExtendedCAN","reserved","reserved","reserved","reserved","reserved","reserved","reserved","reserved","reserved","reserved","reserved","reserved","StandardCAN_FD","ExtendedCAN_FD";
BA_DEF_DEF_  "GenSigStartValue" 0;
BA_DEF_DEF_  "GenSigSendType" "Cyclic";
BA_DEF_DEF_  "GenSigInactiveValue" 0;
BA_DEF_DEF_  "NWM-WakeupAllowed" "";
BA_DEF_DEF_  "GenMsgSendType" "NoMsgSendType";
BA_DEF_DEF_  "GenMsgNrOfRepetition" 0;
BA_DEF_DEF_  "GenMsgDelayTime" 0;
BA_DEF_DEF_  "GenMsgCycleTime" 0;
BA_DEF_DEF_  "GenMsgCycleTimeFast" 0;
BA_DEF_DEF_  "GenMsgILSupport" "Yes";
BA_DEF_DEF_  "NmMessage" "no";
BA_DEF_DEF_  "GenMsgStartDelayTime" 0;
BA_DEF_DEF_  "NmStationAddress" 0;
BA_DEF_DEF_  "NmNode" "no";
BA_DEF_DEF_  "NmBaseAddress" 1024;
BA_DEF_DEF_  "CANFD_BRS" "1";
BA_DEF_DEF_  "DBName" "";
BA_DEF_DEF_  "BusType" "";
BA_DEF_DEF_  "NodeLayerModules" "";
BA_DEF_DEF_  "ECU" "";
BA_DEF_DEF_  "CANoeJitterMax" 0;
BA_DEF_DEF_  "CANoeJitterMin" 0;
BA_DEF_DEF_  "CANoeDrift" 0;
BA_DEF_DEF_  "CANoeStartDelay" 0;
BA_DEF_DEF_  "VFrameFormat" "StandardCAN";
BA_ "BusType" "CAN FD";
""")
        f.write(f'BA_ "DBName" \"{Note}\";\n\n')

        
def Output_SignalAttributes():#添加信号属性
    global Table        #表格
    global RowCount     #行数
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+", encoding="utf-8") as f:
    #with open(OutputFile, "a+", encoding="GB2312") as f:  
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            SingleName = Table.cell_value(i, SignalName_Position)#信号名称
            #print(f"SingleName:{SingleName}\n")
            MessageID = int((Table.cell_value(i, MessageID_Position)), 16)# 报文ID十六进制

            if Table.cell_value(i, SingalInactiveValue_Position) != "":#信号未激活值
                SingalInactiveValue = remove_exponent(Decimal(Table.cell_value(i, SingalInactiveValue_Position)).quantize(Decimal("0")))
                #print(f"SingalInactiveValue:{SingalInactiveValue}\n")
                f.write(f"BA_ \"GenSigInactiveValue\" SG_ {MessageID} {SingleName} {SingalInactiveValue};\n")
            
            if Table.cell_value(i, SingalSendMethod_Position) != "":#信号发送方式
                SingalSendMethod = Table.cell_value(i, SingalSendMethod_Position)
                #print(f"SingalSendMethod:{SingalSendMethod}\n")
                if SingalSendMethod == "Cyclic":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 0;\n")
                elif SingalSendMethod == "OnWrite":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 1;\n")
                elif SingalSendMethod == "OnWriteWithRepetition":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 2;\n")              
                elif SingalSendMethod == "OnChange":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 3;\n")
                elif SingalSendMethod == "OnChangeWithRepetition":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 4;\n")
                elif SingalSendMethod == "IfActive":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 5;\n")
                elif SingalSendMethod == "IfActiveWithRepetition":
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 6;\n")
                else:
                    f.write(f"BA_ \"GenSigSendType\" SG_ {MessageID} {SingleName} 0;\n")
 
            if Table.cell_value(i, SingalStartValue_Position) != "":#信号开始值
                SingalStartValue = remove_exponent(Decimal(Table.cell_value(i, SingalStartValue_Position)).quantize(Decimal("0")))
                #print(f"SingalStartValue:{SingalStartValue}\n")
                f.write(f"BA_ \"GenSigStartValue\" SG_ {MessageID} {SingleName} {SingalStartValue};\n")
                
            if Table.cell_value(i, SingalNWMWakeupAllowed_Position) != "":#信号网管唤醒序号
                SingalNWMWakeupAllowed = Table.cell_value(i, SingalNWMWakeupAllowed_Position)
                #print(f"SingalNWMWakeupAllowed:{SingalNWMWakeupAllowed}\n")
                if SingalNWMWakeupAllowed == "Yes" or SingalNWMWakeupAllowed == "YES":
                    f.write(f"BA_ \"NWM-WakeupAllowed\" SG_ {MessageID} {SingleName} 1;\n")
                else:
                    f.write(f"BA_ \"NWM-WakeupAllowed\" SG_ {MessageID} {SingleName} 0;\n")              
            
            i = i + 1
            
def Output_SignalValue():#打印信号值定义
    global RowCount     #行数
    global Table 
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+", encoding="utf-8") as f:
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            if Table.cell_value(i, SignalName_Position) != "":
                MessageID = int((Table.cell_value(i, MessageID_Position)), 16)
                if Table.cell_value(i, SignalValueDescriptions_Position) != "":
                    SingleName = Table.cell_value(i, SignalName_Position)
                    SignalValueDescription = Table.cell_value(i, SignalValueDescriptions_Position).strip().replace("\r", "").split("\n")
                    SignalValueDescription = list(reversed(SignalValueDescription))
                    if SignalValueDescription != [""]:
                        #处理列表元素使其生成DBC文件格式中一样的格式
                        ELE = "\" ".join(SignalValueDescription).replace("0x","").replace(":"," \"")+"\""
                        f.write(f"VAL_ {MessageID} {SingleName} {ELE} ;\n") 
            i = i + 1      
            
def Output_SignalComment():#添加信号注释
    global Table        #表格
    global RowCount     #行数
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+", encoding="utf-8") as f:#信号注释,不提倡添加中文注释
    #with open(OutputFile, "a+", encoding="GB2312") as f:  
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            if Table.cell_value(i, SignalName_Position) != "":
                MessageID = int((Table.cell_value(i, MessageID_Position)), 16)
                if Table.cell_value(i, SignalValueDescriptions_Position) != "":
                    SingleName = Table.cell_value(i, SignalName_Position)
                    if Table.cell_value(i, SignalComment_Position) != "":#信号标注序号
                        SignalComment = Table.cell_value(i, SignalComment_Position).strip().replace("\n", "").replace("\r", "")
                        f.write(f"CM_ SG_ {MessageID} {SingleName} \"{SignalComment}\";\n")
            
            i = i + 1
          

def Output_MessageAttribute():#打印报文属性
    global RowCount     #行数
    global Table
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+",encoding="utf-8") as f:
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            MessageID = int((Table.cell_value(i, MessageID_Position)), 16)# 报文ID
            
            if Table.cell_value(i, MessageFormat_Position) != "":# 报文格式
                MessageFormat = Table.cell_value(i, MessageFormat_Position)
                #print(f"MessageFormat:{MessageFormat}\n")
                if MessageFormat == "CAN Standard":  
                    f.write(f"BA_ \"VFrameFormat\" BO_ {MessageID} 0;\n")
                elif MessageFormat == "CAN Extended":               
                    f.write(f"BA_ \"VFrameFormat\" BO_ {MessageID} 1;\n") 
                elif MessageFormat == "CAN FD Standard":              
                    f.write(f"BA_ \"VFrameFormat\" BO_ {MessageID} 14;\n")                      
                elif MessageFormat == "CAN FD Extended":              
                    f.write(f"BA_ \"VFrameFormat\" BO_ {MessageID} 15;\n")           
                else:              
                    f.write(f"BA_ \"VFrameFormat\" BO_ {MessageID} 0;\n")  

            if Table.cell_value(i, MessageSendMethod_Position) != "":# 报文发送方式
                MessageSendMethod = Table.cell_value(i, MessageSendMethod_Position)
                #print(f"MessageSendMethod:{MessageSendMethod}\n")
                if MessageSendMethod == "Cyclic":
                    f.write(f"BA_ \"GenMsgSendType\" BO_ {MessageID} 0;\n")   
                elif MessageSendMethod == "IfActive":           
                    f.write(f"BA_ \"GenMsgSendType\" BO_ {MessageID} 7;\n")     
                else:
                    f.write(f"BA_ \"GenMsgSendType\" BO_ {MessageID} 0;\n")     
            
            if Table.cell_value(i, MessagePeriod_Position) != "":            
                MessagePeriod = Table.cell_value(i, MessagePeriod_Position)
                if MessagePeriod != "Event":# 报文周期      
                    MessagePeriod = remove_exponent(Decimal(Table.cell_value(i, MessagePeriod_Position)).quantize(Decimal("0")))                
                    #print(f"MessagePeriod:{MessagePeriod}\n")
                    f.write(f"BA_ \"GenMsgCycleTime\" BO_ {MessageID} {MessagePeriod};\n") 

            if Table.cell_value(i, MessagePeriodFast_Position) != "":#报文快速周期 
                MessagePeriodFast = remove_exponent(Decimal(Table.cell_value(i, MessagePeriodFast_Position)).quantize(Decimal("0")))         
                f.write(f"BA_ \"GenMsgCycleTimeFast\" BO_ {MessageID} {MessagePeriodFast};\n") 

            if Table.cell_value(i, MessageDelayTime_Position) != "":#报文延迟时间
                MessageDelayTime = remove_exponent(Decimal(Table.cell_value(i, MessageDelayTime_Position)).quantize(Decimal("0")))            
                f.write(f"BA_ \"GenMsgDelayTime\" BO_ {MessageID} {MessageDelayTime};\n")  
                
            if Table.cell_value(i, MessageILSupport_Position) != "":#报文支持IL序号
                MessageILSupport = Table.cell_value(i, MessageILSupport_Position)
                if MessageILSupport == "Yes" or MessageILSupport == "YES":
                    f.write(f"BA_ \"GenMsgILSupport\" BO_ {MessageID} 1;\n") 
                else:
                    f.write(f"BA_ \"GenMsgILSupport\" BO_ {MessageID} 0;\n")    

            if Table.cell_value(i, MessageNumOfRepetition_Position) != "":#报文重发次数
                MessageNumOfRepetition = remove_exponent(Decimal(Table.cell_value(i, MessageNumOfRepetition_Position)).quantize(Decimal("0")))              
                f.write(f"BA_ \"GenMsgNrOfRepetition\" BO_ {MessageID} {MessageNumOfRepetition};\n") 
                
            if Table.cell_value(i, MessageStartDelayTime_Position) != "":#报文开始延迟时间
                MessageStartDelayTime = remove_exponent(Decimal(Table.cell_value(i, MessageStartDelayTime_Position)).quantize(Decimal("0")))            
                f.write(f"BA_ \"GenMsgStartDelayTime\" BO_ {MessageID} {MessageStartDelayTime};\n") 
                
            if Table.cell_value(i, MessageNM_Position) != "":#报文网络管理
                MessageNM = Table.cell_value(i, MessageNM_Position)        
                if MessageNM == "Yes" or MessageNM == "YES":
                    f.write(f"BA_ \"NmMessage\" BO_ {MessageID} 1;\n") 
                else:
                    f.write(f"BA_ \"NmMessage\" BO_ {MessageID} 0;\n")
                
            if Table.cell_value(i, MessageCANFDBRS_Position) != "":#报文CANFDBRS
                MessageCANFDBRS = Table.cell_value(i, MessageCANFDBRS_Position)                                          
                if MessageCANFDBRS == "Yes" or MessageCANFDBRS == "YES":
                    f.write(f"BA_ \"CANFD_BRS\" BO_ {MessageID} 1;\n") 
                else:
                    f.write(f"BA_ \"CANFD_BRS\" BO_ {MessageID} 0;\n")

            i = i + 1

def Output_MessageComment():#打印报文注释
    global Table        #表格
    global RowCount     #行数
    global OutputFile   #输出文件
    
    with open(OutputFile, "a+", encoding="GB2312") as f:#打印报文注释  
        i = 1 #跳过表头
        while i < RowCount:#逐行轮询
            MessageID = int((Table.cell_value(i, MessageID_Position)), 16)# 报文ID
            MessageComment = Table.cell_value(i, MessageComment_Position)# 报文注释
            if MessageComment != "":
                f.write(f"CM_ BO_ {MessageID} \"{MessageComment}\";\n") 
            i = i + 1
       
if __name__ == "__main__" :

    print(f"Start")#打印的顺序不要乱,不然会打不开文件
    
    Read_Base_Infomation()
    
    Output_DBC_Head()
    
    Output_MessageMappingSignal()
    
    Output_SignalComment()
    
    Output_MessageComment() 
    
    Output_DBC_Comment()  
    
    Output_SignalAttributes()
    
    Output_MessageAttribute()
    
    Output_SignalValue()

    print(f"Success!!")
    
相关推荐
学习溢出17 分钟前
【网络安全】逆向工程 练习示例
网络·安全·网络安全·渗透测试·逆向工程
枫叶红花21 分钟前
【Linux系统编程】:信号(2)——信号的产生
linux·运维·服务器
_微风轻起32 分钟前
linux下网络编程socket&select&epoll的底层实现原理
linux·网络
Zmxcl-0072 小时前
IIS解析漏洞
服务器·数据库·microsoft
Stark、2 小时前
【Linux】文件IO--fcntl/lseek/阻塞与非阻塞/文件偏移
linux·运维·服务器·c语言·后端
vortex52 小时前
Burp与其他安全工具联动及代理设置教程
网络·安全
xserver23 小时前
ensp 基于端口安全的财务部网络组建
网络·安全
一个不秃头的 程序员4 小时前
服务器上加入SFTP------(小白篇 1)
运维·服务器
fnd_LN4 小时前
Linux文件目录 --- 复制命令CP、递归复制目录、软连接、硬链接
linux·运维·服务器