- One of the fields of handles doesn't exist when the callback is called. Make sure that all the fields (rx_b, stop_b, serpic, editor) are created in the OpeningFcn callback.
- If for some reason the port SerPIC is not open (or has closed unexpectedly) when the callback is called, it will error.
- Your seperate_sensor_data_and_convertvalues assumes that the message received conforms to a particular format. You never check that it actually does and if it doesn't (e.g somehow the message is corrupted), the function may error.
Error while evaluating UIControl Callback.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kamran Latif
am 24 Feb. 2020
Kommentiert: Walter Roberson
am 26 Feb. 2020
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 Kommentare
Akzeptierte Antwort
Guillaume
am 25 Feb. 2020
Bearbeitet: Guillaume
am 25 Feb. 2020
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Conway's Game of Life finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!