MATLAB Answers

0

"Not enough input arguments " error when using timer

Asked by Abdulbaki Gunes on 24 May 2019
Latest activity Answered by Geoff Hayes
on 27 May 2019
Hi, i'm new to matlab and i'm trying to make that when click the button in GUI it will open the serial port then run my DATA function by using timer. Here is my code
function timer_deneme_OpeningFcn(hObject, eventdata, handles)
% 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 timer_deneme (see VARARGIN)
clc
delete(instrfind);
delete(timerfindall);
[~,serialPorts] = dos(['REG QUERY ' 'HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM']);
ports=regexp(serialPorts,'COM[0-9]+','match');
port=ports;
handles.portlist.UserData = 0;
setappdata(handles.portlist.Parent,'ports',port);
while(get(handles.Connect,'UserData'))
pause(0.5);
end
menuitem=get(handles.portlist,'String');
menuitem=getappdata(handles.portlist.Parent,'ports');
set(handles.portlist,'String',menuitem);
handles.t = timer;
handles.t.TimerFcn = @DATA;
handles.t.Period = 1;
handles.t.BusyMode = 'drop';
handles.t.ExecutionMode = 'fixedRate';
% Choose default command line output for timer_deneme
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes timer_deneme wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = timer_deneme_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in portlist.
function portlist_Callback(hObject, eventdata, handles)
% hObject handle to portlist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns portlist contents as cell array
% contents{get(hObject,'Value')} returns selected item from portlist
% --- Executes during object creation, after setting all properties.
function portlist_CreateFcn(hObject, eventdata, handles)
% hObject handle to portlist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Connect.
function Connect_Callback(hObject, eventdata, handles)
% hObject handle to Connect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
serPortn = get(handles.portlist, 'Value');
serList = get(handles.portlist,'String');
serPort = serList{serPortn};
handles.serConn = serial(serPort,'BaudRate',57600,'DataBits',8);
start(handles.t)
fopen(handles.serConn);
guidata(hObject, handles);
function DATA(hObject, eventdata, handles)
disp('HERE I AM')
while (1)
Header = dec2hex(fread(handles.serConn, [1 5]));
Header=[Header(1,:) Header(2,:) Header(3,:) Header(4,:) Header(5,:)];
a =size(Header);
if a(1,2) == 10
if Header == '6642AA4456'
break
end
end
end
Temperature = dec2hex(fread(handles.serPort,[1 2]));
Temperature = [Temperature(1,:) Temperature(2,:)];
aa = size(Temperature);
if aa(1,2) == 2
Temperature = 256*hex2dec(Temperature(1)) + 16*hex2dec(Temperature(2));
Temperatureacisi = Temperature*360/2^16;
else
Temperature = hex2dec(Temperature);
Temperatureacisi = Temperature*360/2^16;
end
if Temperatureacisi > 180 && Temperatureacisi < 360
Temperatureacisi = Temperatureacisi-360;
end
function DisConnect_Callback(hObject, eventdata, handles)
% hObject handle to DisConnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
stop(handles.t)
fclose(handles.serConn);
delete(handles.serConn);
set(handles.lamb, 'BackgroundColor','red');
CommanWindow.png
And I got the error. In my DATA function if I comment all line except disp('HERE I AM') , the everything works fine. Unless if uncomment all line in DATA function , then I getting the same errror.
Error while evaluating TimerFcn for timer 'timer-1'
Not enough input arguments.
I have no idea how to solve this problem.
I would really appreciate any help
Thank you in advance.

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 27 May 2019

Abdulbaki - the function signature for your timer callback is
function DATA(hObject, eventdata, handles)
The timer function callback generally only has two input paramters - the handle to the timer object (hObject), and some event data (eventdata). You can add one or more additional parameter but you need to define those when you assign the callback to your timer. You are doing this at the line
handles.t.TimerFcn = @DATA;
and so the error message makes sense: there are not enough input parameters to your function since your signature expects three but only the default two are provided. What you need to do is something like
handles.t.TimerFcn = {@DATA, hObject};
where hObject is the handle to your GUI. Your callback signature and body then becomes
function DATA(hObject, eventdata, hFigure)
handles = guidata(hFigure);
% etc.
Note that we pass the GUI/figure handle into this callback and, from that, retrieve the handles structure rather than passing in the handles structure directly when we assign the callback to the timer. This is because the handles structure (if passed into the timer callback) will just be a copy at the time the callback is assigned, and will not be the most up-to-date version that we would get when using guidata(hFigure).

  0 Comments

Sign in to comment.