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!