Problem with continuous timer object in Guide GUI
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Image Analyst
2020-10-9
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 个评论
Image Analyst
2020-10-14
No, that's normal, at least for GUIDE. Attach both your .m and .fig files and tell me what you did.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!