S-function simulink for serial communication

In summary, to overcome the problem of having to constantly open and close the serial connection in Simulink, you can use a MATLAB script or a Simulink Stateflow chart to control the opening and closing of the connection, allowing you to continuously read data from the device.
  • #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:

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);
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).
 
Physics news on Phys.org
  • #2
</code>The best solution is probably to use the MATLAB Function block in Simulink and open the serial connection outside of the Simulink model. This allows you to open the connection and keep reading from it without having to open and close it every time step. For example, you could have a MATLAB script that opens the connection and then runs the Simulink model. The Simulink model would then read the data from the serial connection using the MATLAB Function block. When the Simulink model is done running, the MATLAB script would close the connection. Alternatively, you could also use a Simulink Stateflow chart. A Stateflow chart can contain MATLAB code and can be used to control the opening and closing of the serial connection.
 

FAQ: S-function simulink for serial communication

1. What is an S-function in Simulink?

An S-function in Simulink is a user-defined block that allows you to incorporate custom MATLAB code into your Simulink model. It can be used to perform specific tasks or calculations that are not available in the standard Simulink blocks.

2. How does S-function simulink work for serial communication?

S-function simulink for serial communication works by using the S-function block to create a custom block that can send and receive data over a serial port. The S-function block allows you to write custom MATLAB code to perform the necessary serial communication tasks.

3. What are the advantages of using S-function simulink for serial communication?

There are several advantages to using S-function simulink for serial communication. It allows you to have more control over the serial communication process, as you can write custom code to handle specific tasks. It also allows you to integrate serial communication into your Simulink model, making it easier to manage and troubleshoot.

4. Can I use S-function simulink for serial communication with different types of serial ports?

Yes, you can use S-function simulink for serial communication with different types of serial ports. The S-function block allows you to specify the type of serial port and configure the settings accordingly, making it compatible with various types of serial ports.

5. Are there any limitations to using S-function simulink for serial communication?

While S-function simulink for serial communication offers many advantages, there are some limitations to keep in mind. It requires knowledge of MATLAB programming, so it may not be suitable for those who are not familiar with the language. Additionally, it may require more time and effort to implement compared to using standard Simulink blocks for serial communication.

Similar threads

Back
Top