ValueError: "port" must be None or a string, not <type 'int'>
File "C:\Users\clayr\Anaconda3\envs\maps.py\lib\site-packages\serial\serialutil.py", line 264, in port
raise ValueError('"port" must be None or a string, not {}'.format(type(port)))
ValueError: "port" must be None or a string, not <type 'int'>
import serial
#####Global Variables######################################
#be sure to declare the variable as 'global var' in the fxn
ser = 0
#####FUNCTIONS#############################################
#initialize serial connection
def init_serial():
COMNUM = 3 #set you COM port # here
global ser #must be declared in each fxn used
ser = serial.Serial()
ser.baudrate = 9600
ser.port = COMNUM - 1 #starts at 0, so subtract 1
#ser.port = '/dev/ttyUSB0' #uncomment for linux
#you must specify a timeout (in seconds) so that the
# serial port doesn't hang
ser.timeout = 1
ser.open() #open the serial port
# print port open or closed
if ser.isOpen():
print 'Open: ' + ser.portstr
#####SETUP################################################
#this is a good spot to run your initializations
init_serial()
#####MAIN LOOP############################################
while 1:
#prints what is sent in on the serial port
temp = raw_input('Type what you want to send, hit enter:\n\r')
ser.write(temp) #write to the serial port
bytes = ser.readline() #reads in bytes followed by a newline
print 'You sent: ' + bytes #print to the console
break #jump out of loop
#hit ctr-c to close python window
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.