lamondlab / IteadSIM800

Raspberry Pi Python library for the Itead SIM800 GSM/GPRS module
45 stars 27 forks source link

In some cases sms.ReadSMS() cause an error status,(params,msg)=self.sendATCmdWaitReturnResp("AT+CMGR={}".format(number),"OK") #2

Open andy-rwalker opened 7 years ago

andy-rwalker commented 7 years ago

In some cases call sms.ReadSMS() thrown exception: status,(params,msg)=self.sendATCmdWaitReturnResp("AT+CMGR={}".format(number),"OK") in sms.py", line 364, in readSMS ValueError: need more than 0 values to unpack

ericd1949 commented 6 years ago

3rd March 2018 I've just starting using this library on a bare SIM800L EVB board and it works great, except this issue, so I've investigated, with my very limited Python experience and a wonderment of tuples.

For me, this condition occurs when an attempt is made to read an SMS message that doesn't exist. The code calls the function readSMS() (around line 351) which in turn calls the sendATCmdWaitReturnResp() function and errors as shown above. The reason it gives the ValueError is that the second half of the returned tuple is null, because, in the case of a SMS message not being there, the SIM800L gives a simple "OK" response with nothing else, so the msg part is empty.

I have fixed (kludged, messed it about, whatever) for myself by breaking out the return from the sendATCmdWaitReturnResp() function before assigning it to status,(params,msg)

so my code modification is to comment out line 364, & insert some code before line 365 to do some checking, so my code now reads, including the commented line 364 and ex line 365 (now line 374)

#status,(params,msg)=self.sendATCmdWaitReturnResp("AT+CMGR={}".format(number),"OK")
# The next few lines examine the returned status in case the msg part is null
# which means the SMS has been deleted or is simply not there, in which case we return False

 returnValue=self.sendATCmdWaitReturnResp("AT+CMGR={}".format(number),"OK")

  if not returnValue[1]:
       self._logger.debug("SMS msg is null, so non-existant")
       return False

  status,(params,msg)=returnValue

  if status!=ATResp.OK or not params.startswith("+CMGR: "): return None

I haven't created any code change here, because my Pythonology is limited, but I hope this helps anybody who comes across this problem and would like to take full advantage of this really nice library.

Eric