Hi there,
I have a problem on win7 with pyserial 2.6.
Sometimes I get an exception with the following code:
self._ser = serial.Serial() self._ser.close() # should not be needed but it does not hurt self._ser.timeout=0 self._ser.baudrate = 800000 self._ser.port = "COM8" # this is a FTDI USB-Serial adapter self._ser.open()
I get the following exception (serialwin32.py line 59):
could not open port COM8: [Error 5] Zugriff verweigert
When I try to close, delete and instantiate a new serialobject with the same code, I get on the same open() call the following exception.
Cannot configure port, some setting was wrong. Original message: [Error 995] Der E/A-Vorgang wurde wegen eines Threadendes oder einer Anwendungsanforderung abgebrochen.
This exception is in the _reconfigurePort() function (line 186) called from the open() function.
A following close() does not free the hComPort because the self._isOpen is not yet set.
And even deleting the object does not free the handle.
If I delete the serial Object, and try to create a new one, I will not be able to open the port again. I have to close my programm completely to release the handle. Even other programs can not open the port.
My workaround:
after that exception if the handle is still open, I close it by hand with
self._ser.close() if self._ser.hComPort is not None: serial.serialwin32.win32.CloseHandle(self._ser.hComPort)
Now I can reopen the serial port and work with it.
OK, i've moved setting the isOpen flag further up, so that close() does its job, even when the configuration fails.
However, i'm not sure if i understand the description of reopening the port correctly. you are saying open and close works fine the first time. Then opening that same port again (new Serial object, same port name) which fails then. and you get two exceptions printed at the same time, calling open()? If so, i'd guess that you use threads and the reading thread is still accessing the port when you close the 1st instance. It seems the OS keeps the handle for the port because of that and does not allow an other access (the "Zugriff verweigert" error).
If this is the scenario, shutting down nicely would also avoid the problem. That would be stopping the reader thread first (e.g. set a small timeout on the port and poll an "alive" flag when read() returns) and only then when the reader thread has terminated, close the port.
2.7 released