Realtime Plotting of RS232 Data Help
4 ビュー (過去 30 日間)
古いコメントを表示
Hi there all,
I'm trying to plot ultrasound data in realtime in Matlab which is being sent to the computer via RS232. The below code works in batches - i.e. I can click run and Matlab will plot the 6 data points correctly...
but... I can not get it work in real time. The graph needs to update automatically, can anyone please help?
function [back,bottom,front,left,right,top]= ReadUltrasoundOld
%-------------------------------------------------------------------------------
% This module strips the US data from the RS232 link of it's commas
% and puts it in a usable format for real time graphing.
%-------------------------------------------------------------------------------
back = zeros(0,1); %For US data on the X Axis
bottom = zeros(0,1); %For US data on the Y Axis
front = zeros(0,1); %For US data on the Z Axis
left = zeros(0,1); %For US data on the X Axis
right = zeros(0,1); %For US data on the Y Axis
top = zeros(0,1); %For US data on the Z Axis
%--------------------------------------------------------------------------
%Set up the serial port with the settings used by the NIOS and wireless link.
s=serial('COM1');
set(s,'BaudRate',19200);
set(s,'DataBits',8);
set(s,'Parity','none');
set(s,'StopBits',1);
set(s,'flowcontrol','hardware');
set(s,'Timeout',1000);
%Async means that re read the port continusaly.
s.ReadAsyncMode = 'continuous';
%Set the Terminator to be a Carriage Return this can be changed as requred
%but CR is a simple end char.
set(s,'Terminator','CR');
s.BytesAvailableFcnMode = 'terminator';
%On receiving data on the port send the code to the function.
s.BytesAvailableFcn = @IncomingData;
%Open the Serial Port
fopen(s);
%--------------------------------------------------------------------------
%Send Request Data
while isempty(top)
fprintf(s,'u');
pause(0.5);
end
%--------------------------------------------------------------------------
%Strip recieved data into usable format
function IncomingData(obj,~)
%Read in the line
data = fgetl(obj);
%We get the data as one long line of data in the form
%back,bottom,front,left,right,top
%Split it using strtok about a comma ','
%Deal with the back data
[back,data] = strtok(data,',');
back = str2double(back);
back = [back];
back = 0-back;
%Deal with the bottom data
[bottom,data] = strtok(data,',');
bottom = str2double(bottom);
bottom = [bottom];
bottom = 0-bottom;
%Deal with the front data
[front,data] = strtok(data,',');
front = str2double(front);
front = [front];
%Deal with the left data
[left,data] = strtok(data,',');
left = str2double(left);
left = [left];
left = 0-left;
%Deal with the right data
[right,data] = strtok(data,',');
right = str2double(right);
right = [right];
%Deal with the top data
[top,data] = strtok(data,',');
top = str2double(top);
top = [top];
%Send a blank to stop the transmission
fprintf(s,'0');
%flush the input buffer so we do not over load it
flushinput(obj);
end
%--------------------------------------------------------------------------
%Now close the serial port.
fclose(s);
delete(s);
%--------------------------------------------------------------------------
%Stripped data assigned to x,y,z coordinates
back
bottom
front
left
right
top
front = [ 0 front 0 ];
back = [ 0 back 0 ];
top = [ 0 0 top ];
bottom = [ 0 0 bottom ];
right = [ right 0 0 ];
left = [ left 0 0 ];
origin = [ 0 0 0 ];
%--------------------------------------------------------------------------
%Figure Handle
figureHandle = figure('NumberTitle', 'off',... %removes figurenumbers
'Name','Big Bertha Realtime Force Field Display',... %adds a title
'Color',[0 0 0]);
%Axis Handle
axesHandle = axes('Parent',figureHandle,...
'YGrid','on',...
'YColor',[0.9725 0.9725 0.9725],...
'XGrid','on',...
'XColor',[0.9725 0.9725 0.9725],...
'ZGrid','on',...
'ZColor',[0.9725 0.9725 0.9725],...
'Color',[0.6 0.6 0.6]);
hold on;
%x,y,z,title Labels
xlabel({'X axis';'left right'},'Color',[1 1 0]);
ylabel({'Y axis';'top bottom'},'Color',[1 1 0]);
zlabel({'Z axis';'front back'},'Color',[1 1 0]);
title('Ultrasound Data','Color',[1 1 0]);
%x,y,z limits
xlim(axesHandle,[min(-255) max(255)]);
ylim(axesHandle,[min(-255) max(255)]);
zlim(axesHandle,[min(-255) max(255)]);
%Graph Plotted
plotHandle = plot3(right,top,front,'o','MarkerSize',10);
hold on;
plotHandle = plot3(back,bottom,left,'o','MarkerSize',10);
hold on;
plotHandle = plot3(origin,'X','MarkerSize',5);
hold on;
set(plotHandle,'back',back,'bottom',bottom,'front',front,'left',left,'right',right,'top',top);
%--------------------------------------------------------------------------
%Serial Port closed
fclose(serialObject);
delete(serialObject);
clear serialObject;
end
0 件のコメント
回答 (4 件)
Ankit Desai
2011 年 3 月 31 日
Hi,
You might want to look at the examples available on MATLAB Central File Exchange:
You might have to combine these two scripts and modify those to fit your needs.
Hope this helps.
-Ankit
Walter Roberson
2011 年 4 月 1 日
set() is not using function names: set() is using property names, with the valid properties determined by the class of the handle or object being work on.
It appears that you might perhaps wish to set up XDataSource and so on, and use refreshdata().
Be aware that each plot3() call may return a vector of plot handles that reflect only the lines that that particular call added. When you use "hold on", the next plot3() call does not return the vector of all existing lines, just the ones added in that next call. Your code overwrites plothandle twice, so you are losing track of the earlier handles.
If plot3 does return a vector of handles, then XDataSource and so on will not work for that situation. It appears to me, though, that your code probably would not trigger returning multiple handles per call.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!