下面根据之前讲的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模型如下:
然后,该模块可以正常用于simulink仿真,可以联合模型和底层代码一起进行仿真验证。
另外输入输出可以可以使用结构体的形式,但是需要事先在h头文件中定义好相应的结构体声明,大家可以自己试验一下,纸上得来终觉浅,绝知此事要躬行。
|