clear;clc;
xlsfile_info = '转速测试_dbc.xls';
dbcfile_info = 'DBC_CANMatrix2.dbc';
%% 获取CAN 矩阵 EXCEL文件
disp('请选择Excel文件...')
[xlsfile_name,xlsfile_path] =uigetfile({'*.xlsx'},'Select the Data Dictionary');
if isequal(xlsfile_name,0)
disp('取消操作')
return
else
disp(['数据文件路径: ', fullfile(xlsfile_path, xlsfile_name)])
end
[~,file_name,~] = fileparts(xlsfile_name);
[~,~,CAN_Matrix_Text] = xlsread(file_name,'CANMatrix');
[column_num,row_num] = size(CAN_Matrix_Text);
fid = fopen(dbcfile_info,'w');
fprintf(fid, 'VERSION ""\n\n\n');
fprintf(fid, 'NS_ :\n');
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');
fprintf(fid, 'BU_:\n\n');
message_num = 1;
val_num = 1;
val_num1 = 1;
MAX_MESSAGE = 100;
MAX_VAL = 50;
MessageID = cell(1,MAX_MESSAGE);
MessageName = cell(1,MAX_MESSAGE);
MessageType = cell(1,MAX_MESSAGE);
MessageCycle = cell(1,MAX_MESSAGE);
Val_ID = cell(1,MAX_VAL);
Val_SignalName = cell(1,MAX_VAL);
Val_SignalVal = cell(1,MAX_VAL);
Val_ID1 = cell(1,MAX_VAL);
Val_SignalName1 = cell(1,MAX_VAL);
Val_SignalVal1 = cell(1,MAX_VAL);
Val_Comment = cell(1,MAX_VAL);
for column_index = 1:(column_num -2)
[~,~,Message_Text] = xlsread(file_name,'CANMatrix',['A',num2str(column_index+2),':R',num2str(column_index+2)]);
%[signal_num,~] = size(Messgae_Text);
if isnan(cell2mat(Message_Text(1,1)))
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},' : ']);
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');
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
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
fprintf(fid,'\n\n\n\n\n');
if val_num1 > 1
val_num1 = val_num1 - 1;
for val_index = 1:val_num1
fprintf(fid,'CM_ SG_ ');
fprintf(fid,num2str(Val_ID1{val_index}));
fprintf(fid,' ');
fprintf(fid,Val_SignalName1{val_index});
fprintf(fid,' ');
fprintf(fid,['"',Val_Comment{val_index}]);
fprintf(fid,'"');
fprintf(fid,';');
fprintf(fid,'\n');
end
fprintf(fid,'\n');
end
if val_num > 1
val_num = val_num - 1;
for val_index = 1:val_num
fprintf(fid,'VAL_ ');
fprintf(fid,num2str(Val_ID{val_index}));
fprintf(fid,' ');
fprintf(fid,Val_SignalName{val_index});
fprintf(fid,' ');
dec_str = strsplit(cell2mat(Val_SignalVal(val_index)),'\n');
[~,dec_num] = size(dec_str);
for dec_index = 1:dec_num
dec_text = strsplit(dec_str{dec_index},':');
dec_value = strsplit(dec_text{1},'0x');
val_value = hex2dec(dec_value{2});
val_text = dec_text{2};
fprintf(fid,num2str(val_value));
fprintf(fid,' ');
fprintf(fid,['"',val_text]);
fprintf(fid,'"');
fprintf(fid,' ');
end
fprintf(fid,';');
fprintf(fid,'\n');
end
end
fprintf(fid,'\n\n\n\n\n');
fclose all;
clear;
disp('DBC exchange ok')
- 1
- 2
- 3
- 4
- 5
- 6
前往页