Reputation: 65
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.
Upvotes: 0
Views: 715
Reputation: 10782
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.
Upvotes: 1