Detecting a serial port valid after disconnect/reconnect
44 ビュー (過去 30 日間)
古いコメントを表示
I asked before with a long question and got no answers, so let's keep it simpler.
Serial port is connected to Arduino
Arduino resets, so I need to:
1. Detect that it's gone without throwing an error
2. Reconnect
Results:
Connected and callback set
K>> app.Pico
ans =
Serialport with properties:
Port: "COM16"
BaudRate: 57600
NumBytesAvailable: 0
Hit reset on Pico get following error message ( but it doesn't stop my program from running)
Unable to detect connection to the serialport device. Ensure that the device is plugged in and create a new serialport object.
If there is no "keep alive" function for serial ports ( Walter Roberson in https://www.mathworks.com/matlabcentral/answers/1722140-cant-read-the-serial-port) then what is generating this error? How can I catch it without constantly checking the connection myself?
If I could trap the error here that would be a great start.
So let's look at where we are:
K>> app.PicoCom
ans =
Serialport with properties:
In 'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for parameter {0}.
Not useful, but I can check it right?
K>> isvalid(app.PicoCom)
ans =
logical
1
Really? It's still valid? Let's try
K>> serialportlist("available")
ans =
1×2 string array
"COM1" "COM16"
OK, at least I can see it's back in the list of available ports. Is this the best (fastest) way to find out?
Also I can't check in the input call back because then it's too late. Error thrown already.
K>> delete(app.PicoCom)
K>> app.PicoCom
ans =
handle to deleted Serialport
K>> isvalid(app.PicoCom)
ans =
logical
0
At least once deleted it's not valid. Let's reconnect:
K>> app.PicoCom = serialport()
K>> app.PicoCom
ans =
Serialport with properties:
Port: "COM16"
BaudRate: 57600
NumBytesAvailable: 0
ByteOrder: "little-endian"
DataBits: 8
StopBits: 1
Parity: "none"
FlowControl: "none"
Timeout: 10
Terminator: "CR/LF"
Back to normal as soon as I add
K>> configureCallback(app.PicoCom,"terminator",@app.PicoInput);
So I think I've got recovery OK. How about detecting when coms are lost?
BTW serialportfind is only useful when teh prot is there. If it's missing then looking throws an error...
K>> serialportfind
ans =
Serialport with properties:
In 'testmeaslib:CustomDisplay:PropertyWarning',
data type supplied is incorrect for parameter {0}.
Apparently it's a valid question to ask, just not a useful answer.
K>> isvalid(serialportfind)'
ans =
logical
1
3 件のコメント
採用された回答
埃博拉酱
2025 年 1 月 22 日 1:40
You may want to set the ErrorOccurredFcn property of serialport:
S=serialport('COM4',9600);
S.ErrorOccurredFcn=@(varargin)disp(varargin);
4 件のコメント
埃博拉酱
2025 年 1 月 22 日 10:10
The official documentation doesn't give a function signature for ErrorOccurredFcn, so you'll have to try it yourself. I don't know how many input arguments I need, too, so I used varargin to accept any number of arguments. You can also use varargin, or disp varargin like I did and see how many inputs there are.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!