Modem communication working

This commit is contained in:
Giulio 2019-07-17 16:31:12 +02:00
parent dd08dfd93d
commit 3b88286b6b

View File

@ -6,9 +6,11 @@ logging.basicConfig(level=logging.DEBUG)
class Gsm: class Gsm:
def __init__(self, port, msisdn, imei=None, pin=None): def __init__(self, port, msisdn, imei=None, pin=None):
self.port = port self.port = port
logging.info('Initializing Gsm cass for port ' + port) logging.info('Initializing Gsm class for port ' + port)
try: 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: except:
logging.error('Error connecting to ' + self.port) logging.error('Error connecting to ' + self.port)
self.msisdn = msisdn self.msisdn = msisdn
@ -26,8 +28,9 @@ class Gsm:
def cmd(self, cmd): def cmd(self, cmd):
cmd += "\r\n" cmd += "\r\n"
self.console.write(cmd.encode("ascii")) self.console.write(cmd.encode("ascii"))
time.sleep(0.5) time.sleep(1)
return list(map(lambda elem: elem.decode("ascii", errors="replace"), self.console.readlines())) output = self.console.readlines()
return ''.join(l.decode('ascii') for l in output)
def check_status(self): def check_status(self):
r = self.cmd("ATI") r = self.cmd("ATI")
@ -38,9 +41,13 @@ class Gsm:
def unlock_sim(self, pin): def unlock_sim(self, pin):
status = self.cmd("AT+CPIN?") status = self.cmd("AT+CPIN?")
logging.info(status)
if status == "+CPIN:READY":
if "+CPIN:READY" in status:
return True return True
elif "+CME ERROR:10" in status:
logging.error("Sim not inserted")
return False
elif "+CPIN:SIM PIN" in status: elif "+CPIN:SIM PIN" in status:
auth = self.cmd('AT+CPIN="' + pin + '"') auth = self.cmd('AT+CPIN="' + pin + '"')
if "OK" in auth: if "OK" in auth: