Know if Serialport is connected (in App Designer program); ishandle() always true

I open a serial port to my Arduino and it usually works. I'm trying to take care of various errors, for one of which I need to know if the port handle is there and connected or not. In Command window I can tell when ishandle(MyCom) returns a logical answer
ishandle(app.PicoCom)
ans =
0×0 empty logical array
But programatically determining if it's there, say with
ishandle(app.PicoCom) == false
ans =
0×0 empty logical array
Doesn't work, it's always true. How do I ask a false logical array if it's false and why does this have to be so hard? Does anybody really need an array response to ishandle(app.PicoCom)? if ~ishandle(app.PicoCom) should be enough.
I also tried if ~exist(...) and even isstr(app.PicoCom.Port) but they don't work when it's not a handle.
BTW, ishandle() is only described as for graphic objects and figures and the existing answer doesn't answer my question

4 Comments

I just had the idea that if empty(app.MyCom) should work but
Execution of script empty as a function is not supported:
C:\Program Files\MATLAB\R2023b\toolbox\matlab\lang\empty.m
so I tried if empty(app.MyCom) == true No luck there either.
Does anybody really need an array response to ishandle(app.PicoCom)?
For that particular function, if you passed an array into it I believe the answer will always be the same for all the elements (unless maybe you have a heterogenous array hierarchy where some of the classes are handle classes while others are merely HandleCompatible, but I'm not 100% certain off the top of my head if that's allowed.)
Some of the is* functions in MATLAB return a scalar true or false value regardless of the size of the input(s). islogical and isvector are two such functions. Generally these answer a question about the "structure" of the input: classes, sizes, complexity, etc.
islogical(true)
ans = logical
1
islogical(false(0, 0))
ans = logical
1
islogical(magic(4))
ans = logical
0
isvector(magic(4))
ans = logical
0
isvector(zeros(1, 0))
ans = logical
1
Others return an array whose size is based on the size of the input(s). isfinite and isfield are examples of these, as is ishandle. Generally these are answering questions about the contents of the inputs.
isfinite([1 2 3 NaN 5 Inf 7 8])
ans = 1x8 logical array
1 1 1 0 1 0 1 1
s = struct('foo', 1, 'bar', 2);
isfield(s, ["foo", "notAField"])
ans = 1x2 logical array
1 0
f = [figure, figure; figure, figure];
ishandle(f)
ans = 2x2 logical array
1 1 1 1
Calling isempty (a "structural" question) before calling ishandle is a reasonable approach. The all function may be of use as well.
g = gobjects(0); % makes a 0-by-0 array suitable for holding graphics handles
ishandle(g)
ans = 0x0 empty logical array
all(ishandle(g))
ans = logical
1
Yes, this the expected and documented behavior. See the all function's documentation page.
There is another problem when the port connects but there is no device present. It passes the check of isempty() but what we actually have is this when I ask at the command window
app.PicoCom
ans =
Serialport with properties:
In 'testmeaslib:CustomDisplay:PropertyWarning', data type supplied is incorrect for parameter {0}.
Not empty but no properties, so I can't even ask about it in any way I know
I get
Invalid operation. Object must be connected to the serial port.
Trying many things it's stuck even when the device returns
app.PicoCom
ans =
Serialport with properties:
In 'testmeaslib:CustomDisplay:PropertyWarning', data type supplied is incorrect for parameter {0}.
K>> app.PicoCom.Port
ans =
"COM4"
K>> app.PicoCom.NumBytesAvailable
Error using matlabshared.seriallib.internal.Serial/get.NumBytesAvailable
Invalid operation. Object must be connected to the serial port.
Error in internal.Serialport/get.NumBytesAvailable (line 1553)
value = obj.Transport.NumBytesAvailable;
Serial>Serial.get.NumBytesAvailable is not currently in a debuggable state. Skipping the frame.
I'm still looking for an answer as to how to tell if a serial handle is properly connected to a device.
if isempty(app.PicoCom) || ~isvalid(app.PicoCom)
isn't telling me when the handle is stuck in never never land. I can delete it when it finds the port but not the device for a retry. But when it gets into this state (Port is there but no device)
K>> app.PicoCom
ans =
Serialport with properties:
In 'testmeaslib:CustomDisplay:PropertyWarning', data type supplied is incorrect for parameter {0}.
K>> app.PicoCom.Port
ans =
"COM4"
I can also see BaudRate, Parity so
K>> isempty(app.PicoCom)
ans =
logical
0
K>> isvalid(app.PicoCom)
ans =
logical
1
See? It's not there to use, but it is enough to fool me. It's not usable, so how do I discover this state to delete (app.PicoCom) and start over?
And in this state (before initial try:
K>> ishandle(app.PicoCom)
ans =
0×0 empty logical array
K>> all(ishandle(app.PicoCom))
ans =
logical
1
Isn't this supposed to return 0?
K>> app.PicoCom
ans =
[]
K>> isvalid(app.PicoCom)
Incorrect number or types of inputs or outputs for function isvalid.
It won't even let me check without throwing an error.
One simple check to see if I had a valid handle to an actual device so I can query it to see if it's MY device. (I have a handshake routine between the Arduino Pico and startup code that works some times but I need to make it more bulletproof for all the dumb student errors that there are sure to be. I'm still not sure how it gets into the hung state but I want to get out of it painlessly, or at least alive.

Sign in to comment.

 Accepted Answer

Hi @Gavin,
You can use the "serialportlist" function with the "available" argument to check which serial ports are available. If the Arduino board is connected, the COM port of the Arduino will be part of the result. You can use the "ismember" function to check if it is present, thus confirming that the board is connected.
If upgrading to a newer version of MATLAB is an option, since MATLAB R2024a, you can use the "serialportfind" function to check whether the serial port connection is available or not in another app. If the connection is active, it will return the "SerialPort" object back to you. Otherwise, it will be empty. If the result is not empty, you can assume the serial port is connected.
Please refer to the following resources for more information:
Hope this helps!

More Answers (0)

Products

Release

R2023b

Asked:

on 30 Jul 2024

Edited:

on 19 Aug 2024

Community Treasure Hunt

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

Start Hunting!