Issues with PySerial: Port must be configured before it can be used

Question:

I am writing code (in python) to use serial communication with an Arduino, using the pySerial library, on Windows 7. However, I am having issues using the ports correctly. Here is my code:

import serial 

#sets the connection parameters, relook at when know more
ser = serial.Serial(
port ='COM4', 
baudrate = 9600, 
parity = serial.PARITY_ODD, 
stopbits = serial.STOPBITS_TWO, 
bytesize = serial.EIGHTBITS
)

ser = serial.Serial() 

ser.open()      #opens port 
ser.isOpen()    #returns true?

handStateList = [0]*3   #array to hold motor values in 
leftMotorState = 0
rightMotorState = 0
wristBend = 0

while True:
    #need to create options to send to arduino

    if wristBend == 'Left':
        leftMotorState = 127
        rightMotorState = 0
    elif wristBend == 'Right':
        leftMotorState = 0
        rightMotorState = 127
    else:
        leftMotorState = 0
        rightMotorState = 0

    #handStateList = ser.readline()

    handStateList[0] = leftMotorState
    handStateList[1] = rightMotorState
    handStateList[2] = 'n'


    ser.write(handStateList)

When I have ser.open() in the code, I get the traceback:

File "vibMotorTest1.py" line 16, in <module> 
ser.open()
File"C:Python34libsite-packagesserialserialwin32.py", line 44 in open
raise SerialException("Port must be configured before it can be used.")
serial.serialutil.SerialEception: Port must be configured before it can be used

When I have ser.open() commented out, I get the traceback:

File "vibMotorTest1.py", line 44, in <module>
  ser.write(HandStateList)
File"C:Python34libsite-packagesserialserialwin32.py", line 279, in write 
  if not self.hComPort: raise portNotOpenError
serial.serialutil.SerialException: Attempting to use a port that is not open

I am new to serial connections, and do not understand what is going wrong. By the examples I have found of code online, this code should work. Is anyone able to see where I am going wrong? A lot of the examples I have seen are for Apple or Linux, which use a different convention for naming USB’s, could that be part of the problem?

Thank you so much in advance!!

Asked By: rtmurad

||

Answers:

I guess that with the second ser = serial.Serial(), you are overwriting the serial port object that you created in the first few lines. You are replacing it with a new serial port object, which was created without giving it any parameters. Try commenting out that line.

Answered By: Bas Swinckels

If you want to use serial.Serial(), not serial.Serial(port =’COM4′, baudrate = 9600, parity = serial.PARITY_ODD, stopbits = serial.STOPBITS_TWO, bytesize = serial.EIGHTBITS ), Some configuration parameters must be defined before open(). Otherwise you will get the same error.

tty = serial.Serial()

tty.port = 'COM4'
tty.baudrate = 9600
tty.bytesize = serial.EIGHTBITS
tty.timeout = 1
tty.rtscts = True
......
......

try:
    tty.open()
except:
    ....
else:
    ....
Answered By: JaeMann Yeh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.