fcntl.flock(lockf,fcntl.LOCK_UN)
lockf.close()
+def ensure_modem_listening (modem):
+ # We try to ger response for about 2 seconds
+ for i in range(20):
+ modem.send('at junk\r');
+ index = modem.expect (['ERROR\r', pexpect.TIMEOUT], 0.1);
+ if index == 0:
+ modem.send('at\r');
+ index = modem.expect (['OK\r'], 1);
+ return;
+ raise
+
def init_modem(modem):
# We have only one modem, simultaneous acces wouldn't bring anything good
gain_lock()
if modem == None :
# OK response should be recieved shortly
modem = pexpect.spawn('pnatd', [], 2)
- # FIXME This is a dirty hack. A better solution needed
- time.sleep(0.5)
- try :
- modem.send('at\r');
- # Read our "at" command
- modem.readline();
- # Read OK response
- response = modem.readline().strip()
- except pexpect.TIMEOUT:
- modem.kill(9)
- modem = None
- response = ""
- if response != "OK" :
- time.sleep(0.5)
- init_retry -= 1
+ try :
+ ensure_modem_listening (modem);
+ except :
+ modem.kill(9);
+ modem = None;
+ response = "";
else:
try:
# Switch output encoding to GSM default encoding