python, pyserial windows xp [error 5] access denied

Hi,

I am new to python and arduino, but I am trying to create a serial connection with pyserial between my arduino and python. I have been unable to get python to connect to my com port. attempting to run the following code:

Arduino Code:
int potPin = 0;
void setup()
{
** Serial.begin(9600);**
}
void loop()
{
** int val = map(analogRead(potPin), 0, 1023, 0, 255);**
** Serial.println(val);**
** delay(1000);**
}
Python Code:
import serial
serialport = 2
ser = serial.Serial(serialport, 9600)
while(1):
** ser.readline()**

I am able to read data being output from my arduino via the arduino serial port monitor, but when I run the python code I get the following error while the arduino is outputting serial data:

Traceback (most recent call last):
** File "C:/Python32/Scripts/serial-test", line 4, in **
** ser = serial.Serial(serialport, 9600)**
** File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 30, in init**
SerialBase.init(self, args, kwargs)
** File "C:\Python32\lib\site-packages\serial\serialutil.py", line 260, in init

** self.open()
*
** File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 56, in open**
** raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))**
serial.serialutil.SerialException: could not open port COM3: [Error 5] Access is denied.

If I try to run the python code without the arduino running I get the following error:

Traceback (most recent call last):
** File "C:/Python32/Scripts/serial-test", line 4, in **
** ser = serial.Serial(serialport, 9600)**
** File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 30, in init**
SerialBase.init(self, args, kwargs)
** File "C:\Python32\lib\site-packages\serial\serialutil.py", line 260, in init

** self.open()
*
** File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 56, in open**
** raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))**
serial.serialutil.SerialException: could not open port COM3: [Error 2] The system cannot find the file specified

Please help. I do not understand what is wrong. I have searched google, and the arduino forums, and I have not found a solution to this problem. Do I need to use two different com ports, and if so how do I assign python a com port?

Any help will be greatly appreciated.

Thanks!

serialport = 2

serial.serialutil.SerialException: could not open port COM3: [Error 5] Access is denied.

serial.serialutil.SerialException: could not open port COM3: [Error 2] The system cannot find the file specified

Is the Arduino COM2 or COM3?

Hi, thanks for the response.

The arduino is on com3, and serialport=2 is the integer call for the com port it is the same as calling com3 because the integer call for the com port starts at 0, so 0=com1, 1=com2 and so on.

I read that on another forum. It suggested calling the comport as an integer to solve the error that I was having, so I tried it, but I am still getting the same error.

Are you attempting to run the Python script while Serial Monitor is open?

Well I have tried to run the python script with the serial monitor open and with it closed, and I get the same error 5 message as long as the arduino is plugged into my computer before I run the python script. If I run the python script before I plug in my arduino uno I get the error 2 message because python can not find com 3.

gamerglen1246:
Well I have tried to run the python script with the serial monitor open

That will not work. Windows does not allow serial ports to be shared.

and with it closed,

That is the correct choice.

and I get the same error 5 message as long as the arduino is plugged into my computer before I run the python script.

You are getting an access denied error before running the Python script?

Thanks, I think you have found the problem. I get [error 5] if I open the arduino serial monitor before I run my python script.

I do not get the error if I just plug in the arduino and then run my python script.

Thank you for all your help.

I modified my python code to the following:

import serial

serialport = "com3"
ser = serial.Serial(serialport, 9600)
while(1):
val=ser.readline()
print (val)

And I was expecting to see the same output that I saw on the serial monitor, but I am getting the output in a different format. Instead of just printing a number between 0 and 255 it is printing b' 0\r\n thru b' 255\r\n. Can you help me understand why it is printing values like this?

I'm assuming you're using Python 3...

readline returns a bytes datatype. bytes can be converted to a string with the decode method...

  str = val.decode( 'ascii' )

The carriage-return-line-feed can be removed with the strip method...

  str = str.strip()

All of that can be performed at once...

  str = ser.readline().decode( 'ascii' ).strip()

Your assumption is correct. I am using python 3.

By adding the code that you suggested the output looks the same as the arduino serial monitor.

Thanks again for all your help.

I am posting my final python code below:

import serial

serialport = "com3"
ser = serial.Serial(serialport, 9600)
while(1):
val=ser.readline().decode( 'ascii' ).strip()
print (val)

Tip of the hat to you sir! That was sorely needed. Didn't have any of these issues on Lubuntu but moving to windows was every bit as much trouble as expected.

I also found windows much more restrictive and needed port closing and opening with pyserial on python 3.6