import serial from helper_functions import extract_numbers # Fetch operational status def get_modem_status(port="/dev/ttyUSB0", baudrate=115200, dictionary={}): try: ser = serial.Serial(port, baudrate, timeout=1) ser.write(b"AT!GSTATUS?\r") response = ser.readlines() ser.close() full_decoded = "" for item in response: try: decoded_item = item.decode("utf-8") full_decoded = full_decoded + "\n" + decoded_item except Exception as e: dictionary["status encoded"] = str(item) print(f"Could not decode GSTATUS item:\t{str(item)}\n{e}") dictionary["Status"] = full_decoded return dictionary except Exception as e: return {"GSTATUS error": f"{e}"} # Fetch NR (5G) information of the device def get_modem_nr_info(port="/dev/ttyUSB0", baudrate=115200, dictionary={}): try: ser = serial.Serial(port, baudrate, timeout=1) ser.write(b"AT!NRINFO?\r") response = ser.readlines() ser.close() full_decoded = "" for item in response: try: decoded_item = item.decode("utf-8") full_decoded = full_decoded + "\n" + decoded_item if "NR5G RSRP (dBm):" in decoded_item: numbers = extract_numbers(decoded_item) if len(numbers) >= 4: dictionary["RSRP"] = numbers[1] dictionary["RSRQ"] = numbers[3] elif "NR5G SINR (dB):" in decoded_item: numbers = extract_numbers(decoded_item) if len(numbers) >= 2: dictionary["SINR"] = numbers[1] except Exception as e: dictionary["NR info encoded"] = str(item) print(f"Could not decode NRINFO item:\t{str(item)}\n{e}") dictionary["NR Info"] = full_decoded return dictionary except Exception as e: return {"NRINFO error": f"{e}"}