Reopening this thread as no response has solved the issue.

Well, I've been working with a serial (RS232) connection and facing denied access to the communication port while debugging the code in the IDE. This issue is caused by a problem in the code that fails to close the COM port before stopping. As a temporary solution, I've had to close the IDE in order to release the COM port.

I was considering that once the error code is captured, I could disable/remove the serial port and then enable/reinstall it again. By doing so, the need to close the IDE would no longer be necessary.

So, how to perform this task through code ?