From 9625cb8fd666c90b15b129bbd809e3f11d40ea51 Mon Sep 17 00:00:00 2001 From: Alejandro Madariaga Angeles Date: Sat, 16 Jan 2016 01:17:12 -0600 Subject: [PATCH] added "waittingForModemToStartInSeconds" argument to GsmModem.conect(), added AT_CNMI property to GsmModem. The waittingForModemToStartInSeconds arg allows to wait after the serial connection has started, and before sending the AT commands. The AT_CNMI proprety of the GsmModem class allow to specify the CNMI parameter of the gsm modem, this helps to set up the incoming sms notifications. --- gsmmodem/modem.py | 17 ++++++++++++----- tools/identify-modem.py | 6 ++++-- tools/sendsms.py | 14 +++++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/gsmmodem/modem.py b/gsmmodem/modem.py index e468f34..707d4aa 100644 --- a/gsmmodem/modem.py +++ b/gsmmodem/modem.py @@ -4,6 +4,7 @@ import sys, re, logging, weakref, time, threading, abc, codecs from datetime import datetime +from time import sleep from .serial_comms import SerialComms from .exceptions import CommandError, InvalidStateException, CmeError, CmsError, InterruptedException, TimeoutException, PinRequiredError, IncorrectPinError, SmscNumberUnknownError @@ -130,11 +131,12 @@ class GsmModem(SerialComms): # Used for parsing SMS status reports CDSI_REGEX = re.compile(r'\+CDSI:\s*"([^"]+)",(\d+)$') - def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallback=None): + def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsReceivedCallbackFunc=None, smsStatusReportCallback=None, AT_CNMI=""): super(GsmModem, self).__init__(port, baudrate, notifyCallbackFunc=self._handleModemNotification) self.incomingCallCallback = incomingCallCallbackFunc or self._placeholderCallback self.smsReceivedCallback = smsReceivedCallbackFunc or self._placeholderCallback self.smsStatusReportCallback = smsStatusReportCallback or self._placeholderCallback + self.AT_CNMI = AT_CNMI or "2,1,0,2" # Flag indicating whether caller ID for incoming call notification has been set up self._callingLineIdentification = False # Flag indicating whether incoming call notifications have extended information @@ -161,7 +163,7 @@ def __init__(self, port, baudrate=115200, incomingCallCallbackFunc=None, smsRece self._smsMemWrite = None # Preferred message storage memory for writes ( parameter used for +CPMS) self._smsReadSupported = True # Whether or not reading SMS messages is supported via AT commands - def connect(self, pin=None): + def connect(self, pin=None, waitingForModemToStartInSeconds=0): """ Opens the port and initializes the modem and SIM card :param pin: The SIM card PIN code, if any @@ -170,8 +172,13 @@ def connect(self, pin=None): :raise PinRequiredError: if the SIM card requires a PIN but none was provided :raise IncorrectPinError: if the specified PIN is incorrect """ - self.log.info('Connecting to modem on port %s at %dbps', self.port, self.baudrate) + + self.log.info('Connecting to modem on port %s at %dbps', self.port, self.baudrate) super(GsmModem, self).connect() + + if waitingForModemToStartInSeconds > 0: + sleep(waitingForModemToStartInSeconds) + # Send some initialization commands to the modem try: self.write('ATZ') # reset configuration @@ -338,7 +345,7 @@ def connect(self, pin=None): if self._smsReadSupported: try: - self.write('AT+CNMI=2,1,0,2') # Set message notifications + self.write('AT+CNMI=' + self.AT_CNMI) # Set message notifications except CommandError: # Message notifications not supported self._smsReadSupported = False @@ -384,7 +391,7 @@ def _unlockSim(self, pin): else: raise PinRequiredError('AT+CPIN') - def write(self, data, waitForResponse=True, timeout=5, parseError=True, writeTerm='\r', expectedResponseTermSeq=None): + def write(self, data, waitForResponse=True, timeout=10, parseError=True, writeTerm='\r', expectedResponseTermSeq=None): """ Write data to the modem. This method adds the ``\\r\\n`` end-of-line sequence to the data parameter, and diff --git a/tools/identify-modem.py b/tools/identify-modem.py index b56bdf9..1a2167d 100755 --- a/tools/identify-modem.py +++ b/tools/identify-modem.py @@ -21,7 +21,8 @@ def parseArgs(): parser.add_argument('port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.') parser.add_argument('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate') parser.add_argument('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN') - parser.add_argument('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)') + parser.add_argument('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)') + parser.add_argument('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds') return parser.parse_args() def parseArgsPy26(): @@ -32,6 +33,7 @@ def parseArgsPy26(): parser.add_option('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate') parser.add_option('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN') parser.add_option('-d', '--debug', action='store_true', help='dump modem debug information (for python-gsmmodem development)') + parser.add_option('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds') options, args = parser.parse_args() if len(args) != 1: parser.error('Incorrect number of arguments - please specify a PORT to connect to, e.g. {0} /dev/ttyUSB0'.format(sys.argv[0])) @@ -46,7 +48,7 @@ def main(): print('Connecting to GSM modem on {0}...'.format(args.port)) try: - modem.connect(args.pin) + modem.connect(args.pin, waitingForModemToStartInSeconds=args.wait) except PinRequiredError: sys.stderr.write('Error: SIM card PIN required. Please specify a PIN with the -p argument.\n') sys.exit(1) diff --git a/tools/sendsms.py b/tools/sendsms.py index 31c42bd..e519a12 100755 --- a/tools/sendsms.py +++ b/tools/sendsms.py @@ -19,8 +19,10 @@ def parseArgs(): parser.add_argument('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.') parser.add_argument('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate') parser.add_argument('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN') - parser.add_argument('-d', '--deliver', action='store_true', help='wait for SMS delivery report') - parser.add_argument('destination', metavar='DESTINATION', help='destination mobile number') + parser.add_argument('-d', '--deliver', action='store_true', help='wait for SMS delivery report') + parser.add_argument('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds') + parser.add_argument('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications') + parser.add_argument('destination', metavar='DESTINATION', help='destination mobile number') return parser.parse_args() def parseArgsPy26(): @@ -30,7 +32,9 @@ def parseArgsPy26(): parser.add_option('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.') parser.add_option('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate') parser.add_option('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN') - parser.add_option('-d', '--deliver', action='store_true', help='wait for SMS delivery report') + parser.add_option('-d', '--deliver', action='store_true', help='wait for SMS delivery report') + parser.add_option('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds') + parser.add_option('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications') parser.add_positional_argument(Option('--destination', metavar='DESTINATION', help='destination mobile number')) options, args = parser.parse_args() if len(args) != 1: @@ -44,13 +48,13 @@ def main(): if args.port == None: sys.stderr.write('Error: No port specified. Please specify the port to which the GSM modem is connected using the -i argument.\n') sys.exit(1) - modem = GsmModem(args.port, args.baud) + modem = GsmModem(args.port, args.baud, AT_CNMI=args.CNMI) # Uncomment the following line to see what the modem is doing: #logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG) print('Connecting to GSM modem on {0}...'.format(args.port)) try: - modem.connect(args.pin) + modem.connect(args.pin, waitingForModemToStartInSeconds=args.wait) except PinRequiredError: sys.stderr.write('Error: SIM card PIN required. Please specify a PIN with the -p argument.\n') sys.exit(1)