jeudi 28 novembre 2019

Handles in programmatically GUI and flags for buttons MATLAB

Hey i've got really annoying problem.

I want to operate some buttons in my programmatically GUI. I want to open port, close port and exit the while loop. I really want to do it outside the buttons callbacks. I mean, that script is in infinite while loop and when i press(in this case) Exit button it changes it own value(flag) for true and this will proceed to the while loop to break it. I can't get how to parse this data with handles or any other way. I want to avoid initializing so much global variables. Is there any way to do it as I said?

Here is the code:

clc;
close all;
clear all;
global s;
s = serial("COM3",'BaudRate',1000000,'Terminator','CR/LF'); %Zmienna dotycząca portu 
fclose(s);
figureHandle = figure('Position',[160,500,260,200]);
h.Open  = uicontrol('Style','pushbutton','String','Open port','Position',[75,120,90,25],...
             'Callback',@Openbutton_Callback);
h.Close = uicontrol('Style','pushbutton','String','Close port','Position',[75,80,90,25],...
             'Callback',@Closebutton_Callback);
h.Exit = uicontrol('Style','pushbutton','String','Exit','Position',[75,40,90,25],...
             'Callback',@Exitbutton_Callback);


while(1)
    if strcmp(s.Status, 'closed')
        disp('port zamkniety');
        pause(0.5)
    else
        disp('port otwartty');
        pause(0.5)
    end
    if handles.exitbtnValue
        handles.exitbtnValue = false;
        exit
    end
end
function Openbutton_Callback(hObject,event) %Funkcja dotycząca otwierania portu
        global s;
    fopen(s);
        s
    end
    function Closebutton_Callback(hObject,event)%Funkcja dotycząca zamykania portu
    global s;
        fclose(s);

    end
    function Exitbutton_Callback(hObject,event)
    handles.exitbtnValue=true;
    guidata(hObject, handles);

    end

Aucun commentaire:

Enregistrer un commentaire