6969
7070FONA_MAX_SOCKETS = const (6 )
7171
72+ # FONA SMS storage methods
73+ FONA_SMS_STORAGE_SIM = b"\" SM\" "
74+ FONA_SMS_STORAGE_INTERNAL = b"\" SM\" "
75+
7276# pylint: enable=bad-whitespace
7377
7478# pylint: disable=too-many-instance-attributes, too-many-public-methods
@@ -528,17 +532,27 @@ def send_sms(self, phone_number, message):
528532 return True
529533
530534 @property
531- def num_sms (self ):
532- """Returns the number of SMS messages stored in memory, False otherwise.
535+ def num_sms (self , sim_storage = True ):
536+ """Returns the number of SMS messages stored in memory, False if none stored.
537+
538+ :param bool sim_storage: SMS storage on the SIM, otherwise internal storage on FONA chip.
533539 """
534540 # text mode
535541 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
536542 return False
537-
543+
538544 # ask how many SMS are stored
539- self ._send_parse_reply (b"AT+CPMS?" , b"\" SM_P\" " + b"," )
540- print ("Reply: " , self ._buf )
541- # TODO
545+ if sim_storage :
546+ if self ._send_parse_reply (b"AT+CPMS?" , FONA_SMS_STORAGE_SIM + b"," , idx = 1 ):
547+ return self ._buf
548+ else :
549+ if self ._send_parse_reply (b"AT+CPMS?" , FONA_SMS_STORAGE_INTERNAL + b"," , idx = 1 ):
550+ return self ._buf
551+ if self ._send_parse_reply (b"AT+CPMS?" , b"\" SM\" ," , idx = 1 ):
552+ return self ._buf
553+ if self ._send_parse_reply (b"AT+CPMS?" , b"\" SM_P\" ," , idx = 1 ):
554+ return self ._buf
555+ return False
542556
543557 def read_sms (self , sms_slot = None ):
544558 """Reads SMS messages from FONA device.
@@ -552,21 +566,29 @@ def read_sms(self, sms_slot=None):
552566
553567 if not self ._send_check_reply (b"AT+CSDH=1" , reply = REPLY_OK ):
554568 return False
555-
556- sms_length = 0
557569
558- if sms_slot is None :
559- self .uart_write (b"AT+CMGL=\" ALL\" " + b"\r \n " )
560- self ._read_line ()
561- self ._read_line ()
562- self ._read_line ()
563- self ._read_line ()
564- return True
570+ # TODO: Add this back
571+ #if sms_slot is None:
572+ # self.uart_write(b"AT+CMGL=\"ALL\"" + b"\r\n")
573+ # self._read_line()
574+ # return True
565575
566576 self .uart_write (b"AT+CMGR=" + str (sms_slot ).encode () + b"\r \n " )
567- self ._read_line ()
568- return True
569-
577+ self ._read_line (1000 )
578+
579+ # get sms length
580+ if not self ._parse_reply (b"+CMGR:" , idx = 11 ):
581+ return False
582+ sms_len = self ._buf
583+ # rsize shared buf
584+ self ._buf = bytearray (sms_len )
585+ # read into buffer
586+ self ._uart .readinto (self ._buf )
587+ # discard unread chars in buffer
588+ self ._uart .reset_input_buffer ()
589+
590+
591+ return bytes (self ._buf ).decode ()
570592
571593
572594 ### Socket API (TCP, UDP) ###
0 commit comments