Detecting a serial port valid after disconnect/reconnect
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
1I 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 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 serial, usb, reconnect MATLAB Answers — New Questions