Python Forum

Full Version: Python Errors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Those are three different, unrelated errors. You couldn't possibly get all of them all at once.
The first is saying that you're setting a value to ser before declaring it as a global variable. The easy fix to that is to declare it as a global variable before setting a value to it. The better fix is to not use global variables.
Got rid of Global so now only 1 error. Had a look at the 2 links you sent, they seemed very general but will take a detailed look.
Code:
def init_serial():
print "Found Ports:"
for n,s in scan():
  print "%s" % s
print " "
COMNUM = '5' #set you COM port # here
#must be declared in each fxn used
ser = serial.Serial()
ser.baudrate = 4800
ser.port = COMNUM -1#starts at 0, so subtract 1
Error:
Error:
TypeErrorTraceback (most recent call last) C:\Users\Dell\Downloads\lat_lon1.py in <module>()     27 ser = serial.Serial()     28 ser.baudrate = 4800 ---> 29 ser.port = COMNUM -1#starts at 0, so subtract 1     30     31 ser.timeout = 1 #you must specify a timeout (in seconds) so that the serial port doesn't hang TypeError: unsupported operand type(s) for -: 'str' and 'int'
(May-22-2017, 02:59 PM)kendias Wrote: [ -> ]TypeError: unsupported operand type(s) for -: 'str' and 'int'
COMNUM is probably a string, right? Change it so it isn't, otherwise doing math doesn't make sense.
Thanks, will try it.
Surprised that program I am using is from a link I got and the author gave examples of the output and showed it worked. Hence, am a bit nervous @ changes that in the end may not make the program work. The link to the program (with a writeup) is:


https://helentronica.com/2014/05/20/gps-...ll-in-one/
But your code isn't the same as in the article. For COMNUM, for example, your code looks like this:
COMNUM = '5' #set you COM port # here
In the article, it looks like this:
COMNUM = 5  # Set you COM port # here (Check it in device manager)
You added quotes, which makes it a string instead of a number, which causes the -1 to fail.
How to add code tags:

Read BBCODE
Thanks.
Tried after removing quotes but still get error:
ValueErrorTraceback (most recent call last)
C:\Users\Dell\Downloads\lat_lon1.py in <module>()
    27 ser = serial.Serial()
    28 ser.baudrate = 4800
---> 29 ser.port = COMNUM -1#starts at 0, so subtract 1
    30
    31 ser.timeout = 1 #you must specify a timeout (in seconds) so that the serial port doesn't hang
C:\Users\Dell\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\serial\serialutil.pyc in port(self, port)
   262         """
   263         if port is not None and not isinstance(port, basestring):
--> 264             raise ValueError('"port" must be None or a string, not {}'.format(type(port)))
   265         was_open = self.is_open
   266         if was_open:
ValueError: "port" must be None or a string, not <type 'int'>
Thanks, got it now. Will post accordingly. Really enjoy this forum.
Please help! Reposted previous post in code tags.

27 ser = serial.Serial()
   28 ser.baudrate = 4800
---> 29 ser.port = COMNUM -1#starts at 0, so subtract 1
   30 
   31 ser.timeout = 1 #you must specify a timeout (in seconds) so that the serial port doesn't hang
C:\Users\Dell\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\serial\serialutil.pyc in port(self, port)
  262         """
  263         if port is not None and not isinstance(port, basestring):
--> 264             raise ValueError('"port" must be None or a string, not {}'.format(type(port)))
  265         was_open = self.is_open
  266         if was_open:
ValueError: "port" must be None or a string, not <type 'int'>
(May-23-2017, 01:53 PM)kendias Wrote: [ -> ]
Error:
ValueError: "port" must be None or a string, not <type 'int'>

I believe in you, you can figure it out :)
You only set the port in one place, so it's nice and easy to find.
Pages: 1 2 3