Skip to content

Commit

Permalink
Bugfix time parsing gps
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleshot committed Apr 27, 2024
1 parent c5a9d2c commit ba87ae7
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions sim7600x.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
class SIM7600X:
'''Class for the SIM7600X 4G module'''
def __init__(self, port: str = '/dev/ttyUSB2', baudrate: int = 115200, timeout: int = 5):
'''Initialize SIM7600X'''
'''Initialize SIM7600X.'''
try:
self.ser = serial.Serial(port, baudrate, timeout=timeout) # USB connection
self.ser.flushInput()
except Exception as e:
logging.error("Could not initialize SIM7600X: %s", str(e))

def send_at_command(self, command: str, back: str = 'OK', timeout: int = 1) -> str:
'''Send an AT command to SIM7600X'''
'''Send an AT command to SIM7600X.'''
rec_buff = ''
self.ser.write((command+'\r\n').encode())
sleep(timeout)
Expand All @@ -36,7 +36,7 @@ def send_at_command(self, command: str, back: str = 'OK', timeout: int = 1) -> s
# 31 -52 dBm or greater
# 99 not known or not detectable
def get_signal_quality(self) -> float:
'''Gets the current signal quality from the SIM7600G-H 4G module'''
'''Get the current signal quality of the 4G modem.'''
try:
signal_quality = self.send_at_command('AT+CSQ')
signal_quality = signal_quality[8:10]
Expand All @@ -50,14 +50,14 @@ def get_signal_quality(self) -> float:

@staticmethod
def decode_position(position: str, round_to: int = 5) -> float:
'''Decode the GPS position from the SIM7600G-H 4G module to a latitude or longitude value'''
'''Decode the GPS position to a latitude or longitude value.'''
position = position.split('.')
degrees = position[0][:-2]
minutes = position[0][-2:] + '.' + position[1]
return round(float(degrees) + float(minutes)/60, round_to)

def get_gps_position(self, max_attempts=7, delay=5):
'''Gets the current GPS position from the SIM7600G-H 4G module'''
'''Get the current GPS position and time.'''
current_attempt = 0

while current_attempt < max_attempts:
Expand Down Expand Up @@ -88,23 +88,24 @@ def get_gps_position(self, max_attempts=7, delay=5):
height = float(gps_data_cleaned[6])

date_str = gps_data_cleaned[4] + gps_data_cleaned[5]
date = datetime.strptime(date_str, ' %d%m%y %H%M%S.%f')
date = datetime.strptime(date_str, '%d%m%y%H%M%S.%f')

logging.info("GPS date: %s", date)
logging.info("GPS position: LAT %s, LON %s, HEIGHT %s", lat, lon, height)
return lat, lon, height, date

return None

def start_gps_session(self):
'''Starts a GPS session on the SIM7600G-H 4G module'''
'''Start the GPS session.'''
try:
logging.info("Starting GPS session.")
self.send_at_command('AT+CGPS=1,1')
except Exception as e:
logging.error("Could not start GPS session: %s", str(e))

def stop_gps_session(self):
'''Stops a GPS session on the SIM7600G-H 4G module'''
'''Stops the GPS session.'''
try:
logging.info("Stopping GPS session.")
self.send_at_command('AT+CGPS=0')
Expand Down

0 comments on commit ba87ae7

Please sign in to comment.