MATLAB: Am I unable to use the serial port after a hardware reset if the port was open in MATLAB

MATLABreconnectserial

I use USB-devices that create virtual serial ports in Windows, for instance COM5. When MATLAB opens a connection with one of these devices and the device disconnects and reconnects, I am unable to use the device any more. What can I do to regain access to the device?
 
myComPort = serial('COM5');
fopen(myComPort);
% now disconnect the device
% now connect the device again, the following will be unsuccessful:
delete(myComPort)
clear myComPort
myComPort = serial('COM5');
fopen(myComPort);
Error using serial/fopen (line 72)
Open failed: Port: COM5 is not available. Available ports: COM1, COM3.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.

Best Answer

When a serial port connection is opened and the device becomes unavailable, the device remains unavailable after reconnecting. There is currently no way in MATLAB to fully reset the status of this connection, once this state is reached.
As a workaround you can first delete the serial port connection and then reconnect the device. For instance:
 
myComPort = serial('COM5');
fopen(myComPort);
% now disconnect the device
delete(myComPort)
clear myComPort
% now connect the device again, the following will now be successful:
myComPort = serial('COM5');
fopen(myComPort);
Related Question