Denver Quin
Denver Quin

Reputation: 33

How can I get the variables in this code to update?

I'm wanting to be able to print and update values retrieved from a small GPS in real time. For whatever reason, the code will not update the GPS value, and only prints the same value until I restart the code. My guess is the variable is not able to update, but I wouldn't know how to fix that.

while ser.inWaiting() > 0:
decoded = (ser.read(1).decode)
echo += decoded()

class GPS:
    def __init__(self):
        #Write GPSinput
        out = ''
        ser.write(com.GPSstatus.encode())
        time.sleep(1)

        #Read output
        while ser.inWaiting() > 0:
            decoded = (ser.read(1).decode)
            out += decoded()

        strlen = len(str(out))
        substr = out[0:strlen-9]

        #GPS? information list
        variables = substr.splitlines()

        #Storing each output in a variable
        self.PULSE_SAWTOOTH = [int(s) for s in variables[1] if s.isdigit()]
        self.TRACKED_SATELLITES = [int(s) for s in variables[2] if s.isdigit()]
        self.VISIBLE_SATELLITES = [int(s) for s in variables[3] if s.isdigit()]

        self.LONGITUDE = variables[5]
        self.longlen = len(self.LONGITUDE)
        self.LONGDEG = self.LONGITUDE[0:self.longlen-7]
        self.LONGMIN = self.LONGITUDE[self.longlen-7:]

        self.LATITUDE = variables[6]
        self.latlen = len(self.LATITUDE)
        self.LATDEG = self.LATITUDE[0:self.latlen-7]
        self.LATMIN = self.LATITUDE[self.latlen-7:]

        self.HEIGHT = variables[7]
        self.KNOTS = variables[8]
        self.DEGREES = [9]

        self.GPS_STATUS = variables[10]
        self.TIMING_MODE = variables[17] 
        self.FIRMWARE_VERSION = variables[20]

if __name__ == "__main__":

    #Call the functions
    gps = GPS()
    for i in range(100):
        print(gps.LATITUDE)    
    #Enterable Parameters
    # PULSE_SAWTOOTH
    # TRACKED_SATELLITES 
    # VISIBLE_SATELLITES
    # LONGITUDE
    # longlen
    # LONGDEG
    # LONGMIN
    # LATITUDE
    # latlen
    # LATDEG
    # LATMIN
    # HEIGHT  
    # KNOTS
    # DEGREES 
    # GPS_STATUS 
    # TIMING_MODE 
    # FIRMWARE_VERSION

I'm guessing this is a pretty simple solution, but I'm a beginner and I don't fully understand whats happening enough to fix it. If anyone could shed some knowledge and help with my problem it would be greatly appreciated.

Upvotes: 3

Views: 110

Answers (1)

GKE
GKE

Reputation: 1000

Aside from the while loop indent error and possible decode error. You're getting the same output for print(gps.LATITUDE) every time because you never update it, either by the gps object, or a call to a member function of class GPS. Your initial gps object always has LATITUDE equal to the value variables[6] and if that variable isn't changing, neither will your LATITUDE variable. You have options, you can either update LATITUDE directly:

class GPS:
    def __init__(self , latitudeValue = 0):
        self.LATITUDE = latitudeValue

if __name__ == "__main__":
    gps = GPS()
    print(gps.LATITUDE) # Prints 0, default value of latitudeValue
    gps.LATITUDE = 5
    print(gps.LATITUDE) # Now prints 5, changed value of LATITUDE in gps object

Or you can make it part of a function call:

class GPS:
    def __init__(self , latitudeValue = 0):
        self.LATITUDE = latitudeValue
    def updateLatitude(self , newValue):
        self.LATITUDE = newValue

if __name__ == "__main__":
    gps = GPS()
    print(gps.LATITUDE) # Prints 0
    gps.updateLatitude(10) # Updates LATITUDE VALUE
    print(gps.LATITUDE) # Prints 10

You can use either method with whatever the value of variables[6] is. Likewise, you can use this with all member variables of your class.

For real-time updating you need to use a event handling function, this scenario possibly calls for a listener. Without knowing more about the device passing data through the bus. If the manufacturer of your device doesn't supply an API to work with, then while you can use a loop to constantly reinitialize an object, another approach is module reloading.

Upvotes: 1

Related Questions