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.(adsbygoogle = window.adsbygoogle || []).push({});

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).Code (Text):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);

**Physics Forums - The Fusion of Science and Community**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# S-function simulink for serial communication

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

Loading...

Similar Threads - function simulink serial | Date |
---|---|

Mathematica Cannot do the integral of the Hyper-geometric function? | Mar 11, 2018 |

Mathematica Cannot Plot This Function in Mathematica | Feb 26, 2018 |

Matlab Dotting autonomous functions | Jan 24, 2018 |

Mathematica Piecewise Function | Jan 18, 2018 |

Errors with s-function level 1 in simulink | Sep 25, 2011 |

**Physics Forums - The Fusion of Science and Community**