2 # -*- coding: utf-8 -*-
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published
5 ## by the Free Software Foundation; version 2 and higer.
7 ## Guseynov Alexey (kibergus bark-bark gmail.com) 2010
11 from subprocess import *
17 if len(sys.argv) != 2:
18 print "Usage: ussdquery.py <ussd number>"
21 # Operations should timeout in 30 seconds.
22 # I'm not shure, that readline uses timeouts
26 while response != "OK" and retry > 0 :
28 # OK response should be recieved shortly
29 child = pexpect.spawn('pnatd', [], 2)
32 # Read our "at" command
35 response = child.readline().strip()
36 except pexpect.TIMEOUT:
47 print >> sys.stderr, "Couldn't init modem."
51 child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r')
52 # Read our query echoed back
56 replystring = child.readline()
58 except pexpect.TIMEOUT:
59 print >> sys.stderr, "Timeout. Modem didn't reply."
62 if replystring.strip() == "ERROR" :
63 print >> sys.stderr, "Modem returned ERROR. Query not executed."
66 start = replystring.find('"')
67 end = replystring.find('"', start+1)
68 reply = replystring[start+1:end]
69 encoding = replystring[end+2:].strip()
72 reply = gsmdecode.decode(reply, int(encoding))