diff --git a/README.md b/README.md index d3ce711..97a8a3c 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,5 @@ Uploaded only for the Raspberry Pi driver. ![L76X-GPS-Module-2.jpg](L76X-GPS-Module-2.jpg) +ttt + diff --git a/python/L76X.py b/python/L76X.py index f5768b3..359d9f3 100644 --- a/python/L76X.py +++ b/python/L76X.py @@ -24,8 +24,8 @@ class L76X(object): Status = 0 Lon_Baidu = 0.0 Lat_Baidu = 0.0 - Lon_Goodle = 0.0 - Lat_Goodle = 0.0 + Lon_Google = 0.0 + Lat_Google = 0.0 GPS_Lon = 0 GPS_Lat = 0 @@ -83,50 +83,52 @@ def __init__(self): def L76X_Send_Command(self, data): Check = ord(data[1]) + # print(Check) for i in range(2, len(data)): Check = Check ^ ord(data[i]) data = data + Temp[16] - data = data + Temp[(Check/16)] + # print(data) + data = data + Temp[Check//16] data = data + Temp[(Check%16)] - self.config.Uart_SendString(data) - self.config.Uart_SendByte('\r') - self.config.Uart_SendByte('\n') - print data + self.config.Uart_SendString(data.encode()) + self.config.Uart_SendByte('\r'.encode()) + self.config.Uart_SendByte('\n'.encode()) + # print(data) def L76X_Gat_GNRMC(self): data = self.config.Uart_ReceiveString(BUFFSIZE) - print data - print '\n' + # print(data) + # print('\n') add=0 self.Status = 0 for i in range(0, BUFFSIZE-71): - if(ord(data[add]) == 36 and ord(data[add+1]) == 71 and (ord(data[add+2]) == 78 \ - or ord(data[add+2]) == 80) and ord(data[add+3]) == 82 and ord(data[add+4]) == 77\ - and ord(data[add+5]) == 67): + if(data[add]) == 36 and data[add+1] == 71 and (data[add+2] == 78 \ + or data[add+2] == 80) and data[add+3] == 82 and data[add+4] == 77\ + and data[add+5] == 67: x = 0 z = 0 while(x < 12): if(add+z >= BUFFSIZE-1): return - if(ord(data[add+z]) == 44):#, + if(data[add+z]) == 44:#, x = x + 1 if(x == 1): Time = 0 for k in range(0, BUFFSIZE-1): if(add+z+k >= BUFFSIZE-1): return - if(ord(data[add+z+k+1]) == 44):#, + if(data[add+z+k+1]) == 44:#, break - if(ord(data[add+z+k+1]) == 46):#. + if(data[add+z+k+1]) == 46:#. break - Time = (ord(data[add+z+k+1]) - 48) + Time*10 + Time = (data[add+z+k+1]) - 48 + Time*10 self.Time_H = Time/10000 + 8 self.Time_M = Time/100%100 self.Time_S = Time%100 if(self.Time_H >= 24): self.Time_H = self.Time_H - 24 elif(x == 2): - if(ord(data[add+z+1]) == 65):#A + if(data[add+z+1]) == 65:#A self.Status = 1 else: self.Status = 0 @@ -135,11 +137,11 @@ def L76X_Gat_GNRMC(self): for k in range(0, BUFFSIZE-1): if(add+z+k >= BUFFSIZE-1): return - if(ord(data[add+z+k+1]) == 44):#, + if(data[add+z+k+1]) == 44:#, break - if(ord(data[add+z+k+1]) == 46):#. + if(data[add+z+k+1]) == 46:#. continue - latitude = (ord(data[add+z+k+1]) - 48) + latitude*10 + latitude = (data[add+z+k+1]) - 48 + latitude*10 self.Lat = latitude / 1000000.0 elif(x == 4): self.Lat_area = data[add+z+1] @@ -148,11 +150,11 @@ def L76X_Gat_GNRMC(self): for k in range(0, BUFFSIZE-1): if(add+z+k >= BUFFSIZE-1): return - if(ord(data[add+z+k+1]) == 44):#, + if(data[add+z+k+1]) == 44:#, break - if(ord(data[add+z+k+1]) == 46):#. + if(data[add+z+k+1]) == 46:#. continue - longitude = (ord(data[add+z+k+1]) - 48) + longitude*10 + longitude = (data[add+z+k+1]) - 48 + longitude*10 self.Lon = longitude / 1000000.0 elif(x == 6): @@ -176,8 +178,8 @@ def transformLon(self, x, y): return ret def bd_encrypt(self): - x = self.Lon_Goodle - y = self.Lat_Goodle + x = self.Lon_Google + y = self.Lat_Google z = math.sqrt(x * x + y * y) + 0.00002 * math.sin(y * x_pi) theta = math.atan2(y, x) + 0.000003 * math.cos(x * x_pi) self.Lon_Baidu = z * math.cos(theta) + 0.0065 @@ -192,8 +194,8 @@ def transform(self): math.sqrtMagic = math.sqrt(magic) dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * math.sqrtMagic) * pi) dLon = (dLon * 180.0) / (a / math.sqrtMagic * math.cos(radLat) * pi) - self.Lat_Goodle = self.GPS_Lat + dLat - self.Lon_Goodle = self.GPS_Lon + dLon + self.Lat_Google = self.GPS_Lat + dLat - 0.00003 + self.Lon_Google = self.GPS_Lon + dLon - .00293 def L76X_Baidu_Coordinates(self, U_Lat, U_Lon): self.GPS_Lat = U_Lat % 1 *100 / 60 + math.floor(U_Lat) diff --git a/python/main.py b/python/main.py index eff4d50..01b2f99 100644 --- a/python/main.py +++ b/python/main.py @@ -1,36 +1,45 @@ import L76X import time import math +import traceback +import redis_class try: + redis = redis_class.RedisClient(host='192.168.0.150', port=6379, password="Redis2019!") x=L76X.L76X() x.L76X_Set_Baudrate(9600) x.L76X_Send_Command(x.SET_NMEA_BAUDRATE_115200) time.sleep(2) x.L76X_Set_Baudrate(115200) - x.L76X_Send_Command(x.SET_POS_FIX_400MS); + x.L76X_Send_Command(x.SET_POS_FIX_100MS) #Set output message - x.L76X_Send_Command(x.SET_NMEA_OUTPUT); + x.L76X_Send_Command(x.SET_NMEA_OUTPUT) - x.L76X_Exit_BackupMode(); + x.L76X_Exit_BackupMode() while(1): x.L76X_Gat_GNRMC() if(x.Status == 1): - print 'Already positioned' + redis.set('sat_use', 6) else: - print 'No positioning' - print 'Time %d:'%x.Time_H, - print '%d:'%x.Time_M, - print '%d'%x.Time_S - - print 'Lon = %f'%x.Lon, - print ' Lat = %f'%x.Lat + redis.set('sat_use', 0) + # print('Time %d:'%x.Time_H,) + # print('%d:'%x.Time_M,) + # print('%d'%x.Time_S) + # print(int(time.time() * 1000)) + # print('Lon = %f'%x.Lon,) + # print(' Lat = %f'%x.Lat) + x.L76X_Google_Coordinates(x.Lat, x.Lon) x.L76X_Baidu_Coordinates(x.Lat, x.Lon) - print 'Baidu coordinate %f'%x.Lat_Baidu, - print ',%f'%x.Lon_Baidu -except: + print(x.Lat_Google, -x.Lon_Google) + redis.set('gps_lng', -x.Lon_Google) + redis.set('gps_lat', x.Lat_Google) + # print(type(x.Lon)) + # print('Baidu coordinate %f'%x.Lat_Baidu,) + # print(',%f'%x.Lon_Baidu) +except Exception as e: + print(traceback.format_exc()) #GPIO.cleanup() - print "\nProgram end" + print("\nProgram end") exit() \ No newline at end of file diff --git a/python/redis_class.py b/python/redis_class.py new file mode 100644 index 0000000..08b5ed8 --- /dev/null +++ b/python/redis_class.py @@ -0,0 +1,72 @@ +import redis +import datetime +import json + +class RedisClient(object): + def __init__(self, host, port, password, database=0): + self.db = redis.Redis(host=host, port=port, password=password, db=database) + self.host = host + self.port = port + self.password = password + + def get(self, key, start_index=0, end_index=0): + try: + value = self.db.get(key).decode() + if value.isalpha(): + pass + elif "." in value and not ":" in value: + value = float(value) + elif value.count("-") > 1 and ":" in value: + value = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S.%f").timestamp() + elif value.count("-") >= 3: + pass + elif value.count("-") == 1 and value[1:].isnumeric(): + value = int(value) + elif value.isnumeric(): + value = int(value) + else: + pass + except Exception as e: + decoded_list = [] + value = self.db.lrange(key, start_index, end_index) + for i in range(len(value)): + if '{' in value[i].decode(): + value[i] = json.loads(value[i].decode()) + decoded_list.append(value[i]) + else: + decoded_list.append(value[i].decode()) + value = decoded_list + return value + + def get_all_keys(self): + keys = self.db.keys() + for i in range(len(keys)): + keys[i] = keys[i].decode() + return keys + + def get_all_keys_and_values(self): + key_value_dict = {} + keys = self.get_all_keys() + for i in range(len(keys)): + value = self.get(keys[i]) + key_value_dict[keys[i]] = value + return key_value_dict + + def set(self, key, value): + self.db.set(key, value) + + def set_multiple(self, key_value_dict): + pipe = self.db.pipeline() + for key, value in key_value_dict.items(): + pipe.set(key, value) + pipe.execute() + + def delete(self, key): + self.db.delete(key) + + def exists(self, key): + return self.db.exists(key) + +if __name__ == '__main__': + redis = RedisClient(host='192.168.0.150', port=6379, password="Redis2019!") + print(redis.get_all_keys_and_values()) \ No newline at end of file