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

Grant Edwards invalid at invalid.invalid
Thu Jun 28 16:15:15 CEST 2012

On 2012-06-28, Adam <adam at> wrote:

> Obviously pySerial considers the serial port open

Because it's already been opened by the Python program.

> and will not open an already open serial port.

Pyserial will happily try if you call the open() of a port that's
already open, but Windows will return an error.

> However, why is it that TeraTerm can open the serial port?

Because TeraTerm only opens it once.

> Here's the link where I read about calling ser.close() before
> ...
> Trying to open a serial port with pyserial on WinXP -> "Access denied"

That code is broken.

The port is opened by this line:

 self.ser=serial.Serial(port='\\.\COM1', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

And then the author tries to _open_it_a_second_time_ here:
> Here's the Python scripts ...
> Click on the "Download as ..." button for the Python scripts

Like I kept telling you, those programs are trying to re-open a port
that they've already open.  Here's the erroneous code:

    54  # open up the FTDI serial port to get data transmitted to xbee
    55  ser = serial.Serial(SERIALPORT, BAUDRATE)

Line 55 opens the serial port.

Line 56 tries to _open_it_again_.  _The_port_is_already_open_. 
Windows doesn't allow a serial port to be opened twice, therefore the call raises an exception.

Just delete line 56.

The same thing happens here:

    84  # open up the FTDI serial port to get data transmitted to xbee
    85  ser = serial.Serial(SERIALPORT, BAUDRATE)

Just delete line 86.
See how simple it was to get the problem solved once you posted the
actual code?

Grant Edwards               grant.b.edwards        Yow! HUMAN REPLICAS are
                                  at               inserted into VATS of
                                NUTRITIONAL YEAST ...

More information about the Python-list mailing list