0

I am new to matlab. With the help of online search i have written MODBUS RTU code to fetch data from my device. I want to import this data to simulink for further DSP analysis. Below is my matlab code for fetching MODBUS RTU data from serial port.

instrreset;
clear all; 
close all; 
clc; 

s = serial('COM4');    
set(s,'BaudRate',115200,
      'DataBits',8,
      'StopBits',1,
      'Parity','None','Timeout',1);
fopen(s);

request = uint8(hex2dec(['01'; '03'; '00'; '00'; '00'; '02'; 'C4'; '0B']));
ts = timeseries('mySeries'); % Updated
while(1)
    fwrite(s, request);    
    outdec = fread(s,9);
    y = typecast(uint8([outdec(7) outdec(6) outdec(5) outdec(4)]),'int32');
    z = datevec(datetime('now')); % Updated
    ts = timeseries(y, z); % Updated
    disp(y);
end

fclose(s);
delete(s);
clear s
disp('STOP')

in while loop i am continuously fetching the modbus value in variable y. Now i want this value in simulink. My data fetching will be at every 100ms or you can say my sampling frequency will be 10hz.. Any help will be appreciated.

My main aim is to design the digital filter that is best suited for my application.

Thanks in advance.

  • http://stackoverflow.com/questions/8329944/input-data-to-simulink-from-workspace?rq=1 – Hadi Mar 25 '17 at 10:32
  • http://stackoverflow.com/questions/26742660/how-to-input-double-values-in-simulink-from-workspace?rq=1 – Hadi Mar 25 '17 at 10:32
  • read these links and if its not working in your case, let us know :) – Hadi Mar 25 '17 at 10:35
  • Okies as per your links i have made one ts object and i am filling it with my value and date vector. Then i have imported it in simulink. so for so good. Now how to continuously send that data to simulink ? In matlab my data is getting filled in ts object which is timeseries. Any idea on looping the simulation so that the data change is realtime !!! – Nimit Vachhani Mar 25 '17 at 11:21
  • My modified code s = serial('COM4'); set(s,'BaudRate',115200,'DataBits',8,'StopBits',1,'Parity','None','Timeout',1); fopen(s); request = uint8(hex2dec(['01'; '03'; '00'; '00'; '00'; '02'; 'C4'; '0B'])); ts = timeseries('mySeries'); datetime.setDefaultFormats('default','dd-MM-yyyy hh:mm:ss.SSS') while(1) fwrite(s, request); outdec = fread(s,9); y = typecast(uint8([outdec(7) outdec(6) outdec(5) outdec(4)]),'int32'); x = datetime; z = datevec(datetime('now')); ts = timeseries(y, z); disp([datestr(x) [' ',num2str(y)]]); end – Nimit Vachhani Mar 25 '17 at 11:22
  • edit and update your question with new code – Hadi Mar 25 '17 at 11:24
  • I have updated the code with 3 new lines of code. I not able to post the screen grab of my simulink... Any ideas on how to do it ? – Nimit Vachhani Mar 25 '17 at 11:51

1 Answers1

1

You really should write this as an m-code S-Function, with your (one-off) set up code being performed during block mdlInitialize function; your (one-off) termination code in the block mdlTerminate function; and the contents of your loop in the mdlUpdate function. Simulink will then query/read your COM port at every time step of the model.

However, should you wish to have this driven by MATLAB code, then you need to force your Simulink model to update the From Workspace block inside your while loop. Assuming the name of the variable in the block is ts, then doing the following should suffice:

set_param(NameOfFromWorkspaceBlockAsString,'VariableName','ts');

That will force the model to look for the variable ts each time, and get the value from that variable (which you have just updated).

However, I'm skeptical that using the From Workspace block is the right way to do this. That block uses the time in the variable to determine when the value should be used in the model. Since now is about 736779.5, your model would have to run for that period of time before the data would be used.

I suspect you really just want to use a Constant block, and change the value of the constant to the new value for y (without including the time stamp). Although as per my first comment, the right way to do this is to use an S-Function.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • There are plenty of demos that ship with Simulink. Type `sfundemos` at the command line; open the m-code S-Function demos; look at the template and the various examples. – Phil Goddard Mar 26 '17 at 03:25