前提・実現したいこと
GPSモジュールを用いて緯度と経度の情報を、2秒ごとに取得し、それを記録したいと考えています。
発生している問題
しかし、print()で.txtで保存しようとしても、最初の緯度経度だけが保存されるだけでそれ以降の情報が追記されません。
該当のソースコード
Python3
1import serial 2import micropyGPS 3import threading 4import time 5import codecs 6 7gps = micropyGPS.MicropyGPS(9, 'dd') # MicroGPSオブジェクトを生成する。 8 # 引数はタイムゾーンの時差と出力フォーマット 9 10def rungps(): # GPSモジュールを読み、GPSオブジェクトを更新する 11 s = serial.Serial('/dev/serial0', 9600, timeout=10) 12 s.readline() # 最初の1行は中途半端なデーターが読めることがあるので、捨てる 13 while True: 14 sentence = s.readline().decode('utf-8') # GPSデーターを読み、文字列に変換する 15 if sentence[0] != '$': # 先頭が'$'でなければ捨てる 16 continue 17 for x in sentence: # 読んだ文字列を解析してGPSオブジェクトにデーターを追加、更新する 18 gps.update(x) 19 20gpsthread = threading.Thread(target=rungps, args=()) # 上の関数を実行するスレッドを生成 21gpsthread.daemon = True 22gpsthread.start() # スレッドを起動 23 24while True: 25 if gps.clean_sentences > 5: # ちゃんとしたデーターがある程度たまったら出力する 26 h = gps.timestamp[0] if gps.timestamp[0] < 24 else gps.timestamp[0] - 24 27 with open('gps.txt', "a")as f: 28 print((h, gps.timestamp[1], gps.timestamp[2]), file=codecs.open('gps.txt', 'w', 'utf-8')) 29 print((gps.latitude[0], gps.longitude[0]), file=codecs.open('gps.txt', 'w', 'utf-8')) 30 time.sleep(2.0) 31
補足情報(FW/ツールのバージョンなど)
Raspberry pi zero wで、Python3.5.3です。よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/13 21:42