How do I reconnect USB serial that gets lost programatically.

31 ビュー (過去 30 日間)
Gavin
Gavin 2025 年 1 月 18 日
コメント済み: Gavin 2025 年 2 月 5 日 0:11
MatLab R2024b using AppDesigner on Windows 10 Enterprise
My GUI app connects to an Arduino which sometimes looses it's connection or reboots itself.
I'm trying to detect when connection is lost and reconnect, but after getting an error message in the Command Window:
Unable to detect connection to the serialport device. Ensure that the device is plugged in and create a new serialport object.
My program (at least interacting with it in the CW is hung until I Ctrl-C.
I have set a dummy breakpoint so I can see variables from the Command Window (It doesn't seem to know anything about my app until I break, but at least the program still runs after the break button is pushed)
So here's what happens in CW with my comments added in bold and extra blank lines removed:
2513 pause(0.5); Pressed the Break Button
K>> app.PicoCom Look at my Serial Port connection
ans =
Serialport with properties:
Port: "COM11"
BaudRate: 57600
NumBytesAvailable: 0
etc... Now I reboot my Arduino and get:
Unable to detect connection to the serialport device. Ensure that the device is plugged in and create a new serialport object.
K>> app.PicoCom Try to look at serial port (or do ANYTHING)
[CW hangs until I hit Ctrl-C - WHY?
Operation terminated by user during MouseOdor61/RunOneTrial (line 1131)
In MouseOdor61/DoSession (line 1238)
etc
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
newCallback = @(source, event)executeCallback(ams, ...
^^^^^^^^^^^^^^^^^^^^^^^^
Interrupt while evaluating Button PrivateButtonPushedFcn.
ans =
Serialport with properties:
In
'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for parameter
{0}.
Now I can try to see the handle again.
K>> app.PicoCom
ans =
Serialport with properties:
In
'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for
parameter {0}.
Not very helpful. At this point I can see:
K>> serialportlist()
ans =
1×2 string array
"COM1" "COM11"
To recover this seems to work:
K>> delete(app.PicoCom)
Let's check
K>> app.PicoCom
ans =
handle to deleted Serialport
Reconnect
K>> app.PicoCom = serialport
[ shows every object in app! If I don't add the ;]
K>> app.PicoCom
ans =
Serialport with properties:
Port: "COM11"
BaudRate: 57600
Tag: ""
NumBytesAvailable: 0
OK, it's back, all I need to add, it appears is the callback
K>> configureCallback(app.PicoCom,"terminator",@app.PicoInput)
I've tried putting things in my callback for input (like try ... catch) but it never gets there.
It's stuck in matlab.apps.AppBase not my code
I also added checks in the output routine to the Arduino, but it doesn't seem to be helpful. That thread is stuck! I can press another button in the app the forces a reconnect but I want it to happen without user intervention.
How can I detect and trap the loss of COM in my program? (Obviously I want to stop the Pico from rebooting, but meanwhile my users need some decent work around).
  1 件のコメント
Gavin
Gavin 2025 年 1 月 18 日
One more thing. How do I even tell that the connection is lost?
K>> app.PicoCom
ans =
Serialport with properties:
In
'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for parameter
{0}.
Looks broken to me, but...
K>> isvalid(app.PicoCom)
ans =
logical
1
How do I tell if the port is actually valid so I need to re-initialize the Arduino?
K>> serialportfind Is this a reliable way to look?
ans =
Serialport with properties:
In
'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for
parameter {0}.
Apparently not, not even if I have deleted the port
K>> app.PicoCom
ans =
handle to deleted Serialport
Not even useful if I've reconnected to see if all is well
K>> serialportfind
ans =
1×2 Serialport array with properties:
Serialport with properties:
Invalid or deleted object.
Serialport with properties:
Port: "COM11"
BaudRate: 57600
Tag: ""
NumBytesAvailable: 0
Show all accessible properties of Serialport
How do you get rid of a deleted object?

サインインしてコメントする。

採用された回答

Naga
Naga 2025 年 1 月 30 日 8:49
I understand you're attempting to reconnect a USB serial device after it becomes unavailable. In MATLAB, once a serial port connection is opened and the device becomes unavailable, it remains in that state even after reconnection. Unfortunately, there isn't a built-in way to completely reset the connection status once this occurs.
However, a workaround exists: you can delete the existing serial port connection and then establish a new connection. For example:
myComPort = serial('COM5');
fopen(myComPort);
% Now disconnect the device
delete(myComPort);
clear myComPort;
% Reconnect the device; the following will now be successful:
myComPort = serial('COM5');
fopen(myComPort);
  1 件のコメント
Gavin
Gavin 2025 年 2 月 5 日 0:11
That was the easy part. What I really needed is to detect when it gets disconnected.
What I didn't know and found in another Ask is to use this:
app.PicoCom.ErrorOccurredFcn=@(~)app.PicoError; %reconnect
My PicoError() routine can then do the reconnecting as suggested above.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by