PySerial [Error 5] Access is Denied
Asked Answered
C

7

9

I am trying to write a program in Python that will loop to keep checking the serial port (COM4) and print out a message when the character "1" is read from the serial port. I want to send "1" over the serial port from an Arduino gadget upon the push of a button.

However, I get the error "[Error 5]: Access is Denied" when I try to create an instance of a serial object. (It automatically tries to open upon instantiation, which is where the error is, from what I can see from the file in the PySerial package that handles this.)

My code:

c = serial.Serial('COM4', 9600)
while True:
    signal = c.read()
    print signal
    print "running"
    time.sleep(2)
    c.flushOutput()

It never gets past the "c = serial.Serial('COM4', 9600), though. That's where the error pops up. How can I fix this?

Carcinogen answered 28/11, 2011 at 2:32 Comment(3)
Can you add full call stack of the error message?Boatsman
Check if other process is using the com port.Aalst
Access Denied(ERR NO:5) : Already on use error, REQUEST REJECTED (ERR NO:13) : Sytstem Colsed USB or Data Bus PORT (check power management settings.)Setscrew
O
3

UPDATE: This is apparently no longer possible in PySerial 3.0.

Under Windows, I've always used the port=<int> approach with success.

I.e. change your code to:

c = serial.Serial(3, 9600)
Oldham answered 28/11, 2011 at 3:22 Comment(4)
Wow, this works! Thank you so much! Why does this work though? How does the number 3 indicate 'COM4'?Carcinogen
The port parameter can be either a string or a number. When a number under Windows, it's the zero-based COM port. So, 0 is COM1 and 3 is COM4. It's possible "COM4:" would work also, but I've never tried it.Oldham
Well, #5602849 implies that "COM4" should work. Googling turns up potential issues with the names of virtual serial ports. I guess port=<int> works around those naming issues.Oldham
ValueError: "port" must be None or a string, not <class 'int'>Setscrew
D
9

For me the solution didn't work but what worked was closing all the applications that were interacting with the given com port.

Deledda answered 11/2, 2017 at 19:24 Comment(2)
Good answer IMO: when developing for Arduino, it's rather common to have the Arduino IDE running, but this makes serial unable to access the port.Imbrication
Duh! Of course that's what I was missing. You cannot have two applications open a serial port at the same time.Diseuse
O
3

UPDATE: This is apparently no longer possible in PySerial 3.0.

Under Windows, I've always used the port=<int> approach with success.

I.e. change your code to:

c = serial.Serial(3, 9600)
Oldham answered 28/11, 2011 at 3:22 Comment(4)
Wow, this works! Thank you so much! Why does this work though? How does the number 3 indicate 'COM4'?Carcinogen
The port parameter can be either a string or a number. When a number under Windows, it's the zero-based COM port. So, 0 is COM1 and 3 is COM4. It's possible "COM4:" would work also, but I've never tried it.Oldham
Well, #5602849 implies that "COM4" should work. Googling turns up potential issues with the names of virtual serial ports. I guess port=<int> works around those naming issues.Oldham
ValueError: "port" must be None or a string, not <class 'int'>Setscrew
U
2

Please, take care with the python versions.

From the pyserial manual about: class serial.Serial https://pyserial.readthedocs.io/en/latest/pyserial_api.html#classes

...........

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() is required.

port is a device name: depending on operating system. e.g. /dev/ttyUSB0 on GNU/Linux or COM3 on Windows.

............

Changed in version 3.0: numbers as port argument are no longer supported

Unanswerable answered 27/8, 2016 at 13:0 Comment(0)
W
2

Close your serial monitor, opened from Arduino IDE.

Wilbanks answered 3/9, 2021 at 11:5 Comment(1)
Closing the serial monitor on the Arduino IDE worked for me.Urethra
K
1

For Python 2.6 use the zero-based COM port index. For Python 2.7.x you can use the full name "COM4". From my experience it's better to use the 2.7 version. Install Python 2.7.x and Setup Tools (aka Easy Install). Once you've got this, install pyserial module by typing easy_install -U pyserial (see pyserial installation doc).

Remember to add python path to PATH environmental variable.

Kairouan answered 9/3, 2013 at 15:39 Comment(0)
P
1

that works with PORT COM N-1 in python (N is your number of COM)

Presley answered 8/2, 2015 at 18:42 Comment(0)
A
0
    At one time, there is only an application that can access one com port. If application A is accessing this com port, application B can not access it. You should do these following steps as below:
    1. Find the application that is accessing this port and then close the connection.
    2. Reconnect your application to this port. You can use the below source code.
    
     port='COM8',
 baudrate = 2400,
 parity=serial.PARITY_EVEN,
 stopbits=serial.STOPBITS_ONE,
 bytesize=serial.SEVENBITS,
 timeout=None
)

while 1:
 x = ser.readline()
 print(x)
Aurita answered 24/12, 2021 at 2:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.