01-24-2020, 07:48 AM
Shure, here is python script that I"m using in combination with "BU-353-S4" usb gps dongle.
and yes, It gives me "incorrect sytax" error
#!/usr/bin/python
import serial
import os
from decimal import *
getcontext().prec = 4
# enter your time zone offset
TimeOffset = +2
gpsfound = 2
settime = 0
count = 120
if os.path.exists('/dev/ttyUSB0') == True:
gpsfound = 0
ser = serial.Serial('/dev/ttyUSB0',4800,timeout = 10)
else:
print "No GPS RX found on USB0"
if os.path.exists('/dev/ttyUSB1') == True and gpsfound == 2:
gpsfound = 1
ser = serial.Serial('/dev/ttyUSB1',4800,timeout = 10)
else:
if gpsfound == 2:
print "No GPS RX found on USB1"
if gpsfound < 2:
Months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
MDays = [31,28,31,30,31,30,31,31,30,31,30,31]
Days = ['Thu','Fri','Sat','Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun','Mon','Tue','Wed']
count1 = 1
locked = 0
found = 0
while locked == 0 and count1 <= count:
gps = ser.readline()
if gps[1 : 6] == "GPGGA":
if found == 0:
print "GPS RX found on USB" + str(gpsfound)
print "Waiting for lock...."
found = 1
if int(gps[43]) > 0 and gps[42] != "*":
locked = 1
count1 +=1
if count1 < 120:
print "Locked"
else:
print "Signal Unlocked, aborting time setting"
count2 = 1
cday = 0
Leap = 0
while settime == 0 and count1 <= count and count2 <= count:
gps = ser.readline()
if gps[1 : 6] == "GPRMC":
gps2 = gps.split(',',12)
fulldate = gps2[9]
fulltime = gps2[1]
clock = ":" + fulltime[2:4] + ":" + fulltime[4:6]
Hour = int(fulltime[0:2])
Hour = Hour + TimeOffset
if Hour > 23:
Hour = Hour - 24
cday = 1
if Hour < 0:
Hour = Hour + 24
cday = -1
if Hour < 10:
Hour = "0" + str(Hour)
else:
Hour = str(Hour)
Year = int(fulldate[4:6])
Mon = int(fulldate[2:4])
Day = int(fulldate[0:2])
if cday == 1:
Day = Day + 1
if Day > MDays[Mon-1]:
Mon = Mon + 1
Day = 1
if Mon > 12:
Year = Year + 1
Mon = 1
if cday == -1:
Day = Day - 1
if Day < 1:
Mon = Mon - 1
Day = MDays[Mon-1]
if Mon < 1:
Year = Year - 1
Mon = 12
Day = MDays[Mon-1]
Month = Months[Mon-1]
Day = str(Day)
Years = "20" + str(Year)
x = 0
YDays = 0
while x < Mon - 1:
YDays = YDays + MDays[x]
if (2000 + Year)/4 == (2000+ Decimal(Year))/4 and x == 1:
YDays = YDays + 1
Leap = 1
x +=1
YDays = YDays + int(Day)
Weekday = (YDays - ((YDays/7)*7))+(Year - 16) + int(Decimal(0.5) + (Decimal(Year)-Decimal(15))/Decimal(4))
path = "sudo date -s '" + Days[Weekday] + " " + Month + " " + str(Day) + " " + Hour + clock + " " + Years + "'"
print path
os.system (path)
settime = 1
count2 +=1
if settime == 1:
print "Time set with Time Offset to UTC " + str(TimeOffset) + " Hour(s)"
else:
print "Unable to set time"
and yes, It gives me "incorrect sytax" error