- #1

Henkjaap

- 4

- 0

In matlab, I can easily read out data from the device using fopen() and fscanf().

Using the MATLAB function block in simulink, I get the same result in simulink. Problem is: it needs to open and close the serial connection every step, slowing down my system.

I'm trying to write level1 s-function to overcome this problem. Right now I have:

Code:

```
function [sys,x0,str,ts] = test1(t,x,u,flag)
NMEA = serial('COM2', 'BaudRate', 4800);
switch flag
case 0
[sys,x0,str,ts] = mdlInitializeSizes(NMEA);
case 3
sys = mdlOutputs(t,x,u, NMEA);
case { 1, 2, 4, 9 }
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end;
function [sys,x0,str,ts] = mdlInitializeSizes(NMEA)
sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs= 1;
sizes.NumInputs= 1;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [-1 0];
fopen(NMEA);
function sys = mdlOutputs(t,x,u,NMEA)
sys = fscanf(NMEA);
```