Python Forum

Full Version: Python in Linux environment on RPI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Thanks.
I put this in the script as below and ran but still get nothing printed to the file and it does not open the file locations.csv :
from gps import *
import time
import threading
def run(self):
      global gpsd
      while gpsp.running:
          with open("locations.csv","w") as f:
              gpsd.next()
              f.write('{}, {}\n'.format(gpsd.fix.longitude, gpsd.fix.latitude))
              # print (gpsd.fix.longitude)
              # print (gpsd.fix.latitude)
if you remove comments from the print statements do you see any output?
perhaps you are not receiving any data from your gps.
Thanks.
No output after removing comments from script in my previous post but do get output from script below. It prints lat, lon but into the python shell i.e. print works but  f.write does not work.

from gps import *
import time
import threading
f = open("locations.csv","w")
gpsd = None
class GpsPoller(threading.Thread):
  def __init__(self):
      threading.Thread.__init__(self)
      global gpsd
      gpsd=gps(mode=WATCH_ENABLE)
      self.current_value = None
      self.running = True
            
  def run(self):
      global gpsd
      while gpsp.running:
          gpsd.next()
          print (gpsd.fix.longitude)
          print (gpsd.fix.latitude)
        
if  __name__ == '__main__':
  
  gpsp=GpsPoller()
  try:
      gpsp.start()
      
      while True:
          f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
        
          time.sleep(30)
  except(KeyboardInterrupt,SystemExit):
      f.close()
      gpsp.running = False
      gpsp.join()
Did you leave the __init__ on the first attempt (with code I showed you)?
It is of course necessary to still initialize
Thanks.
I tried the foll. and no errors but no printing either:
from gps import *
import time
import threading
f = open("locations10.csv","w")
gpsd = None
def __init__(self):
     threading.Thread.__init__(self)
     global gpsd
     gpsd=gps(mode=WATCH_ENABLE)
     self.current_value = None
     self.running = True
class GpsPoller(threading.Thread):
 def run(self):
      global gpsd
      while gpsp.running:
          with open("locations10.csv","w") as f:
              gpsd.next()
              f.write('{}, {}\n'.format(gpsd.fix.longitude, gpsd.fix.latitude))
              print (gpsd.fix.longitude)
              print (gpsd.fix.latitude)
And you do get data printed out if you comment out the file stuff? (and adjust indentation)
yes, only with this script below:
from gps import *
import time
import threading
f = open("locations.csv","w")
gpsd = None
class GpsPoller(threading.Thread):
 def __init__(self):
     threading.Thread.__init__(self)
     global gpsd
     gpsd=gps(mode=WATCH_ENABLE)
     self.current_value = None
     self.running = True
            
 def run(self):
     global gpsd
     while gpsp.running:
         gpsd.next()
         print (gpsd.fix.longitude)
         print (gpsd.fix.latitude)
        
if  __name__ == '__main__':
  
 gpsp=GpsPoller()
 try:
     gpsp.start()
      
     while True:
         f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
        
         time.sleep(30)
 except(KeyboardInterrupt,SystemExit):
     f.close()
     gpsp.running = False
     gpsp.join()
Well, there is one more thing you can try now.
that is to flush the file after write.

        #immediately after 
        f.write('{}, {}\n'.format(gpsd.fix.longitude, gpsd.fix.latitude))
        # add
        f.flush()
If that doesn't work, I guess you can cache to a list, and write after you have enough samples
While working on and testing GPS code I always have a Terminal window open and type one of the following commands so I can verify my GPS is working and receiving data, if you think your signal is intermittent you can scroll back and look for breaks in the GPS data stream.

Type cgps -s in Terminal use Ctrl + Z too stop

Type gpsmon In Terminal use Ctrl + Z too stop
K_Research -- Please note that you are responding to a three+ year old thread,and doubtful OP will see, hasn't logged in since last January.
Pages: 1 2 3