-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps_serial.py
65 lines (51 loc) · 1.65 KB
/
gps_serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import serial
import time
import threading
import pigpio
pi=pigpio.pi()
pi.set_mode(6,pigpio.OUTPUT)
time.sleep(1.0)
s = serial.Serial('/dev/serial0', 115200, timeout=10)
pi.write(6,0)
s.readline()
def get_gps():
global t,lat,lon
try:
while True:
sentence = s.readline().decode('utf-8')
print(sentence)
#時間
h = int(sentence[7]+sentence[8]) + 9
if h > 23:
h - 24
t = str(h)+sentence[9]+sentence[10]+sentence[11]+sentence[12]+sentence[13]+sentence[14]+sentence[15]+sentence[16]
print(time)
#緯度経度
lat = float(sentence[20]+sentence[21]) + float(sentence[22]+sentence[23]+sentence[24]+sentence[25]+sentence[26]+sentence[27]+sentence[28])/60
lon = float(sentence[32]+sentence[33]+sentence[34]) + float(sentence[35]+sentence[36]+sentence[37]+sentence[38]+sentence[39]+sentence[40]+sentence[41])/60
if sentence[43] == 'W':
lon = 0.0 - lon
print(str(lat)+','+str(lon))
#速度
speed = float(sentence[45]+sentence[46]+sentence[47]+sentence[48]) * 1.852
print(str(speed))
#方位
if sentence[55] == ',':
dir = sentence[50]+sentence[51]+sentence[52]+sentence[53]+sentence[54]
else :
dir = sentence[50]+sentence[51]+sentence[52]+sentence[53]+sentence[54]+sentence[55]
print(dir)
except ValueError:
#s.write(b'ValueError\r\n')
get_gps()
gpsthread = threading.Thread(target=get_gps, args=()) # 上の関数を実行するスレッドを生成
gpsthread.daemon = True
gpsthread.start() # スレッドを起動
try:
while True:
time.sleep(1.0)
s.write(str(t).encode()+b','+str(lat).encode()+b','+str(lon).encode()+b'\r\n')
except KeyboardInterrupt:
s.close()
pass