How can I use dsp.timescope in appdesigner to measure data from serial?

6 ビュー (過去 30 日間)
Donato stigliano
Donato stigliano 2020 年 12 月 3 日
回答済み: Khodour Al Kadry 2020 年 12 月 8 日
hi everyone, to simplify the problem I wrote a script of a few lines that makes the connection and reads data from a serial through a for loop and inside I inserted the plot through dsp.timescope.
My problem is why can't I see the waveform until the end of the for loop?
if I used a timer would I solve the problem?
The Matlab code I used is the following:
Fs=100;
scope = dsp.TimeScope(1,Fs,'TimeSpanSource','Auto', ...
"TimeSpanOverrunAction",'Scroll', ...
"ReduceUpdates",false, ...
"AxesScaling","Updates", ...
"ShowGrid",true);
arduinoObj = serialport("COM10",115200);
flush(arduinoObj);
arduinoObj.UserData = struct("Data",[],"Count",1)
show(scope)
release(scope);
for ll=1:20
data = readline(arduinoObj);
% Convert the string data to numeric type and save it in the UserData
% property of the serialport object.
arduinoObj.UserData.Data(end+1) = str2double(data);
% Update the Count value of the serialport object.
arduinoObj.UserData.Count = arduinoObj.UserData.Count + 1;
%Plot in timescope
signal = arduinoObj.UserData.Data';
scope(signal);
release(scope);
end
I used the following arduino code:
%%ARDUINO CODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Create a variable to send
float number=0;
// Define Analog input Pin
const int analogInPin = A1;
void setup() {
// initialize serial, use the same boudrate in Matlab command
Serial.begin(115200);
}
void loop(){
number = ((analogRead(analogInPin)*5)/1023)*sin(((float) millis()/1000)); // Give your float a value
// Print terminator
Serial.println(number);
delay(200);
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

採用された回答

Khodour Al Kadry
Khodour Al Kadry 2020 年 12 月 8 日
Hi Donato
By Looking at your code, I see that you are calling release in two unnessary spots. After show there is no need to call release. Inside the for loop as well I see that you are calling release which is not a good practice.
If the data coming from the arduino is not fixed size and for that reason you had to call release, I do recommned using dsp.AsyncBuffer ro buffer the data to the desired frame size, and then plot the data using the dsp.TimeScope object.
-Regards
Khodour

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScopes and Data Logging についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by