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

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
Gavin
Gavin il 31 Lug 2024
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.
Gavin
Gavin il 1 Ago 2024
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.

Accedi per commentare.

Risposta accettata

Malay Agarwal
Malay Agarwal il 19 Ago 2024
Modificato: Malay Agarwal il 19 Ago 2024
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!

Più risposte (0)

Categorie

Scopri di più su Debugging and Analysis in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by