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

Roel Schroeven roel at roelschroeven.net
Thu Jun 28 15:08:29 EDT 2012


Temia Eszteri schreef:
> 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.

That doesn't have anything to do with Windows, but with how pySerial 
works. See the documentation for __init__():

"The port is immediately opened on object creation, when a port is 
given. It is not opened when port is None and a successive call to 
open() will be needed."

So if your script does something like

prt = serial.Serial('COM4')

then pySerial automatically opens the port, and you shouldn't call 
prt.open() anymore.

If, on the contrary, you do something like

prt = serial.Serial()
prt.port = 'COM4'

then pySerial doesn't open the port, and you have to call prt.open() to 
do it.

PySerial has this same behavior on both Windows and Linux. The 
difference might be that on Linux it is possible to open serial ports 
more than once, while that doesn't work on Windows.

Best regards,
Roel




More information about the Python-list mailing list