-1

i am using gsmmodem package, and trying to get response from USSD code which i am sending. USSD code execution works fine, but response is not in proper format (non-human readable) i want to get it in readable format.

Please check code here

from __future__ import print_function
import logging
PORT = 'COM11'
BAUDRATE = 921600
USSD_STRING = '*111#'
PIN = None # SIM card PIN (if any)

from gsmmodem.modem import GsmModem

def main():
    print('Initializing modem...')
    modem = GsmModem(PORT, BAUDRATE)
    modem.connect(PIN)
    modem.waitForNetworkCoverage(10)
    print('Sending USSD string: {0}'.format(USSD_STRING))
    response = modem.sendUssd(USSD_STRING) # response type: gsmmodem.modem.Ussd
    print('USSD reply received: {0}'.format(response.message))
    if response.sessionActive:
        print('Closing USSD session.')
        # At this point, you could also reply to the USSD message by using response.reply()
        response.cancel()
    else:
        print('USSD session was ended by network.')
    modem.close()

if __name__ == '__main__':
    main()

Link for the code http://pastebin.com/SvYptykS

Ashfaque Ali Solangi
  • 1,883
  • 3
  • 22
  • 34

1 Answers1

-1

You are getting the answer in a gsm hexadecimal type. So you have to decode the response.message string to a proper format and encode it after in a human readable format like utf-8.

So, after

response = modem.sendUssd(USSD_STRING)

Write:

ussd_message = unicode(response.message.decode('hex'), 'utf-16-be').encode('utf8')
print('USSD reply received: {0}'.format(ussd_message))
Moumit
  • 8,314
  • 9
  • 55
  • 59