function myfun()
[FileName,PathName,FilterIndex] = uigetfile({'*.xlsx';'*.xls'},'Select');
[num txt CANInformation]=xlsread([PathName FileName],1);
MessageNum=size(CANInformation);
MessageNum=MessageNum(1);
MessageName=cell(500,1);
MessageName{1}=CANInformation(2,1);
MessageTemp=CANInformation(2,1);
MessageID='0';
n=1;
for i=2:MessageNum
if isequal(MessageTemp,CANInformation(i,1))
disp('1');
else
n=n+1;
MessageName{n}=CANInformation(i,1);
MessageTemp=CANInformation(i,1);
end
end
fid=fopen('test.dbc','w+');
header={
'VERSION ""'
'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_:'};
for i=1:length(header)
fprintf(fid,'%s\n',(header{i}));
end
%BU_:
%VAL_TABLE_ ABC 3 "Description for the value '0x3'" 2 "Description for the value '0x2'" 1 "Description for the value '0x1'" 0 "Description for the value '0x0'" ;
fprintf(fid,'%s\n','BU_:');
% for i=2:MessageNum
% if ~isequal(CANInformation(i,18),'') || ~isequal(CANInformation(i,18),'-')
% valuelist=cell2mat(CANInformation(i,18));
% ValueDescription=cell2mat(CANInformation(i,19));
% valtable=['VAL_TABLE_'];
% fprintf(fid,'%s/n',valtable);
% end
% end
% BO_ 2147483921 ELP_CMD: 8 Vector__XXX
MsgWrtFlg=0;
i=2;
n=1;
while(i<=MessageNum)
if isequal(MessageName{n},CANInformation(i,1))
if isequal('Standard' ,cell2mat(CANInformation(i,3)))
MessageID=num2str(hex2dec(num2str(cell2mat(CANInformation(i,2)))));
elseif isequal('Extended' ,cell2mat(CANInformation(i,3)))
hexID=(cell2mat(CANInformation(i,2)));
decID=hex2dec(num2str(hexID+80000000));
MessageID=num2str((decID));
else
error(['The ' cell2mat(MessageName{n}) ' MessageID Or ID_Format error']);
end
if (MsgWrtFlg==0)
MessageFormat=['BO_ ' MessageID ' ' cell2mat(MessageName{n}) ': 8 Vector__XXX'];
MsgWrtFlg=1;
fprintf(fid,'%s\n',MessageFormat);
end
%% SG_ ELP_EN_Pump : 0|1@1+ (1,0) [0|1] "" Vector__XXX
SignalName=cell2mat(CANInformation(i,7));
StartBit=num2str(cell2mat(CANInformation(i,8)));
SignalLength=num2str(cell2mat(CANInformation(i,9)));
if isequal('Intel',cell2mat(CANInformation(i,10)));
ByteOrder='@1';
elseif isequal('Motorola',cell2mat(CANInformation(i,10)));
ByteOrder='@0';
else
error(['The ' cell2mat(MessageName{n}) ' ByteOrder error']);
end
if isequal('Unsigned',(cell2mat(CANInformation(i,11))));
ValueType='+';
elseif isequal('Signed',cell2mat(CANInformation(i,11)));
ValueType='-';
else
error(['The ' cell2mat(MessageName{n}) ' ValueType error']);
end
%InitialValue=num2str(cell2mat(CANInformation(i,12)));
Factor=num2str(cell2mat(CANInformation(i,13)));
Offset=num2str(cell2mat(CANInformation(i,14)));
Minimum=num2str(cell2mat(CANInformation(i,15)));
Maximum=num2str(cell2mat(CANInformation(i,16)));
Unit=cell2mat(CANInformation(i,17));
% SG_ ELP_CMD_Speed : 16|16@1+ (1,0) [0|10000] "V" Vector__XXX
SignalFormat=[' SG_ ' SignalName ' : ' StartBit '|' SignalLength ByteOrder ValueType...
' (' Factor ',' Offset ') ' '[' Minimum '|' Maximum '] ' '"' Unit '"' ' Vector__XXX'];
fprintf(fid,'%s\n',SignalFormat);
else
MsgWrtFlg=0;
n=n+1;
i=i-1;
end
i=i+1;
end
%VAL_ 2147483649 ELP_EN_Pump 3 "Description for the value '0x3'" 2 "Description for the value '0x2'" 1 "Description for the value '0x1'" 0 "Description for the value '0x0'" ;
for i=2:MessageNum
if (~isequal(cell2mat(CANInformation(i,18)),'') && ~isequal(cell2mat(CANInformation(i,18)),'-'))
if isequal('Standard' ,cell2mat(CANInformation(i,3)))
MessageID=num2str(hex2dec(num2str(cell2mat(CANInformation(i,2)))));
elseif isequal('Extended' ,cell2mat(CANInformation(i,3)))
hexID=(cell2mat(CANInformation(i,2)));
decID=hex2dec(num2str(hexID+80000000));
MessageID=num2str(hex2dec(ID));
else
error(['the ' cell2mat(MessageName{n}) ' MessageID Or ID_Format error']);
end
SignalName=cell2mat(CANInformation(i,7));
valuelist=cell2mat(CANInformation(i,18));
ValueDescription=cell2mat(CANInformation(i,19));
val=regexp(valuelist, ';', 'split');
description=regexp(ValueDescription, ';', 'split');
valtilte=['VAL_ ' MessageID ' ' SignalName ' '];
fprintf(fid,'%s',valtilte);
for i=1:length(val)-1
valhex=val{i};
valdescription=description{i};
valhex(find(isspace(valhex)))=[];
valdescription(find(isspace(valdescription)))=[];
valdescription=['"' valdescription '"'];
valdec=num2str(hex2dec((valhex)));
fprintf(fid,'%s',(valdec));
fprintf(fid,' ');
fprintf(fid,'%s',(valdescription));
fprintf(fid,' ');
end
fprintf(fid,';')
end
fprintf(fid,'\n')
end
fclose(fid);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
excel2dbc.zip (3个子文件)
excel2dbc
can.xlsx 10KB
excel2dbc.m 5KB
excel2dbc.p 1KB
共 3 条
- 1
资源评论
- Suns3tz2020-04-07有用,自己根据实际Excel改改很实用
- dad1231232019-08-05收藏学习,谢谢
- x4291651292019-06-07效果不甚理想上传好多文件2019-06-12后来确实发现一些bug,正在修复
上传好多文件
- 粉丝: 5
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功