Clear Filters
Clear Filters

Plotting real time data through serial port in MATLAB

4 views (last 30 days)
Hi There,
I have created a program in matlab that uses the bytesavailable function to read data through a serial port from arduino.
I now want to plot this data in real time while I could run other programs in MATLAB.
I think Im very close, however when I plot the data points they dont appear on the figure plot but the axis gets updated evertime the data comes through, however I dont actually see the data points on the plot. I have included my code here for your review, l would appreciate some help on this.
fclose(instrfind);
delete(instrfind);
Arduino_Blood_Pressure=serial('COM3','BaudRate', 9600);
Arduino_Blood_Pressure.BytesAvailableFcnMode = 'byte';
Arduino_Blood_Pressure.BytesAvailableFcnCount = 47;
Arduino_Blood_Pressure.InputBufferSize = 200000000 ;
Arduino_Blood_Pressure.BytesAvailableFcn = {@bytesAvailable_callback};
fopen(Arduino_Blood_Pressure);
function bytesAvailable_callback(obj,~)
DataFolder ='D:\';
Data= fscanf(obj,'%f')
Pressure_Data = Data(1);
% Trigger_Data = Data(2);
Sample_Index = Data(3);
plot(Sample_Index,Pressure_Data);
drawnow;
end

Answers (1)

Pavan Sahith
Pavan Sahith on 6 Oct 2023
Hi Nikan,
I understand that you are facing an issue with plotting data points while simultaneously running other programs in MATLAB.
It seems that you are not able to see the data points on the plot, and this might be due to your use of the "fscanf" function in your code for reading the data.
To address this issue, utilizing the "BytesAvailableFcn" property of the serial port object instead of "fscanf" might help.
This approach enables asynchronous data acquisition, meaning your code can continue running and performing other tasks while waiting for data to arrive.
Utilizing the "BytesAvailable" property of the serial port object to ensure data availability prior to reading it might also help.
% Check if there are enough bytes available to read by using
obj.BytesAvailable
Moreover, When working with real-time data visualization, using the "BytesAvailableFcn" callback allows you to update the plot immediately as new data arrives.
I recommend referring to the MathWorks documentation to learn more about the serial port object: https://in.mathworks.com/help/instrument/serial.html
Hope it would help!

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!