Callback error for Serialport

20 views (last 30 days)
Alessandro Livi
Alessandro Livi on 19 Jul 2024
Commented: Gavin on 16 Sep 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 Comments
Walter Roberson
Walter Roberson on 22 Jul 2024
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{2}));
the {2} might need to be adjusted to {1}
Alessandro Livi
Alessandro Livi on 22 Jul 2024
Have you actually tried any of these or are you still guessing?
I assume varargin is short for variable argument input. Does it have a value or is it like a ~
I tried {2}
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property
in object of matlabshared.asyncio.internal.Channel class:
Error using PicoInput
Too many input arguments.
Error in MouseOdor4>@(varargin)PicoInput(varargin{2}) (line 331)
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{2}));
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;
The I tried {1} and it still says:
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property
in object of matlabshared.asyncio.internal.Channel class:
Error using PicoInput
Too many input arguments.
Error in MouseOdor4>@(varargin)PicoInput(varargin{1}) (line 331)
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{1}));
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;
For grins I tried
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin));
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property
in object of matlabshared.asyncio.internal.Channel class:
Error using PicoInput
Too many input arguments.
And then
configureCallback(app.PicoCom,"terminator",@PicoInput(varargin));
But it couldn't even parse it correctly being confused by the ()
What! Can't copy and paste an error message in App Designer?

Sign in to comment.

Accepted Answer

Alessandro Livi
Alessandro Livi on 25 Jul 2024
After struggling with this for over a week I found this on my own.
Callback function specified by configureCallback is sometimes not executed (2626524)
Modified: 03/21/2024
Description
A callback function specified as an input to the configureCallback method of the following interface objects might not get triggered even when data is available to trigger it:
  • serialport
  • tcpserver
  • tcpclient
  • udpport
  • bluetooth
That might not be exactly the same bug but I went ahead and did the update (to rev 8)
It seems to be better now, at least it doesn't throw an error whenever I try to talk to my device, but I'm still having it drop out of the debugger when the startupfcn completes. I tried to step into the problem and could only track it to
classdef AppManagementService < handle
% APPMANAGEMENTSERVICE Singleton object to manage running apps
No particular line shown but Ap D has gone from Continue/Step options to Run. The app is still running though.
  1 Comment
Gavin
Gavin on 16 Sep 2024
I'm having the same problem. This hasn't been answered yet.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!