2016-05-01 18 views
0

2つのプッシュボタン付きMATLAB GUIがあります。各プッシュボタンは、Com-Port(異なる)からシリアルデータを読み取る無限ループの実行を開始します。その後、ポート1停止し、ポート2開始読んで、私はポート2を停止したときに、ポート1が再開、私は、whileループはシリアルポートを読んでプッシュボタンを押すと、私は次のプッシュボタンを押すと、 。だからここに私の質問は、時間でつのタスクを実行することしかできないので、あなたは、MATLABでこれを行うことはできませんどのようにすべてのコールバック関数ループが独立して同時に作業することができますしながら、と..コールバック関数を独立して実行する方法matlab

function samplegui_OpeningFcn(hObject, ~, handles, varargin) 



handles.output = hObject; 
handles.vec_A=[]; 
handles.vec_B=[]; 
handles.vec_C=[]; 

handles.vec_A_1=[]; 
handles.vec_B_1=[]; 
handles.vec_C_1=[]; 
guidata(hObject, handles); 

function open_Callback(hObject, eventdata, handles) % push button1 to receive serial data. 

cnt=0; 

while 1 

     % Getting data from Serial Port 
     get_lines=fgets(handles.se) % getting data from serial port 
      if~isempty(get_lines) 
      cnt=cnt+1; 
     if strfind(get_lines,'T') %Parsing data 
     handles.vec_A=[handles.vec_A;[timet newword]]; 
     plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); % plotting 

     % Same follows for parsing and plot vec_B and Vec_C 
     drawnow(); % to update the Plots 
     end 
    end 
Pause(.05); 


end 
guidata(hObject, handles); 

function open2_Callback(hObject, eventdata, handles) % push button2 to receive serial data. 

cnt=0; 

while 1 

     % Getting data from Serial Port 
     get_lines=fgets(handles.se2) % getting data from serial port2 
      if~isempty(get_lines) 
      cnt=cnt+1; 
     if strfind(get_lines,'T') % Parsing data 
     handles.vec_A_1=[handles.vec_A;[timet newword]]; 
     plot(handles.vec_A_1(:,1),handles.vec_A_1(:,2:end),'r'); % plotting 

     % Same follows for parsing and plot vec_B and Vec_C 
     drawnow(); % to update the Plots 
     end 
    end 
Pause(.05); 


end 
guidata(hObject, handles) 

答えて

3

、です。これを回避するには、一定の間隔で各シリアルポートをリッスンし、プッシュボタンがこのタイマーを開始/停止するtimerを持つことです。この場合、whileループにpauseのループは必要ありません。シリアルポートからデータを一度取得し、タイマーが起動するたびにこの関数を呼び出すfuncitonが必要になります。

%// Function called each time a timer is fired, gets data from specified serial port 
function getData(hObject, handles, serialport) 
    get_lines = fgets(serialport); 

    if isempty(get_lines) 
     return 
    end 

    if strfind(get_lines,'T') 
     handles.vec_A = [handles.vec_A; [timet newword]]; 
     plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); 
     drawnow(); 
    end 

    guidata(hObject, handles); 
end 

%// And for your button callbacks that will toggle the timers on/off 
function open2_Callback(hObject, eventdata, handles) 

    if ~isfield(handles, 't2') || ~isvalid(handles.t2) || ~handles.t2.Running 
     %// Create a timer that checks the serial port twice a second 
     handles.t2 = timer('ExecutionMode', 'fixedRate', ... 
          'Period', 0.5, ... 
          'TimerFcn', @(s,e)getData(hObject, handles, handles.se2)); 

     %// Start the timer 
     start(handles.t2); 
    else 
     %// Stop and destroy the timer 
     stop(handles.t2); 
     delete(handles.t2); 
    end 

    guidata(hObject, handles); 
end 

function open_Callback(hObject, eventdata, handles) 
    if ~isfield(handles, 't1') || ~isvalid(handles.t1) || ~handles.t1.Running 
     handles.t1 = timer('ExecutionMode', 'fixedRate', ... 
          'Period', 0.5, ... 
          'TimerFcn', @(s,e)getData(hObject, handles, handles.se1)); 
     start(handles.t1); 
    else 
     stop(handles.t1); 
     delete(handles.t1); 
    end 

    guidata(hObject, handles); 
