Callback error for Serialport
    16 次查看(过去 30 天)
  
       显示 更早的评论
    
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 个评论
  Walter Roberson
      
      
 2024-7-22
				 configureCallback(app.PicoCom,"terminator",@(varargin) PicoInput(varargin{2}));
the {2} might need to be adjusted to {1}
采纳的回答
  Alessandro Livi
 2024-7-25
        2 个评论
  Christopher Stapels
    
 2024-12-12
				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.
更多回答(0 个)
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




