S-function for reading com-port

21 views (last 30 days)
Jan De Vries
Jan De Vries on 12 Apr 2012
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:
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 = [];
error(['Unhandled flag = ',num2str(flag)]);
function [sys,x0,str,ts] = mdlInitializeSizes(NMEA)
sizes = simsizes;
sizes.NumContStates= 0;
sizes.NumDiscStates= 0;
sizes.NumOutputs= 1;
sizes.NumInputs= 1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [-1 0];
function sys = mdlOutputs(t,x,u,NMEA)
sys = fscanf(NMEA);
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).

Answers (4)

Kaustubha Govind
Kaustubha Govind on 12 Apr 2012
If following Walter's suggestion, you will need:
persistent NMEA;
if isempty(NMEA) %true only the first time
NMEA = serial('COM2', 'BaudRate', 4800);
However, persistent variables don't work well if multiple instances of your S-function exist in a model. So we recommend using DWork vectors to store such instance-specific persistent data in S-functions.
Also, remember to use fclose in the mdlTerminate method (flag=9).
Jan De Vries
Jan De Vries on 13 Apr 2012
Ok thanks again... Unfortunately this is getting too complicated for me... So frustrating I can't get something that works so easily in Matlab (using fscanf) into simulink!

Sign in to comment.

Shankar Subramanian
Shankar Subramanian on 15 May 2012
Hi Jan,
The Serial Configuration, Serial Receive and Serial Transmit blocks support reading and writing binary data (equivalent of fwrite and fread within MATLAB).
The Query Instrument and To Instrument blocks allow reading and writing ASCII (using fscanf and fprintf) as well as binary data. You need not write an S-Function to accomplish this task. You can import an object created in MATLAB command line into the Simulink model as well using these blocks.
Two things:
1. Can you specify the format of your data and also the settings that you had to perform an ASCII read and I will be able to help you.
2. When you mentioned, Query Instrument block is not working properly, what are you seeing? Are you seeing, no data or dropped data? Please give more info.
Jan De Vries
Jan De Vries on 17 May 2012
I put a "to workspace" block after the query instrument to check the output; this generated a variable with every new character of the ascii sentence in a new row. So the output looked like
representing the $HCH etc. sentence
With the serial config/serial input blocks I could not get any readings; the output constantly was 00

Sign in to comment.

Walter Roberson
Walter Roberson on 12 Apr 2012
Use a persistent variable to hold NEMA . Otherwise it goes out of scope when the function returns and that causes the serial port to be closed.
Koenraad de Haas
Koenraad de Haas on 5 Mar 2018
Hi Jan, were you able to solve this issue, I would be interested, thanks

Sign in to comment.

Zerno on 17 May 2012
Please, can somebody help with taking data from comport to simulink model??
  1 Comment
Zerno on 17 May 2012
I need to write s-function in C for this//

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!