Clear Filters
Clear Filters

Problem with continuous timer object in Guide GUI

3 views (last 30 days)
I am having trouble making a timer that runs continuously to communicate with a relay controller to check the status of one of the analog outputs. When I run the GUI I get the error:
Error while evaluating TimerFcn for timer 'timer-1'
Object must be a figure or one of its child objects.
Here is my code for the opening of the GUI and the separate callback function for the timer:
% --- Executes just before pipeline_draft2 is made visible.
function pipeline_draft2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to pipeline_draft2 (see VARARGIN)
% Choose default command line output for pipeline_draft2
handles.output = hObject;
% Reading the gate valve to check status
echotcpip('on', 2101);
relay = tcpip('169.254.241.77', 2101);
relay.Timeout = 1;
fopen(relay);
handles.relay = relay;
% writing to relay 1
hex = ['0xAA'; '0x02'; '0xFE'; '0xA6'; '0x50'];
data = hex2dec(hex);
fwrite(relay, data)
response = fread(relay);
% Interpreting response from gate valve and setting relay buttons
if length(response) == 11
if response(3) == 0
set(handles.gate_valve_off,'enable','off')
else
set(handles.gate_valve_on,'enable','off')
end
else
if response(7) == 0
set(handles.gate_valve_off,'enable','off')
else
set(handles.gate_valve_on,'enable','off')
end
end
% Setting status of gate valve
if length(response) == 11
if response(3) == 0
set(handles.gate_valve_status, 'BackgroundColor', 'red')
else
set(handles.gate_valve_status, 'BackgroundColor', 'green')
end
else
if response(7) == 0
set(handles.gate_valve_status, 'BackgroundColor', 'red')
else
set(handles.gate_valve_status, 'BackgroundColor', 'green')
end
end
% writing to relay 1
hex = ['0xAA'; '0x03'; '0xFE'; '0x74'; '0x01'; '0x20'];
data = hex2dec(hex);
fwrite(relay, data)
solenoid_status = fread(relay);
% Setting status of solenoid
if length(solenoid_status) == 8
if solenoid_status(7) == 0
set(handles.solenoid_status, 'BackgroundColor', 'red')
else
set(handles.solenoid_status, 'BackgroundColor', 'green')
end
else
if solenoid_status(3) == 0
set(handles.solenoid_status, 'BackgroundColor', 'red')
else
set(handles.solenoid_status, 'BackgroundColor', 'green')
end
end
% Creating timer to update gate valve status at a rate of 20 Hz
t = timer();
t.Period = 2;
t.ExecutionMode = 'fixedRate';
t.TimerFcn = {@gate_valve_status, handles};
handles.timer = t;
start(t)
% Update handles structure
guidata(hObject, handles);
% Separate callback function for the timer object
function gate_valve_status(~, ~, handles)
% importing info from gui
guidata(handles.gate_valve_status, handles)
guidata(handles.relay, handles)
guidata(hObject, handles)
% Reading the gate valve to check status
% writing to relay 1
hex = ['0xAA'; '0x02'; '0xFE'; '0xA6'; '0x50'];
data = hex2dec(hex);
fwrite(handles.relay, data)
response = fread(handles.relay);
% Setting status of gate valve
if length(response) == 11
if response(3) == 0
set(handles.gate_valve_status, 'BackgroundColor', 'red')
else
set(handles.gate_valve_status, 'BackgroundColor', 'green')
end
else
if response(7) == 0
set(handles.gate_valve_status, 'BackgroundColor', 'red')
else
set(handles.gate_valve_status, 'BackgroundColor', 'green')
end
end
end
Any help would be appreciated.
Thanks!

Answers (1)

Image Analyst
Image Analyst on 9 Oct 2020
Look at this code:
% Separate callback function for the timer object
function gate_valve_status(~, ~, handles)
% importing info from gui
guidata(handles.gate_valve_status, handles)
guidata(handles.relay, handles)
guidata(hObject, handles)
You replaced hObject by ~ so it does not see it. But you don't even need to call guidata() at all. Just get the info from the handles structure if you need to. But I think you can probably just delete those 3 lines.
  3 Comments
Image Analyst
Image Analyst on 14 Oct 2020
No, that's normal, at least for GUIDE. Attach both your .m and .fig files and tell me what you did.
Keir Hunter
Keir Hunter on 15 Oct 2020
Here is my .m and .fig files. I am trying to update the gate_valve_status color from red to green based on the status of a relay controller. When I open the code it works but I believe this is because I have a separate check before the timer object starts. When the timer object starts I have it call a separate function, that I have also attached, to check the relay controller. When the timer object is called the gate valve status does not updated when changed. Let me know if this helps thanks!

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!