Convert EXCEL form to DBC database through MATLAB

Table of contents

1. Transformation principle

2. Implementation process

3. Application fields

4. Core program


       In modern engineering and communication fields, data exchange and sharing are crucial. Controller Area Network (CAN) is a communication protocol widely used in automotive and industrial fields, and CAN database (DBC) is a key component in CAN network, which is used to describe messages, signals and nodes on the network.

1. Transformation principle

       A DBC file in a CAN network contains the definition, layout and properties of all messages and signals in the network. The basic idea of ​​converting an EXCEL form to a DBC file is to map the data in the EXCEL form to the message and signal definitions in the DBC file. Specifically, the following need to be addressed:

  1. Message definition : In the EXCEL table, each row can represent a CAN message, including information such as message ID, message name, and sending cycle.

  2. Signal definition : The signal definition in each line also includes signal name, start bit, length, factor and other information.

  3. Node definition : The nodes in the CAN network also need to be defined in the DBC file, including node name, node type and other information.

2. Implementation process

The implementation process of converting an EXCEL table into a DBC database through MATLAB can be divided into the following steps:

  1. Read EXCEL data : Use MATLAB's built-in functions (such as xlsread) to read data in EXCEL tables, including messages, signals and node information.

  2. Create DBC file : Use MATLAB to create a text file as the final DBC file.

  3. Write message definition : write the message information read from EXCEL into the DBC file, and arrange it according to the format of the DBC file.

  4. Write signal definition : write the signal information into the DBC file, and also need to arrange it according to the specified format.

  5. Write node definition : write node information into the DBC file, also need to follow the specified format.

  6. Save DBC file : Save the generated DBC file, which means the process of converting EXCEL to DBC is completed.

3. Application fields

Converting EXCEL tables into DBC databases has important applications in the field of CAN communication:

  • Automotive field : In automobiles, CAN network is the main communication method between vehicle electronic control units (ECUs). Converting vehicle parameters, status information, etc. into DBC files can facilitate system development and integration.

  • Industrial automation : In industrial automation, CAN network is widely used in control systems, and the control information of industrial equipment is defined and transmitted through DBC files, which is convenient for system integration and maintenance.

  • Internet of Things : With the development of the Internet of Things, more and more devices and sensors use the CAN network for communication, and converting device data into DBC files is helpful for data transmission and processing.

  • Aerospace : In the field of aerospace, the CAN network is used for data transmission and communication in aircraft, and converting aircraft parameters into DBC files is helpful for flight control and monitoring.

        Converting EXCEL tables into DBC databases through MATLAB is an important method to realize data conversion and sharing in the field of CAN communication. It facilitates data transmission and communication in areas such as vehicle control, industrial automation, IoT, and aerospace. Through simple data processing and file generation, the seamless connection between different data formats is realized, and the efficiency of engineering development and the convenience of data exchange are improved.

4. Core program

