简体   繁体   中英

Python read ussd response with GsmModem in human-readable string (en)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM