Callback error for Serialport

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

Try making PicoInput into its own .m file.
Why? and How do I do that?
And how do I get App Designer to find it and use it?
If it is in its own .m file then it will be found by normal MATLAB search procedures.
OK Made an M file, and yes it is found when in same directory as the mlapp
Program starts OK, execute a callback function (click a button that writes to and triggers a response)
Get this error:
Error using internal.Serialport/callbackFunction
Too many input arguments.
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));
Error in MouseOdor4/ManualRewardsButtonGroupSelectionChanged (line 562)
pause(0.3);
My mfile PicoInput.m
% callback for serial line input from Pico
% function PicoInput(src) % what arguments does it need?
function PicoInput(src)
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
Startup configures it like this
configureCallback(app.PicoCom,"terminator",@(src) PicoInput(src));
Looking at serialport.m for clues:
function callbackFunction(obj, ~, evt)
% This is the callback function that gets fired whenever a bytes
% available callback event occurs. The BytesAvailableFcn
% contains the function handle for the specified callback
% function, set in configureCallback.
dataAvailableInfo = instrument.internal.DataAvailableInfo( ...
obj.BytesAvailableFcnCount, evt.AbsTime);
obj.BytesAvailableFcn(obj, dataAvailableInfo);
end
What a pain! If I don't have blank lines after code then I can't add anything to the comment when code is att the bottom. I had to undo (line by line the code tags), add blank lines, then reset code area with spaces below it! Worth fixing?
So now I tried since there are too many input args
configureCallback(app.PicoCom,"terminator",@(src) PicoInput);
Warning: Error executing listener callback for PostSet event on TotalBytesWritten dynamic property in object of matlabshared.asyncio.internal.Channel class:
Error using MouseOdor4>@(src)PicoInput
Too many input arguments.
Zero is too many? Not what it's talking about?
From serialport.m comments (which I shouldn't have to read if there was a working example for me
function configureCallback(obj, varargin)
% CONFIGURECALLBACK(OBJ,MODE,CALLBACKFCN) - For this syntax,
% the only possible value for MODE is "terminator". This sets the
% BytesAvailableFcnMode property to "terminator". CALLBACKFCN
% is the function handle that is assigned to BytesAvailableFcn.
% CALLBACKFCN is triggered whenever a terminator is available
% to be read.
Took out the (src)
configureCallback(app.PicoCom,"terminator",@PicoInput);
OH! That almost worked! Got the reply showing up in the Debug box.. But still
Removed (src) from PicoInput.m From Command window:
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.
In App Designer (in Red)
Error using internal.Serialport/callbackFunction
Too many input arguments.
Error in internal.Serialport>@(varargin)obj.callbackFunction(varargin{:}) (line 980)
obj.StringClient.StringReadFcn = @obj.callbackFunction;
I'm running out of ideas! How do I get this to work? I've spent 4 days on what should be really simple!
How could a non programmer possible be expected to do this? I have 30+ years of coding experience.
configureCallback(app.PicoCom,"terminator",@(src) PicoInput(src));
Callbacks are always passed (at least) two parameters, src and event. You would need
configureCallback(app.PicoCom,"terminator",@(src,event) PicoInput(src));
Nope that didn't work. Notice that the error is TOO MANY, not: not enough arguments
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>@(src,event)PicoInput(src) (line 329)
configureCallback(app.PicoCom,"terminator",@(src,event) PicoInput(src));
It wouldn't hurt to document what really works somewhere.
Here's what I've tried so far
% Set callback to handle future inputs
% app.PicoCom.configureCallback("terminator",@PicoInput);
% configureCallback(app.PicoCom,"terminator",@PicoInput);
% configureCallback(app.PicoCom,"terminator",@app.PicoInput); % answer 555832
% As recommended by staff
% configureCallback(app.PicoCom,"terminator",@(src) PicoInput(src));
% Next guy (Walter Roberson MVP) suggests make PicoInput a separate m file
% Did that and then connect how?
% configureCallback(app.PicoCom,"terminator",@(src) PicoInput(src)); %nope
% configureCallback(app.PicoCom,"terminator",@(src) PicoInput); % NOPE
% configureCallback(app.PicoCom,"terminator",@PicoInput); % almost!
configureCallback(app.PicoCom,"terminator",@(src,event) PicoInput(src));
Still not working
Another thing that isn't clear
% What parameters for the function in the m file and why does it need a separate file?
% function PicoInput(src) % need app, ? Use real name or src? No event?
function PicoInput % (src) % If I take out (src) how would it know where to read from? Seems to work in that the text from COM is read and displayed, THEN it crashes
configureCallback(app.PicoCom,"terminator",@(app,src,event) PicoInput(src));
I tried that and this is the result. The command completes. the text goes to my debug window and then.
Too many input arguments.
obj.BytesAvailableFcn(obj, dataAvailableInfo);
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)
obj.(name) = value;
@(source, data) obj.onPropertyChanged(data.Name, data.Value));
pause(0.3);
Removing the pause(0.3); makes no difference.
Now error message ends with
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)
configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{2}));
the {2} might need to be adjusted to {1}
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.

2 Comments

I'm having the same problem. This hasn't been answered yet.
You might want to start a new post about the specific problem that still remans. This one might not get new attention since it has an accepted answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions 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!