1

For all this, I am using Windows 7, Python 2.7.3 and Pyserial 2.6. I am using COM6 for my Arduino.

I am trying to send data from a Python program, to the Arduino to read, and it keeps returning a "SerialException error.

Here is the Python code:

import serial
import time
ser = serial.Serial('COM6', 9600, timeout=0)
var = raw_input("Enter something: ")
ser.write(var)
while 1:
    try:
        print ser.readline()
        time.sleep(1)
    except ser.SerialTimeoutException:
        print('Data could not be read')

Here is the Arduino code:

int incomingByte = 0;

void setup(){
// Open serial connection.
Serial.begin(9600);

}

void loop(){
if (Serial.available() > 0) {
 // read the incoming byte:
 incomingByte = Serial.read();

 // say what you got:
 Serial.print("I got: "); // ASCII printable characters
 Serial.println(incomingByte, DEC);
}

}

Here is the error I am getting in Python when I run the script:

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/test", line 3, in <module>
    ser = serial.Serial('COM6', 9600, timeout=0)
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 34, in __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\serial\serialutil.py", line 261, in __init__
    self.open()
  File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 62, in open
    raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
SerialException: could not open port COM6: [Error 5] Access is denied.

How can I resolve that error? Python will read from the Arduino just fine, but not write to it.

3
  • A few things to check: * The port is not open by other program * The port is not open by a python process lurking in the background (check task manager) * If you need UAC permissions to access the serial port Commented May 11, 2016 at 17:02
  • You should close the serial connection at the end of your program, using ser.close(). You may have to restart you computer or kill background running python instances to release the port. Commented May 11, 2016 at 17:05
  • Thank you for that! That solved the issue. Commented May 13, 2016 at 4:03

1 Answer 1

1

Besides ensuring that the port is closed, a couple of additional things to note...

  • Data sent using PySerial can only be of type byte (or bytestream for more than 1 byte data). You can't send user input as it is.

  • flush() the data after writing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.