S-function for reading com-port

21 ビュー (過去 30 日間)
Jan De Vries
Jan De Vries 2012 年 4 月 12 日
コメント済み: Koenraad de Haas 2018 年 3 月 5 日
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).

回答 (4 件)

Kaustubha Govind
Kaustubha Govind 2012 年 4 月 12 日
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).
  5 件のコメント
Jan De Vries
Jan De Vries 2012 年 4 月 13 日
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!


Shankar Subramanian
Shankar Subramanian 2012 年 5 月 15 日
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.
  2 件のコメント
Jan De Vries
Jan De Vries 2012 年 5 月 17 日
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


Walter Roberson
Walter Roberson 2012 年 4 月 12 日
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.
  2 件のコメント
Koenraad de Haas
Koenraad de Haas 2018 年 3 月 5 日
Hi Jan, were you able to solve this issue, I would be interested, thanks


Zerno 2012 年 5 月 17 日
Please, can somebody help with taking data from comport to simulink model??
  1 件のコメント
Zerno 2012 年 5 月 17 日
I need to write s-function in C for this//


Community Treasure Hunt

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

Start Hunting!

Translated by