필터 지우기
필터 지우기

Real Time TCP Data Plotting

조회 수: 6 (최근 30일)
Matthew Mellor
Matthew Mellor 2016년 6월 14일
편집: Matthew Mellor 2016년 6월 14일
Hi I'm trying to plot TCP data in real time and I've modified example code I found here on mathworks. The problem is when I run the example code (and a version I modified) it doesn't update the graph although I can tell it is streaming data. Anyone have a idea why the graph isn't updating? Here is the code I'm working with (with my own minor changes):
%Orginal Code Author Ankit Desai
%Copyright 2010 - The MathWorks, Inc.
function real_time_data_stream_plotting
interfaceObject = tcpip('18.111.48.184',23);
figureHandle = figure('NumberTitle','off',...
'Name','Live Data Stream Plot',...
'Color',[0 0 0],...
'CloseRequestFcn',{@localCloseFigure,interfaceObject});
axesHandle = axes('Parent',figureHandle,...
'YGrid','on',...
'YColor',[0.9725 0.9725 0.9725],...
'XGrid','on',...
'XColor',[0.9725 0.9725 0.9725],...
'Color',[0 0 0]);
xlabel(axesHandle,'Number of Samples');
ylabel(axesHandle,'Value');
ylim([0 4096]);
%Initialize the plot and hold the settings on
hold on;
plotHandle = plot(axesHandle,0,'-y','LineWidth',1);
bytesToRead = 31;
%Define a callback function to be executed when desired number of
%bytes are availabe in the input buffer
interfaceObject.BytesAvailableFcn ={@localReadAndPlot,plotHandle,bytesToRead};
interfaceObject.BytesAvailableFcnMode = 'byte';
interfaceObject.BytesAvailableFcnCount = bytesToRead;
%Open the interface Object
fopen(interfaceObject);
pause(3);
snapnow;
%Implement the bytes available CallBack
function localReadAndPlot(interfaceObject,~,figureHandle,bytesToRead)
% Read the desired number of data bytes
data = fread(interfaceObject,bytesToRead);
dataStr = char(data(1:end-2)')
% remove the /n /r characters and convert it to a string
dataNum = sscanf(dataStr,'%d,%d,%d,%d,%d,%d');
% convert the string into numbers
% Update the plot
set(figureHandle,'Ydata',dataNum(1))
%Implement the close figure callback
function localCloseFigure(figureHandle,~interfaceObject)
fclose(interfaceObject);
delete(interfaceObject);
clear interfaceObject;
delete(figureHandle)
EDIT: I managed to get it to plot the data real time by changing the dataNum(1) to dataNum.

채택된 답변

Matthew Mellor
Matthew Mellor 2016년 6월 14일
Figured it out... Had to change dataNum(1) to dataNum in localReadAndPlot No helped needed :)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by