....................................................................
% 打开要写入的DBC文件
fid = fopen(dbcfile_info,'w');
fprintf(fid, 'VERSION ""\n\n\n');
fprintf(fid, 'NS_ :\n');% ...(省略一些NS_的格式定义)
fprintf(fid, '	NS_DESC_\n');
fprintf(fid, '	CM_\n');
fprintf(fid, '	BA_DEF_\n');
fprintf(fid, '  BA_\n');
fprintf(fid, '	VAL_\n');
fprintf(fid, '	CAT_DEF_\n');
fprintf(fid, '	CAT_\n');
fprintf(fid, '	FILTER\n');
fprintf(fid, '	BA_DEF_DEF_\n');
fprintf(fid, '	EV_DATA_\n');
fprintf(fid, '	ENVVAR_DATA_\n');
fprintf(fid, '	SGTYPE_\n');
fprintf(fid, '	SGTYPE_VAL_\n');
fprintf(fid, '	BA_DEF_SGTYPE_\n');
fprintf(fid, '	BA_SGTYPE_\n');
fprintf(fid, '	SIG_TYPE_REF_\n');
fprintf(fid, '	VAL_TABLE_\n');
fprintf(fid, '	SIG_GROUP_\n');
fprintf(fid, '	SIG_VALTYPE_\n');
fprintf(fid, '	SIGTYPE_VALTYPE_\n');
fprintf(fid, '	BO_TX_BU_\n');
fprintf(fid, '	BA_DEF_REL_\n');
fprintf(fid, '	BA_REL_\n');
fprintf(fid, '	BA_DEF_DEF_REL_\n');
fprintf(fid, '	BU_SG_REL_\n');
fprintf(fid, '	BU_EV_REL_\n');
fprintf(fid, '	BU_BO_REL_\n');
fprintf(fid, '	SG_MUL_VAL_\n\n');
fprintf(fid, 'BS_:\n\n');
................................................................
for column_index = 1:(column_num -2)
    [~,~,Message_Text] = xlsread(file_name,'CANMatrix',['A',num2str(column_index+2),':R',num2str(column_index+2)]);
    % 从Excel中读取消息的相关信息
    
    if isnan(cell2mat(Message_Text(1,1)))% 如果读取到的消息ID为空,终止循环
        break;
    end
    % 保存消息的相关信息
    MessageID(message_num) = Message_Text(1,1);
    if ~((message_num > 1) && isequal(MessageID{message_num},MessageID{message_num - 1}))
           

    MessageName(message_num) = Message_Text(1,2);
    MessageType(message_num) = Message_Text(1,3);
    MessageCycle(message_num) = Message_Text(1,4);

    SignalName = Message_Text(1:end,5);
    SignalName1 = Message_Text(1:end,5);
    ByteOrder = Message_Text(1:end,6);
    DateType = Message_Text(1:end,7);
    ByteNum = Message_Text(1:end,8);
    BitNum = Message_Text(1:end,9);
    Length = Message_Text(1:end,10);
    StartBit = Message_Text(1:end,11);
    Offset = Message_Text(1:end,12);
    Factor = Message_Text(1:end,13);
    Max = Message_Text(1:end,14);
    Min = Message_Text(1:end,15);
    Unit = Message_Text(1:end,16);
    ValueDesc = Message_Text(1:end,17);
    Comment = Message_Text(1:end,18);
    
    fprintf(fid, '\nBO_ ');
    fprintf(fid, MessageID{message_num});
    fprintf(fid, '  ');
    fprintf(fid, MessageName{message_num});
    fprintf(fid, ': 8 Vector__XXX\n');

    [signal_num,~] = size(SignalName);
    for index = 1:signal_num    % 遍历消息的每个信号
        fprintf(fid, [' SG_ ',SignalName{index},' : ']);% 将信号相关信息写入DBC文件
        fprintf(fid,num2str(StartBit{index}));
        fprintf(fid,'|');
        fprintf(fid,num2str(Length{index}));
        fprintf(fid,'@');
        if isequal(ByteOrder{index}, 'Motorola')
            fprintf(fid,'0');
        else
            fprintf(fid,'1');
        end
        if isequal(DateType{index}, 'signed')
        fprintf(fid,'- ');
        else
            fprintf(fid,'+ ');
        end
        fprintf(fid,'(');
        fprintf(fid,num2str(Factor{index}));
        fprintf(fid,',');
        fprintf(fid,num2str(Offset{index}));
        fprintf(fid,') [');
        fprintf(fid,num2str(Max{index}));
        fprintf(fid,'|');
        fprintf(fid,num2str(Min{index}));
        fprintf(fid,'] "" Vector__XXX');
        fprintf(fid, '\n');
        % 如果有ValueDesc,保存到Val_ID和Val_SignalName中
        if ~isnan(ValueDesc{index})
            Val_ID(val_num) = MessageID(message_num);
            Val_SignalName(val_num) = SignalName(index);
            Val_SignalVal(val_num) = ValueDesc(index);
            val_num = val_num + 1;
        end
        % 如果有Comment,保存到Val_ID1和Val_SignalName1中
        if ~isnan(Comment{index})
            Val_ID1(val_num1) = MessageID(message_num);
            Val_SignalName1(val_num1) = SignalName(index);
            Val_Comment(val_num1) = Comment(index);
            val_num1 = val_num1 + 1;
        end
    end
 
    message_num = message_num + 1;

    end
end
...................................................................

% 关闭文件
fclose all;
clear;
disp('DBC exchange ok')
up2190

Guess you like

Origin blog.csdn.net/ccsss22/article/details/132286844