分享

[转载]基于模型开发之代码集成(五)

 stevin_lib 2018-07-19

下面根据之前讲的LegacyCodeTool再举一个例子:

底层在进行通信时常会遇到一些数据解析或分解情况,可能的函数如下:

test.c

#include "test.h"

void testcan(unsigned char * phval, unsigned char *sg1,unsigned int *sg2,unsigned int *sg3,unsigned int *sg4,unsigned char *sg5)
{
   *sg1=phval[0];
   *sg2=(((unsigned short int)phval[2])<<8)+(unsigned short int)phval[1];
   *sg3=(unsigned short int)phval[3]+(unsigned short int)phval[4];
   *sg4=(unsigned short int)phval[5]*2;
   *sg5=phval[6]|phval[7];
};

 

test.h

 #ifndef ____testcan_H
#define ____testcan_H

extern void testcan(unsigned char *phval, unsigned char *sg1,unsigned int *sg2,unsigned int *sg3,unsigned int *sg4,unsigned char *sg5);

#endif

 

可以编辑如下m脚本或依次在Matlab中输入下列命令:

 %% Define Legacy Code Tool options
testdecode = legacy_code('initialize');                          %Create specifications structure
testdecode.OutputFcnSpec = 'void testcan(uint8 u1[8], uint8 y1[1],uint16 y2[1],...

           uint16 y3[1],uint16 y4[1],uint8 y5[1])';     %Function to be called during simulation
testdecode.HeaderFiles = {'test.h'};                       %Necessary external header files
testdecode.SourceFiles = {'test.c'};                       %Necessary external source files
testdecode.SFunctionName = 'testmycode';                 %S-function name

%% Create S-function, TLC file, etc.
legacy_code('sfcn_cmex_generate', testdecode);     %Generate S-function code
legacy_code('compile', testdecode)                            %Compile S-function
legacy_code('slblock_generate',testdecode)              %Create block for S-function
%legacy_code('sfcn_tlc_generate', testdecode)           %Inline S-function by creating TLC file
%legacy_code('rtwmakecfg_generate', testdecode)     �d path info to RTW make file

 

然后会自动生成相应的slx模型如下:

基于模型开发之代码集成(五)——LegacyCodeTool之例2

然后,该模块可以正常用于simulink仿真,可以联合模型和底层代码一起进行仿真验证。

 

另外输入输出可以可以使用结构体的形式,但是需要事先在h头文件中定义好相应的结构体声明,大家可以自己试验一下,纸上得来终觉浅,绝知此事要躬行。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多