- #1
Henkjaap
- 4
- 0
I'm trying to read data from a serial port in simulink. The serial configuration blocks and query instrument are not working properly for the device I try to read over COM port.
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:
Giving me the error that the port needs to be opened at flag=3 (output). But I don't want to open the com2 every step as this will get really slow. How to solve this? I just want to open the connection once and constantly read from it (in simulink).
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);