Answer the question
In order to leave comments, you need to log in
Executing a USSD Request from a Python Script
There is an SMS gateway based on Nokia 5130 xpressmusic, connected by cable to the server on ubuntu.
I decided to check the balance with a Python script by executing a ussd request *102#, but I just can’t get a correct answer from the phone.
The script itself:
import serial
ser = serial.Serial('/dev/ttyACM0')
ser.write('AT+CUSD=1,"*102#",15\r\n') #
#ser.write('ATD*102#\r\n')
#ser.write('AT+csq\r\n')
while True:
print ser.readline()
Answer the question
In order to leave comments, you need to log in
I'll stick with the one that worked...
import base64
f = open('/dev/ttyACM1','w+a')
f.write('AT+CUSD=1,"*102#",15\r\n')
while True:
line = f.readline()
if len(line) == 0:
continue
else:
if line.startswith('+CUSD'):
print base64.b16decode(line[10:line.rfind('"')]).decode('utf-16-be')
break
else:
print line
There may be the wrong encoding for AT commands, it is treated with the AT+CSCS=UCS2/IRA command:
www.activexperts.com/mmtoolkit/at/commands/?at=%2BCSCS
Also, your device may not support the encoding in which the response comes to a USSD request (if the balance comes with Cyrillic) - then just change the device to one that supports encoding in AT commands.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question