Error while evaluating UIControl Callback.

13 次查看(过去 30 天)
I am trying to add the functionality to an oppen source code. I made a new function and called it in the existing code. However, following error is generating on execution of the code. Although the code and new function added is working fine standalone. Please help how to overcome this isue.
"Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui_rx('rx_b_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback."
Following is the code.
function rx_b_Callback(hObject, eventdata, handles)
set(handles.editor,'String','')
set(handles.rx_b,'Enable','off'); pause(0.1)
handles.acc=[ ];
set(handles.stop_b,'UserData',0)
while 1
if get(handles.stop_b,'UserData') % Verificar ID de parada
break
end
if handles.SerPIC.BytesAvailable
A=fscanf(handles.SerPIC); % Leer buffer de entrada
%%%%%%%%%%%%%%%%%%%%%% Error Generating Code %%%%%%%%%%%%%%%%%%%%%%%%%%
seperate_sensor_data_and_convertvalues(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(handles.editor,'String',handles.acc)% Escribir mensaje
end
pause(0.02);
end
% --- FUNCIÓN DE PARADA DEL CICLO DE ADQUISICIÓN.
function stop_b_Callback(hObject, eventdata, handles)
% Colocar el ID en 1 para romper el ciclo while
set(handles.stop_b,'UserData',1)
% Cerrar el puerto serial
if strcmp(handles.SerPIC.Status,'open')
fclose(handles.SerPIC);
end
% Habilitar botones
set(handles.rx_b,'Enable','off')
set(handles.com_ser,'Enable','on')
% Borrar edit-text
set(handles.editor,'String','')
% Mensaje de aviso de cierre del puerto
warndlg('Reception END','WARNING RS-232')
% Actualizar estructura handles
guidata(hObject,handles)
%%%%%%%%%%%%%%%%%Function to calculate critical values of co2, co, and Temp
function seperate_sensor_data_and_convertvalues(packet)
packet=erase(packet," "); %str = erase(str," ") erease character from string
sensor_id= extractBefore(packet,"!");
packet = extractafter(packet,"!");
co2= hex2dec(extractBefore(packet,"!"));
if (check_critical_co2(co2))
msgbox("Co2 Critical")
end
packet = extractafter(packet,"!");
temp= hex2dec(extractBefore(packet,"!"));
packet = extractafter(packet,"!");
co= hex2dec(extractBefore(packet,"!"));
function output = check_critical_co2(val)
co2ppm=((val/4096)*2.5)*1000-200;
if (co2ppm >= 2000 ) && (co2ppm <= 5000)
output=true;
else
output=false;
end
  3 个评论
Kamran Latif
Kamran Latif 2020-2-25
Dear Guillaume. Thanks for responding my issue. Actually this code is for serial communication. The gui and complete code is attached below. Code is working fine. Receiving data realtime. I want to extend its functionality during realtime data receive. However as i added new function, above error generated. Now it looks either it is not appropriate place to add the code or I am missing some instruction of code to handle this issue.
Below is the complete code and its GUI.
function varargout = gui_rx(varargin)
% _____________________________________________________
%Author: Diego Barragán.
% www.matpic.com
% _____________________________________________________
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_rx_OpeningFcn, ...
'gui_OutputFcn', @gui_rx_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- CONDICIONES INICIALES DEL PROGRAMA
function gui_rx_OpeningFcn(hObject, eventdata, handles, varargin)
% Centrar la GUI
movegui(hObject,'center')
% Deshabilitar botón de envío serial y parada del bucle
set(handles.rx_b,'Enable','off')
set(handles.stop_b,'Enable','off')
handles.acc=[ ];
% Choose default command line output for gui_rx
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = gui_rx_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- ABRIR PUERTO SERIAL.
function com_ser_Callback(hObject, eventdata, handles)
% Desactivar botón actual
set(hObject,'Enable','off')
pause(0.1)
% Tomar y verificar el ID del puerto serial
com=get(handles.com_e,'String');
if isempty(com)||isnan(str2double(com))
return
end
% Obtener velocidad (baudrate)
contents = get(handles.los_baud,'String');
vel=str2double(contents{get(handles.los_baud,'Value')});
% Configuración del puerto serial
handles.SerPIC = serial(['COM',com]);%Seleccionar puerto COM1
set(handles.SerPIC,'BaudRate',vel);%Velocidad 9600 baudios
set(handles.SerPIC,'DataBits',8);%8 bits de datos
set(handles.SerPIC,'Parity','none');%Sin control de paridad
set(handles.SerPIC,'StopBits',1);%Un bit de parada
set(handles.SerPIC,'FlowControl','none');%Sin control de flujo
%set(handles.SerPIC,'Terminator',50);%Terminador
%set(handles.SerPIC,'TimeOut',10);%Tiempo de espera
% Abrir el puerto serial
fopen(handles.SerPIC);
% Mensaje de apertura del puerto serial
msgbox(['Port COM',com,' Enabled'],'RS_232')
% Habilitar botón de envío
set(handles.rx_b,'Enable','oN')
set(handles.stop_b,'Enable','on')
% Actualizar estructura handles
guidata(hObject,handles)
% --- FUNCIÓN DE CIERRE DE GUI.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
try
fclose(handles.SerPIC);%Cerrar puerto serial
catch
end
delete(hObject);
% --- FUNCIÓN DE RECEPCIÓN SERIAL.
function rx_b_Callback(hObject, eventdata, handles)
% Borrar edit-text de recepción de mensaje
set(handles.editor,'String','')
% Desactivar botón actual
set(handles.rx_b,'Enable','off'); pause(0.1)
% Matriz de concatenación
handles.acc=[ ];
% Poner a 0 el ID de parada del ciclo "while"
set(handles.stop_b,'UserData',0)
% Ciclo de adquisición
while 1
if get(handles.stop_b,'UserData') % Verificar ID de parada
break
end
% Si hay datos en buffer de recepción, leerlo
if handles.SerPIC.BytesAvailable
A=fscanf(handles.SerPIC); % Leer buffer de entrada
%A=A(1:end-1); % Quitar "terminator" (~)
%%%%%%%%%%%%%%%%%%%%%%Error Generating Code %%%%%%%%%%%%%%%%%%%%%%%%%%
seperate_sensor_data_and_convertvalues(A);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(handles.editor,'String',handles.acc)% Escribir mensaje
end
pause(0.02);
end
% --- FUNCIÓN DE PARADA DEL CICLO DE ADQUISICIÓN.
function stop_b_Callback(hObject, eventdata, handles)
% Colocar el ID en 1 para romper el ciclo while
set(handles.stop_b,'UserData',1)
% Cerrar el puerto serial
if strcmp(handles.SerPIC.Status,'open')
fclose(handles.SerPIC);
end
% Habilitar botones
set(handles.rx_b,'Enable','off')
set(handles.com_ser,'Enable','on')
% Borrar edit-text
set(handles.editor,'String','')
% Mensaje de aviso de cierre del puerto
warndlg('Reception END','WARNING RS-232')
% Actualizar estructura handles
guidata(hObject,handles)
%%%%%%%%%%%%%%%%%Function to calculate critical values of co2, co, and Temp
function seperate_sensor_data_and_convertvalues(packet)
packet=erase(packet," "); %str = erase(str," ") erease character from string
sensor_id= extractBefore(packet,"!");
packet = extractafter(packet,"!");
co2= hex2dec(extractBefore(packet,"!"));
if (check_critical_co2(co2))
msgbox("Co2 Critical")
end
packet = extractafter(packet,"!");
temp= hex2dec(extractBefore(packet,"!"));
packet = extractafter(packet,"!");
co= hex2dec(extractBefore(packet,"!"));
function output = check_critical_co2(val)
co2ppm=((val/4096)*2.5)*1000-200;
if (co2ppm >= 2000 ) && (co2ppm <= 5000)
output=true;
else
output=false;
end

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2020-2-25
编辑:Guillaume 2020-2-25
I cannot debug the code for you, I neither have the time or the necessary hardware to receive the serial data.
The problem is nothing to do with the location of the code you've added. Your callback is called succesfully, but an error occurs inside it so something you didn't expect happens within the callback. Your code makes many assumptions that things happen as they should but never check that it is the case and will error if they don't. When dealing with external input such as serial data or user input it's always a good idea to check that what you get is what you expect.
For example:
  • your code assumes that the serial port opens succesfully. It never check that it does nor that there is something at the other hand that responds or transmit
  • your code assumes that it is receiving messages in a particular format. Again, you never checks that the messages received are actually in that format. If the message isn't, your code will error. I suspect that it is what is happening.
So, as I said, use the debugging commands I showed you to see what is actually happening. Only you, with the appropriate hardware can find out why the callback errors.
And at the very least, inside seperate_sensor_data_and_convertvalues, I would add a check that the message is what is expected, so after the:
packet=erase(packet," "); %str = erase(str," ") erease character from string
I would add:
if isempty(regexp(packet, "^[^!]+![A-Fa-f0-9]+![A-Fa-f0-9]+![A-Fa-f0-9]+!", "once"))
warndlg(sprintf('Unexpected message received: %s', packet));
end
edit: in fact you could replace the whole function by:
function seperate_sensor_data_and_convertvalues(packet)
packet = erase(packet, " ");
decoded = regexp(packet, "^([^!]+)!([A-Fa-f0-9]+)!([A-Fa-f0-9]+)!([A-Fa-f0-9]+)!", "tokens", "once");
if isempty(decoded)
warndlg(sprintf('Unexpected message received: %s', packet));
else
sensor_id = decoded(1);
co2 = hex2dec(decoded(2));
temp = hex2dec(decoded(3));
co = hex2dec(decoded(4));
%add your checks after that
end
end
edit 2: there's certainly a few things not right about your code, but this is not what causes the error:
  • seperate_sensor_data_and_convertvalues does not return anything, wich is fine since your callback doesn't require any output from it. However, it's unclear what the point of the function is, nor why it defines several unused variables, in particular output.
  • Your callback defines handles.acc as [], never modifies it, and then sets that as the text of handles.editor.
Again, use the debugger to check that what is happening is what you meant to happen.
  1 个评论
Kamran Latif
Kamran Latif 2020-2-26
Thanks Mr. Guillaume. I really appriciate your effort and time for guidance. Hopefully it will resolve my issue.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by