Reputation: 11
I have been coding in python to develop a library for handling serial connecitons. It so happens that a python process doesn't end with the end of my program and the next time when I run my program and try to open a serial port (which I have closed mostly always ) it returns with a windows error 5,Access is Denied.
If I manually check in taskbar and kill the old python process I can connect to the com port. I want to handle this exception programmtically by: 1. Finding the old process holding my serial port and killing it through code. 2. having some cleanup operation before I start my code.
Can any one suggest some techniques ? by the way I can only work with Python 2.7
Upvotes: 1
Views: 2726
Reputation:
This is a limitation that all programs run into. (I've had this happen with expensive commercial products as well)
You might try unplugging and replugging the serial adapter, but apart from that it's best to just perform proper exception handling (which python is well-suited to), and ensure that you always close the port before exiting the program.
The simplest form of this would be:
try:
# all your code...
except Exception as e:
print("unhandled exception: {}".format(str(e))
finally:
# close the serial port...
Even in the case where you don't have a handler for the specific exception, you're always guaranteed to hit the code that will close/release the serial port.
Upvotes: 1