Reputation: 1088
I can control my device for the first time I run python script, however, if I'm trying to run the code second time I have the following error:
SerialException: could not open port 'COM3': WindowsError(5, 'Access is denied.')
If I disable and enable the port, the code works again.
The main question is: How to close opened port?
I've tried to close the port using the following code, however, it didn't help:
import serial
ser = serial.Serial()
ser.baudrate = 38400
ser.port = 'COM3'
ser.close()
I would appreciate any suggestions and solutions.
Upvotes: 0
Views: 2848
Reputation: 1088
Special thanks to @Pourya and @Jaba for helping.
All I had to do is following:
from pipython.interfaces.piserial import PISerial;
import serial
try:
gateway = PISerial('COM3', 38400);
except serial.serialutil.SerialException:
gateway.close();
gateway = PISerial('COM3', 38400);
Upvotes: 1
Reputation: 27485
I'm not sure if Windows 10 is different. Although I did a quick search and found this. Seems my hunch was right. (or at least I hope)
My assumption is you're not running the program with admin permissions. Run it from within cmd
with admin privelages and see if this works.
If not, see if the port isn't just disabled from in device manager.
Upvotes: 0
Reputation: 90
As I had worked with ports, I've gotten this error many times and most of the times, the problem was that the process that's using the port is still running, it may be in a while loop, so you should terminate the running code or write your code in a way to break the loop.
Upvotes: 0