@@ -229,7 +229,6 @@ def iccid(self):
229229 @property
230230 def gprs (self ):
231231 """Returns module's GPRS state."""
232- self ._read_line ()
233232
234233 if not self ._send_parse_reply (b"AT+CGATT?" , b"+CGATT: " , ":" ):
235234 return False
@@ -330,24 +329,15 @@ def set_gprs(self, apn=None, enable=True):
330329
331330 @property
332331 def network_status (self ):
333- """Returns cellular/ network status"""
332+ """Returns cellular network status"""
334333 if self ._debug :
335334 print ("Network status" )
336335 if not self ._send_parse_reply (b"AT+CREG?" , b"+CREG: " , idx = 1 ):
337336 return False
338- if self ._buf == 0 : # Not Registered
339- return self ._buf
340- if self ._buf == 1 : # Registered (home)
341- return self ._buf
342- if self ._buf == 2 : # Not Registered (searching)
343- return self ._buf
344- if self ._buf == 3 : # Denied
345- return self ._buf
346- if self ._buf == 4 : # Unknown
347- return self ._buf
348- if self ._buf == 5 : # Registered Roaming
349- return self ._buf
350- return False
337+ status = self ._buf
338+ if not 0 <= self ._buf <= 5 :
339+ status = - 1
340+ return status
351341
352342 @property
353343 def rssi (self ):
@@ -451,7 +441,6 @@ def get_host_by_name(self, hostname):
451441 """
452442 if self ._debug :
453443 print ("*** Get host by name" )
454- self ._read_line ()
455444 if isinstance (hostname , str ):
456445 hostname = bytes (hostname , "utf-8" )
457446
@@ -527,7 +516,7 @@ def num_sms(self, sim_storage=True):
527516 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
528517 return False
529518
530- if sim_storage : # ask how many SMS are stored
519+ if sim_storage : # ask how many SMS are stored
531520 if self ._send_parse_reply (b"AT+CPMS?" , FONA_SMS_STORAGE_SIM + b"," , idx = 1 ):
532521 return self ._buf
533522 else :
@@ -562,7 +551,6 @@ def delete_sms(self, sms_slot):
562551 :param int sms_slot: SMS SIM or FONA memory slot number.
563552
564553 """
565- self ._read_line ()
566554 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
567555 return False
568556
@@ -579,7 +567,6 @@ def read_sms(self, sms_slot):
579567 :param int sms_slot: SMS SIM or FONA memory slot number.
580568
581569 """
582- self ._read_line ()
583570 if not self ._send_check_reply (b"AT+CMGF=1" , reply = REPLY_OK ):
584571 return False
585572 if not self ._send_check_reply (b"AT+CSDH=1" , reply = REPLY_OK ):
@@ -620,7 +607,7 @@ def get_socket(self):
620607 self ._read_line (100 ) # table header
621608
622609 allocated_socket = 0
623- for sock in range (0 , FONA_MAX_SOCKETS ): # check if INITIAL state
610+ for sock in range (0 , FONA_MAX_SOCKETS ): # check if INITIAL state
624611 self ._read_line (100 )
625612 self ._parse_reply (b"C:" , idx = 5 )
626613 if self ._buf .strip ('"' ) == "INITIAL" or self ._buf .strip ('"' ) == "CLOSED" :
@@ -661,7 +648,7 @@ def socket_status(self, sock_num):
661648 return False
662649 self ._read_line ()
663650
664- for state in range (0 , sock_num + 1 ): # read "C: <n>" for each active connection
651+ for state in range (0 , sock_num + 1 ): # read "C: <n>" for each active connection
665652 self ._read_line ()
666653 if state == sock_num :
667654 break
@@ -762,7 +749,6 @@ def socket_close(self, sock_num, quick_close=1):
762749 sock_num < FONA_MAX_SOCKETS
763750 ), "Provided socket exceeds the maximum number of \
764751 sockets for the FONA module."
765- self ._read_line ()
766752
767753 self .uart_write (b"AT+CIPCLOSE=" + str (sock_num ).encode () + b"," )
768754 self .uart_write (str (quick_close ).encode () + b"\r \n " )
@@ -782,7 +768,6 @@ def socket_read(self, sock_num, length):
782768 sock_num < FONA_MAX_SOCKETS
783769 ), "Provided socket exceeds the maximum number of \
784770 sockets for the FONA module."
785- self ._read_line ()
786771 if self ._debug :
787772 print ("* socket read" )
788773
@@ -807,7 +792,6 @@ def socket_write(self, sock_num, buffer):
807792
808793 """
809794 self ._read_line ()
810- self ._read_line ()
811795 assert (
812796 sock_num < FONA_MAX_SOCKETS
813797 ), "Provided socket exceeds the maximum number of \
@@ -849,6 +833,7 @@ def _send_parse_reply(self, send_data, reply_data, divider=",", idx=0):
849833 :param str divider: Separator
850834
851835 """
836+ self ._read_line ()
852837 self ._get_reply (send_data )
853838
854839 if not self ._parse_reply (reply_data , divider , idx ):
@@ -914,9 +899,9 @@ def _read_line(self, timeout=FONA_DEFAULT_TIMEOUT_MS, multiline=False):
914899 if char == b"\r " :
915900 continue
916901 if char == b"\n " :
917- if reply_idx == 0 : # ignore first '\n'
902+ if reply_idx == 0 : # ignore first '\n'
918903 continue
919- if not multiline : # second '\n' is EOL
904+ if not multiline : # second '\n' is EOL
920905 timeout = 0
921906 break
922907 self ._buf += char
@@ -945,6 +930,7 @@ def _send_check_reply(
945930 :param bytes reply: Expected response from module.
946931
947932 """
933+ self ._read_line ()
948934 if send is None :
949935 if not self ._get_reply (prefix = prefix , suffix = suffix , timeout = timeout ):
950936 return False
0 commit comments