From 3b88286b6b0f8a855fc2c3478f11e198e5118849 Mon Sep 17 00:00:00 2001 From: Giulio Date: Wed, 17 Jul 2019 16:31:12 +0200 Subject: [PATCH] Modem communication working --- agent/gsm/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/agent/gsm/__init__.py b/agent/gsm/__init__.py index 6d57c1c..859ead7 100644 --- a/agent/gsm/__init__.py +++ b/agent/gsm/__init__.py @@ -6,9 +6,11 @@ logging.basicConfig(level=logging.DEBUG) class Gsm: def __init__(self, port, msisdn, imei=None, pin=None): self.port = port - logging.info('Initializing Gsm cass for port ' + port) + logging.info('Initializing Gsm class for port ' + port) try: - self.console = serial.Serial(self.port, 112500, timeout=3) + self.console = serial.Serial(self.port, 115200, timeout=3) + if not self.console.isOpen(): + self.console.open() except: logging.error('Error connecting to ' + self.port) self.msisdn = msisdn @@ -26,8 +28,9 @@ class Gsm: def cmd(self, cmd): cmd += "\r\n" self.console.write(cmd.encode("ascii")) - time.sleep(0.5) - return list(map(lambda elem: elem.decode("ascii", errors="replace"), self.console.readlines())) + time.sleep(1) + output = self.console.readlines() + return ''.join(l.decode('ascii') for l in output) def check_status(self): r = self.cmd("ATI") @@ -38,9 +41,13 @@ class Gsm: def unlock_sim(self, pin): status = self.cmd("AT+CPIN?") - - if status == "+CPIN:READY": + logging.info(status) + + if "+CPIN:READY" in status: return True + elif "+CME ERROR:10" in status: + logging.error("Sim not inserted") + return False elif "+CPIN:SIM PIN" in status: auth = self.cmd('AT+CPIN="' + pin + '"') if "OK" in auth: