Link to home
Start Free TrialLog in
Avatar of Dolamite Jenkins
Dolamite JenkinsFlag for United States of America

asked on

ValueError: "port" must be None or a string, not <type 'int'>

ValueError: "port" must be None or a string, not <type 'int'>

Open in new window


keep getting this error when I run my python 2.7  code to find and connect with my port which I have a gps tracker connected ... I'm using 2.7 ... I figured once I get this working I can translate it into python3..

this is the error .. as I try is tell my laptop what port my GPS trackers in connected to
  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'>

Open in new window


this code I am starting with
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

Open in new window


thanks for anyone that can guide me
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing to do with the question. Norie should get full points.

If you want to have code, that runs with python2 and python3, you could install the future package and call program futurize to translate your python file to a version, that executes with python2 and python3 (but it requires the package future installed for execution).

Sometimes the result of futurize is not correct, but it does a rather good job.


so
pip install future
futurize yourscript.py

Open in new window

Avatar of Dolamite Jenkins

ASKER

thank you both