D
D
Denis Khabarov2013-12-17 01:29:29
Python
Denis Khabarov, 2013-12-17 01:29:29

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()

Result:
# python ./test.py
AT+CUSD=1,"*102#",15
ATZCPAS
OK
ATZ
OK
What could be the problem?
ps. The phone has a SIM card from Rostelecom.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Khabarov, 2014-03-16
@Saymon21

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

M
moonsly, 2013-12-17
@moonsly

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 question

Ask a Question

731 491 924 answers to any question