Callback error for Serialport
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alessandro Livi
el 19 de Jul. de 2024
Comentada: Gavin
el 16 de Sept. de 2024
Using App Designer on Win10
I successfully open a serial port, communicate with my Pico Arduino doing a handshake that verifies that I have the correct device.
But when I put in the callback function for received data it sends a warning to the MatLab Command window many seconds AFTER StartupFcn completes. (Got "Setup Done" to debug text window I made)
Warning:
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property
in object of matlabshared.asyncio.internal.Channel class:
Undefined function 'PicoInput' for input arguments of type 'internal.Serialport'.
After that it sends this error to the command window every few seconds:
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property
in object of matlabshared.asyncio.internal.Channel class:
Undefined function 'PicoInput' for input arguments of type 'internal.Serialport'.
Error in internal.Serialport/callbackFunction (line 1472)
obj.BytesAvailableFcn(obj, dataAvailableInfo);
Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 980)
obj.StringClient.StringReadFcn = @obj.callbackFunction;
Error in matlabshared.transportclients.internal.StringClient.StringClient/DataAvailableCallback
Error in
matlabshared.transportclients.internal.StringClient.StringClient>@(varargin)obj.DataAvailableCallback(varargin{:})
Error in matlabshared.transportlib.internal.DataReceivedHandlerMixin/valuesWrittenUpdated
Error in
matlabshared.transportlib.internal.DataReceivedHandlerMixin>@(src,evt)obj.valuesWrittenUpdated(src,evt)
Error in matlabshared.asyncio.internal.Channel/onPropertyChanged (line 569)
obj.(name) = value;
Error in
matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value)
(line 465)
@(source, data) obj.onPropertyChanged(data.Name,
data.Value));
> In matlabshared.asyncio.internal/Channel/onPropertyChanged (line 569)
In matlabshared.asyncio.internal.Channel>@(source,data)obj.onPropertyChanged(data.Name,data.Value) (line 465)
>>
Nothing is being sent or received
The GUI figure keeps running until I try to send a command then the App Designer debugger closes (goes back to Run from Step) though the app stays open and keeps running (other callbacks still work)
One odd thing is that it shouldn't even be activating the callback since nothing is coming in.
I only need the callback for received messages, output commands to Pico are sent from other functions and callbacks.
StartupFcn Code:
function startupFcn(app)
% init vars to default start values
% (probably not needed as one can usually call .Value property)
app.ITImax = app.ITI_maxSpinner.Value;
app.ITImin = app.ITI_minSpinner.Value;
app.OdorTm = app.OdorDurationsSpinner.Value;
app.EndSndTm = app.EndSoundDurSpinner.Value;
app.ErrorSndTm = app.ErrorSoundSpinner.Value;
app.VlvOTm = app.ValOpenTimeSpinner.Value;
% init find and the serial port
ports = serialportlist("available");
% There's the mouse on Com1 so are there more Coms?
if size(ports,2)==2 % 2 cols so 2 or more S ports
app.PicoCom = serialport(ports(1,2),115200); % attach port
configureTerminator(app.PicoCom,"CR/LF"); % What Pico does
% send ID to see if we have correct "instrument"
flush(app.PicoCom); % start clean
write(app.PicoCom,'i','char'); % prompt for id
pause(0.02); % Need a delay? Why not? USB or Pico could be slow
if app.PicoCom.NumBytesAvailable > 1 % got something
idData = read(app.PicoCom,2,'char');
if ~ischar(idData) % must be a char array unless
warningMessage = ...
sprintf('Warning: Pico not found. Maybe another USB device at %s',app.PicoCom.Port);
uiwait(msgbox(warningMessage));
return;
end
else % no answer
idData = ' '; % No ID data
warningMessage = ...
sprintf('Warning: Pico not responding at %s',app.PicoCom.Port);
uiwait(msgbox(warningMessage));
return;
end
% ok, got a 2 char string...
if strcmp(idData,'#1') % There's my baby
flush(app.PicoCom); % clear Serial input and output buffers
write(app.PicoCom,'d','char'); % gotya - Pico can move on
% Set callback to handle future inputs from Pico
app.PicoCom.configureCallback("terminator",@PicoInput);
else % wrong answer
warningMessage = sprintf(...
'Warning: Pico not found. There is another USB device at %s',app.PicoCom.Port);
uiwait(msgbox(warningMessage));
end
else
warningMessage = sprintf('Warning: No Serial devices found.');
uiwait(msgbox(warningMessage));
end
app.DebugText.Value = "Setup done";
% about to end startupFcn
end
Output Function:
% Output to Pico
function CmdToPico(app,toPicoStr)
% app.DebugText.Value = toPicoStr;
write(app.PicoCom, toPicoStr, "string");
end
Here is PicoInput callback: (I can't add anything after the next code section. It's at the bottom of this window! Probably enough info, but ask if you need more.
% callback for serial line input from Pico
function PicoInput(app,src,~) % don't need event?
raw = readline(src); % src.UserData??
if raw{1}=='#' % A debug line from Pic0
app.DebugText.Value = raw;
else
app.PicoType = raw{1}; % first char always "type"
app.PicoData = str2double(raw{2:end}); % numeric data starts at second char
app.PicoData = uint16(app.PicoData); % save as integer
% Show Pico Dat String after parsing
PicoS = sprintf("Char: %c Value: %d", app.PicoType, app.PicoData);
app.DebugText.Value = PicoS;
end
end
13 comentarios
Walter Roberson
el 22 de Jul. de 2024
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{2}));
the {2} might need to be adjusted to {1}
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!