end 

更新

コメントで@Hokiで述べたように、新しいデータが到着したとき、あなたはまた、でしょう、自動的に火災(ややassynchronously)シリアル接続のbyteAvailableFcnプロパティを設定することができます。これにより、定期的にシリアルポートに新しいデータをポーリングする必要がなくなります。

function getData(serialport, hObject, handles) 
    get_lines = fgets(serialport); 

    if strfind(get_lines,'T') 
     handles.vec_A = [handles.vec_A; [timet newword]]; 
     plot(handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); 
     drawnow(); 
    end 

    guidata(hObject, handles); 
end 

set([handles.se2, handles.se1], 'BytesAvailableFcn', @(s,e)getData(s, hObject, handles); 
+0

予想されるデータ量が重要な場合はタイマーを使用する傾向がありますが、シリアルポート 'byteavailableFcn'のビルトインコールバックについても言及しています。また、シリアルポートにデータが到着したときに静かに鳴り、 。 – Hoki

+0

@Sueverポートからのデータをそれぞれ保存してプロットすることは可能ですか? – MaK

+0

@MaKシリアルポートごとに別々のコールバックを設定し、コールバック内でカスタムプロットを行うことができます。 – Suever

0
function samplegui_OpeningFcn(hObject, ~, handles, varargin) 

    handles.output = hObject; 

    handles.vec_A=[]; 
    handles.vec_B=[]; 
    handles.vec_C=[]; 

    handles.vec_A_1=[]; 
    handles.vec_B_1=[]; 
    handles.vec_C_1=[]; 

    guidata(hObject, handles); 
end 

function getData_1(hObject, handles, serialport) 
    get_lines = fgets(handles.se_1); 

    if isempty(get_lines) 
     return; 
    end 

    if strfind(get_lines,'T') 
     handles.vec_A = [handles.vec_A; [timet newword]]; 
     plot(handles.axes1,handles.vec_A(:,1),handles.vec_A(:,2:end),'r'); 
     drawnow(); 
    end 

    guidata(hObject, handles); 
end 

function getData_2(hObject, handles, serialport) 
    figure; 
    hold on; 
    ax(1)=subplot(3,1,1); 
    ax(2)=subplot(3,1,2); 
    ax(3)=subplot(3,1,3); 

    get_lines = fgets(serialport); 

    if isempty(get_lines) 
     return 
    end 

    if strfind(get_lines,'T') 
     handles.vec_A_1 = [handles.vec_A; [timet newword]]; 
     plot(handles.vec_A_1(:,1),handles.vec_A_1(:,2:end),'r'); 
     drawnow(); 
    end 

    guidata(hObject, handles); 
end 

function open2_Callback(hObject, eventdata, handles) 

    if ~isfield(handles, 't2') || ~isvalid(handles.t2) || ~handles.t2.Running 
     %// Create a timer that checks the serial port twice a second 
     handles.t2 = timer('ExecutionMode', 'fixedRate', ... 
          'Period', 0.5, ... 
          'TimerFcn', @(s,e)getData_2(hObject, handles, handles.se2)); 

     %// Start the timer 
     start(handles.t2); 
    else 
     %// Stop and destroy the timer 
     stop(handles.t2); 
     delete(handles.t2); 
    end 
end 

function open1_Callback(hObject, eventdata, handles) 
    if ~isfield(handles, 't1') || ~isvalid(handles.t1) || ~handles.t1.Running 

     %// Create a timer that checks the serial port twice a second 
     handles.t2 = timer('ExecutionMode', 'fixedRate', ... 
          'Period', 0.5, ... 
          'TimerFcn', @(s,e)getData_2(hObject, handles, handles.se2)); 

     %// Start the timer 
     start(handles.t2); 
    else 
     %// Stop and destroy the timer 
     stop(handles.t2); 
     delete(handles.t2); 
    end 
end 

あなたが与えられながら、同様のコード..ですそれはポートで継続的にデータを読み取ることができなかったため、whileループを追加。

+1

あなたの投稿を実際に読めるように更新しました。また、いくつかのエラーを修正しました。投稿時に問題はありません。存在する場合は、正確なエラーメッセージを投稿してください。 – Suever

関連する問題