2016-09-19 2 views
0

마이크로 컨트롤러에서 전송되는 직렬 포트에서 데이터를 플롯하려고 할 때이 데이터를 해석하고 그래프로 나타냅니다. 데이터는 너무 빨리 (마이크로 초의 50 분마다) 오게 될 것이므로 특정 수의 데이터 포인트를 읽으면 그래프를 따라 스크롤하고 싶습니다. 스크롤없이 단일 데이터 값과 여러 데이터 값을 성공적으로 그래프화할 수 있었지만 스크롤을 구현하려고하면 값이 왜곡되고 보통 스크롤 할 값에 도달하면 코드가 깨집니다.스크롤 할 Matlab 플로트 가져 오기

delete(instrfind); 
clear; 
close all; 

s = serial('COM3'); %assigns the object s to serial port 

set(s, 'InputBufferSize', 1); %number of bytes in inout buffer 
set(s, 'FlowControl', 'hardware'); 
set(s, 'BaudRate', 9600); 
set(s, 'Parity', 'none'); 
set(s, 'DataBits', 8); 
set(s, 'StopBit', 1); 
set(s, 'Timeout',10); 
%s.Terminator = '"'; 
clc; 

disp(get(s,'Name')); 
prop(1)=(get(s,'BaudRate')); 
prop(2)=(get(s,'DataBits')); 
prop(3)=(get(s, 'StopBit')); 
prop(4)=(get(s, 'InputBufferSize')); 

disp(['Port Setup Done!!',num2str(prop)]); 

fopen(s);   %opens the serial port 
t=1; 
a = zeros(100,'int8'); 
dataToDisplay = zeros(100,'int8'); 
disp('Running'); 


dataToDisplay = []; 
while(t < 501) %Runs for 500 cycles 

     for x = 1:4 
     a(x) = fread(s); %reads 3 values of the data from the serial port and stores it to the matrix a 
     end 

     if (t>101) 
      for i = 1:98 
      dataToDisplay(100) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); % combines the values in a and changes them into the value to display 
      dataToDisplay(i) = dataToDisplay(i+1); 
      end 
     else 
      dataToDisplay(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); 
    end 




    %if(data(t) == 10) 
    %dataToDisplay(t) = a; 

    plot(dataToDisplay,'-*r'); 
    axis auto; 
    grid on; 
    hold on; 

    t=t+1; 
    x = 0; 
    a=0; %Clear the buffer 
    drawnow; 
end 

fclose(s); %close the serial port 

나는 또한 내가 읽고있다 값이 4 7 세그먼트 디스플레이에 동시에 표시하므로 내가 표시 할 양식 번호를 얻기 위해 필요한 디코딩 할 것을 추가해야합니다. 처음 3 개의 디스플레이는 숫자를 유지하는 반면, 4 번째 유닛은 현재 MATLAB 코드에서 필요하지 않은 유닛을 가지고 있습니다. 때문에 assumedly 오타 무엇

+0

당신은 확실 이 '보류'가 필요한가요? 또한 아마도 수동으로 축을 정의 할 수 있습니다. 아마 훨씬 쉽게 읽을 수 있기 때문입니다. – MayeulC

+0

지금 당장 스크롤링 작업을 시작하려고합니다. 결과적으로 결국 다른 축을 수동으로 설정 한 다른 서브 플로트를 갖게 될 것입니다. – AmatuerCoder101

답변

0

에 코드를 나누기 : t (102) =되면,이 dataToDisplay의 값 (103)을 요청

 dataToDisplay(i) = dataToDisplay(i+1); 

, 아직 존재하지 않는다.

다음 줄로 무엇을하려고합니까? 아래의 코멘트 당신이 뭘 하려는지에서 올바른 추측이라고 가정하면, 내가 (다음에 코드를 변경 내 노트를 참조 것 : 당신의 논리와 구현 사이의 불일치가

if (t>101) 
     for i = 1:98 
     dataToDisplay(100) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); % combines the values in a and changes them into the value to display 
     dataToDisplay(i) = dataToDisplay(i+1); 
     end 
    else 
     dataToDisplay(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); 
end 

편집 있다고 생각) %%%로 :

data = []; %%%Renamed variable 
numSamples = 500; %%%Set numSamples as a variable, so that if you need this number later you can just change it once 
figure; %%%Start figure before loop 
axis auto; 
grid on; 

for t=1:numSamples %Runs for 500 cycles %%% More standard to use for loops than rolling your own with while and incrementing 

    for x = 1:3 %%% Unsure why you had this set as 1:4, since you didn't read the fourth value 
     a(x) = fread(s); %reads 3 values of the data from the serial port and stores it to the matrix a 
    end 

    data(t) = ((a(1)-96)*10)+(a(2)-80)+((a(3)-32)/10); %Update current value of data 

    plot(data(max(1,t-100):t),'-*r'); %%%Plot the most recent 100 values 

    %%% Unnecessary, variable clears when for x=1:3 loop ends x = 0; 
    %%% Unnecessary, a is reassigned when assigned to fre a=0; %Clear the buffer 
    drawnow; 
end 

이 방법, 당신은 루프보다는 자신의 만들기위한 정상적인 사용, 코드가 덜 읽기 악화-수행하게하는 불필요한 명령을 줄이려고하고, "데이터의 전체를 유지 "당신이 그것을 다시 참조해야 할 경우를 대비하여.

+1

그는 다음과 같이하려고합니다 : "첫 번째 100 개의 값을'dataToDisplay'에 추가 한 다음 추가합니다. 마지막에 새로운 것들이 하나씩 (fifo) 이동합니다. 당신이 맞을 수도, 나는 구현을 확인하지 않았고, 이것을 달성하기위한 확실한 방법이있다. dataToDisplay (:) = [dataToDisplay (2 : end), newdata] – MayeulC

+0

그것이 맞다면 위의 EDIT1로이 코드를 정리하고 실제로 목표를 달성하는 방법을 제안했습니다. –

+0

죄송합니다. 너무 많은 코드가 포함되어 있다고 생각하지만 답변은 정확히 무엇입니까? (데이터 (max (1, t-100) : t), '- * r'); %%% 가장 최근의 100 개의 값을 표시합니다. – AmatuerCoder101

0

plot (data (max (1, t-100) : t), '- * r'); %%% 플롯이 100 개 값을 플롯합니다

가장 최근 (100 개) 값은 다음 환호를 원하는대로 정확하게 그래프에 추가 된 새로운 데이터에 따라 왼쪽으로 한 자리에 너무 데이터 이동을 통해 이동

관련 문제