python - Issues with PySerial: Port must be configured before it can be used -
i writing code (in python) use serial communication arduino, using pyserial library, on windows 7. however, having issues using ports correctly. here code:
import serial #sets connection parameters, relook @ 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 hold motor values in leftmotorstate = 0 rightmotorstate = 0 wristbend = 0 while true: #need create options send 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 have ser.open() in code, traceback:
file "vibmotortest1.py" line 16, in <module> ser.open() file"c:\python34\lib\site-packages\serial\serialwin32.py", line 44 in open raise serialexception("port must configured before can used.") serial.serialutil.serialeception: port must configured before can used when have ser.open() commented out, traceback:
file "vibmotortest1.py", line 44, in <module> ser.write(handstatelist) file"c:\python34\lib\site-packages\serial\serialwin32.py", line 279, in write if not self.hcomport: raise portnotopenerror serial.serialutil.serialexception: attempting use port not open i new serial connections, , not understand going wrong. examples have found of code online, code should work. able see going wrong? lot of examples have seen apple or linux, use different convention naming usb's, part of problem?
thank in advance!!
i guess second ser = serial.serial(), overwriting serial port object created in first few lines. replacing new serial port object, created without giving parameters. try commenting out line.
Comments
Post a Comment