PySerial could not open port COM4: [Error 5] Access is denied - please help

Adam adam at no_thanks.com
Wed Jun 27 20:14:51 EDT 2012


"Grant Edwards" <invalid at invalid.invalid> wrote in message 
news:jsg4o8$o4p$1 at reader1.panix.com...
> On 2012-06-27, Adam <adam at no_thanks.com> wrote:
>>
>>> Actually, I believe someone in an earlier thread in the newsgroup or
>>> elsewhere pointed out that serial ports automatically open under
>>> Windows. I'd have to look it back up when I have the time, which I
>>> don't have at the moment, unfortunately.
>
> What they're referring to is that on startup, Windows used to open
> serial ports and query them to see if there was a serial mouse
> connected.  If it _thought_ it found a mouse, it would then hold the
> port. I don't think that behavior has been enabled by default for a
> long time.
>
> If that were the case, then your terminal program wouldn't be able to
> open the port either.
>
> However, IIRC, some versions of windows do open and then close the
> ports during the bus/device enumeration step of startup. However, they
> don't keep the port open, so it doesn't affect the ability of user
> applications to later open the port.
>
>> Thanks, I think I read that as well but can't recall where.
>>
>> I am just running Python scripts (downloaded), which is not opening
>> the serial port more than once (as Grant keeps assuming).
>
> Well, I'm assuming your description of what you're doing is accurate.
>
> If you're telling the truth, then the program is opening the port more
> than once.
>
> If the port wasn't already open, then calling ser.close() wouldn't do
> _anything_.  Here's the close() implmentation from pyserial:
>
>    def close(self):
>        """Close port"""
>        if self._isOpen:
>            if self.hComPort:
>                # Restore original timeout values:
>                win32.SetCommTimeouts(self.hComPort, self._orgTimeouts)
>                # Close COM-Port:
>                win32.CloseHandle(self.hComPort)
>                win32.CloseHandle(self._overlappedRead.hEvent)
>                win32.CloseHandle(self._overlappedWrite.hEvent)
>                self.hComPort = None
>            self._isOpen = False
>
> There's only _one_ place where self._isOpen is set to True, and that's
> at the end of the open() call:
>
>    def open(self):
>        """Open port with current settings. This may throw a 
> SerialException
>           if the port cannot be opened."""
> [...]
>        self._overlappedWrite.hEvent = win32.CreateEvent(None, 0, 0, None)
>        self._isOpen = True
>
> If you have to add the call "ser.close()" before you can open the port
> with "ser.open()", then that means that the port _was_already_open_.
>
> -- 
> Grant Edwards               grant.b.edwards        Yow! World War III?
>                                  at               No thanks!
>                              gmail.com


Obviously pySerial considers the serial port open and will not open an 
already open serial port.
However, why is it that TeraTerm can open the serial port?

Here's the link where I read about calling ser.close() before ser.open() ...

Trying to open a serial port with pyserial on WinXP -> "Access denied"
http://stackoverflow.com/questions/2063257/trying-to-open-a-serial-port-with-pyserial-on-winxp-access-denied


Here's the Python scripts ...
https://github.com/adafruit/Tweet-a-Watt/downloads
Click on the "Download as ..." button for the Python scripts





More information about the Python-list mailing list