babca / python-gsmmodem

Maintained fork of a Python module to control GSM modems attached to the system: send/receive SMS messages in your scripts, handle calls, and more.
GNU Lesser General Public License v3.0
174 stars 106 forks source link

The example in ussd is not working. Not getting ussd response #25

Open mwaaas opened 7 years ago

mwaaas commented 7 years ago

This the error am gettings.

/Users/francis/PycharmProjects/python-gsmmodem/.venv/bin/python /Users/francis/PycharmProjects/python-gsmmodem/examples/ussd_demo.py Initializing modem... Sending USSD string: 1405*0702729654# Traceback (most recent call last): File "/Users/francis/PycharmProjects/python-gsmmodem/examples/ussd_demo.py", line 41, in main() File "/Users/francis/PycharmProjects/python-gsmmodem/examples/ussd_demo.py", line 30, in main response = modem.sendUssd(USSD_STRING) # response type: gsmmodem.modem.Ussd File "/Users/francis/PycharmProjects/python-gsmmodem/gsmmodem/modem.py", line 946, in sendUssd raise TimeoutException() gsmmodem.exceptions.TimeoutException: None

menon92 commented 7 years ago

I have same problem. Do you get the solution of this problem

mwaaas commented 7 years ago

Yeah I managed, will find the solution I used then share it with you

menon92 commented 7 years ago

could you please help me now. How can I solve this problem ? I am completely stuck with this problem @mwaaas

mwaaas commented 7 years ago

Here is the code that I used

!/usr/bin/env python

"""\ Demo: Simple USSD example

Simple demo app that initiates a USSD session, reads the string response and closes the session (if it wasn't closed by the network)

Note: for this to work, a valid USSD string for your network must be used. """

from future import print_function

import logging

PORT = '/dev/tty.HUAWEIMobile-Pcui' BAUDRATE = 115200 USSD_STRING = '1405*0703488092#' PIN = None # SIM card PIN (if any)

from gsmmodem.modem import GsmModem

def main(): print('Initializing modem...')

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

modem = GsmModem(PORT, BAUDRATE)
modem.connect(PIN)
modem.waitForNetworkCoverage(10)
print('Sending USSD string: {0}'.format(USSD_STRING))
response = modem.sendUssd(USSD_STRING, 30) # 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()

mwaaas commented 7 years ago

use this python-gsmmodem-new

pip install python-gsmmodem-new

menon92 commented 7 years ago

Thanks for your reply. I try your code. In your code from future import print_function give me Import Error. for this I change your code like this. and this give me the same error. could you please help me.

#!/usr/bin/env python

"""
Demo: Simple USSD example

Simple demo app that initiates a USSD session, reads the string response and closes the session
(if it wasn't closed by the network)

Note: for this to work, a valid USSD string for your network must be used.
"""

#from future import print_function

import logging

#PORT = '/dev/tty.HUAWEIMobile-Pcui'
PORT = '/dev/ttyUSB0'
BAUDRATE = 115200
#USSD_STRING = '1405*0703488092#'
USSD_STRING = 'AA988C3602' # *124# = AA988C3602
PIN = None # SIM card PIN (if any)

from gsmmodem.modem import GsmModem

def main():
    print('Initializing modem...')
    #logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
    modem = GsmModem(PORT, BAUDRATE)
    modem.connect(PIN)
    modem.waitForNetworkCoverage(10)
    print('Sending USSD string: {0}'.format(USSD_STRING))
    response = modem.sendUssd(USSD_STRING, 30) # 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()

if __name__ == '__main__':
    main()

Thanks @mwaaas

menon92 commented 7 years ago

My modem port:

menon@menon-Inspiron-N4050:~$ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 May 20 08:50 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 May 20 08:47 /dev/ttyUSB1
crw-rw---- 1 root dialout 188, 2 May 20 10:16 /dev/ttyUSB2
lrwxrwxrwx 1 root root         7 May 20 08:47 /dev/ttyUSB_utps_diag -> ttyUSB1
lrwxrwxrwx 1 root root         7 May 20 08:47 /dev/ttyUSB_utps_modem -> ttyUSB0
lrwxrwxrwx 1 root root         7 May 20 08:47 /dev/ttyUSB_utps_pcui -> ttyUSB2
menon@menon-Inspiron-N4050:~$ 
babca commented 7 years ago

@menon92 set your PORT to dev/ttyUSB2. The TimeoutException is expected in this case. You can't catch incoming calls, USSDs and SMS messages with dev/ttyUSB0 port on Huawei modems, because this channel doesn't contain modem notifications.

I tested ussd_demo.py with my Huawei modem and it's working as expected: dev/ttyUSB2 => USSD working fine ✓ dev/ttyUSB0 => TimeoutException ✓

If your code still doesn't work, uncomment logging.basicConfig(... line and post here your console output.

menon92 commented 7 years ago

when I set PORT to /dev/ttyUSB2 then output is :

Initializing modem...
INFO: Connecting to modem on port /dev/ttyUSB2 at 115200bps
DEBUG: write: ATZ
DEBUG: response: ['OK']
DEBUG: write: ATE0
DEBUG: response: ['ATE0\r', 'OK']
DEBUG: write: AT+CFUN?
DEBUG: response: ['+CFUN: 1', 'OK']
DEBUG: write: AT+CMEE=1
DEBUG: response: ['OK']
DEBUG: write: AT+CPIN?
DEBUG: response: ['+CPIN: READY', 'OK']
DEBUG: write: AT+CLAC
DEBUG: response: ['+CLAC:&C,&D,&E,&F,&S,&V,&W,E,I,L,M,Q,V,X,Z,T,P,\\Q,\\S,\\V,%V,D,A,H,O,S0,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S30,S103,S104,+FCLASS,+ICF,+IFC,+IPR,+GMI,+GMM,+GMR,+GCAP,+GSN,+DR,+DS,+WS46,+CLAC,+CCLK,+CBST,+CRLP,+CV120,+CHSN,+CSSN,+CREG,+CGREG,+CFUN,+GCAP,+CSCS,+CSTA,+CR,+CEER,+CRC,+CMEE,+CGDCONT,+CGDSCONT,+CGTFT,+C', 'ERROR']
DEBUG: write: AT
DEBUG: response: ['OK']
DEBUG: write: AT^CVOICE=?
DEBUG: write: AT+VTS=?
DEBUG: write: AT^DTMF=?
DEBUG: write: AT^USSDMODE=?
DEBUG: write: AT+WIND=?
DEBUG: write: AT+ZPAS=?
DEBUG: write: AT+CSCS=?
DEBUG: write: AT+CNUM=?
DEBUG: write: AT+CGMI
Traceback (most recent call last):
  File "pygsm_modem_send_ussd.py", line 47, in <module>
    main()
  File "pygsm_modem_send_ussd.py", line 29, in main
    modem.connect(PIN)
  File "/usr/local/lib/python2.7/dist-packages/gsmmodem/modem.py", line 282, in connect
    if b'simcom' in self.manufacturer.lower() : #simcom modems support DTMF and don't support AT+CLAC
  File "/usr/local/lib/python2.7/dist-packages/gsmmodem/modem.py", line 524, in manufacturer
    return self.write('AT+CGMI')[0]
  File "/usr/local/lib/python2.7/dist-packages/gsmmodem/modem.py", line 470, in write
    responseLines = super(GsmModem, self).write(data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq)
  File "/usr/local/lib/python2.7/dist-packages/gsmmodem/serial_comms.py", line 133, in write
    self.serial.write(data)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 528, in write
    raise portNotOpenError
serial.serialutil.SerialException: Attempting to use a port that is not open

And when I set PORT to /dev/ttyUSB0 then output is :

Initializing modem...
INFO: Connecting to modem on port /dev/ttyUSB0 at 115200bps
DEBUG: write: ATZ
DEBUG: response: ['OK']
DEBUG: write: ATE0
DEBUG: response: ['ATE0\r', 'OK']
DEBUG: write: AT+CFUN?
DEBUG: response: ['+CFUN: 1', 'OK']
DEBUG: write: AT+CMEE=1
DEBUG: response: ['OK']
DEBUG: write: AT+CPIN?
DEBUG: response: ['+CPIN: READY', 'OK']
DEBUG: write: AT+CLAC
DEBUG: response: ['+CLAC:&C,&D,&E,&F,&S,&V,&W,E,I,L,M,Q,V,X,Z,T,P,\\Q,\\S,\\V,%V,D,A,H,O,S0,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S30,S103,S104,+FCLASS,+ICF,+IFC,+IPR,+GMI,+GMM,+GMR,+GCAP,+GSN,+DR,+DS,+WS46,+CLAC,+CCLK,+CBST,+CRLP,+CV120,+CHSN,+CSSN,+CREG,+CGREG,+CFUN,+GCAP,+CSCS,+CSTA,+CR,+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VOICE,^DDSETEX,^pcmrecord,^CMSR,^CMMT,^CMGI,^PCMBUFFER,^CSNR,^PNN,^OPL,^CPNN,^ADCTEMP,^RDCUST,^SETPID,^DIALMODE,^YJCX,^NDISDUP,^DNSP,^DNSS,^DHCP,^AUTHDATA,^CRPN,^ICCID,^FPLMN,^FPLMNCTRL,^NVMBN,^RXDIV,', 'OK']
DEBUG: write: AT^CVOICE=0
DEBUG: response: ['OK']
DEBUG: write: AT^USSDMODE=0
DEBUG: response: ['OK']
INFO: Loading Huawei call state update table
DEBUG: write: AT+COPS=3,0
DEBUG: response: ['OK']
DEBUG: write: AT+CMGF=0
DEBUG: response: ['OK']
DEBUG: write: AT+CSCA?
DEBUG: response: ['+CSCA: "+880150159999",145', 'OK']
DEBUG: write: AT+CSMP=49,167,0,0
DEBUG: response: ['OK']
DEBUG: write: AT+CSCA?
DEBUG: response: ['+CSCA: "+880150159999",145', 'OK']
DEBUG: write: AT+CPMS=?
DEBUG: response: ['+CPMS: ("ME","MT","SM","SR"),("ME","MT","SM","SR"),("ME","MT","SM","SR")', 'OK']
DEBUG: write: AT+CPMS="ME","ME","ME"
DEBUG: response: ['+CPMS: 0,23,0,23,0,23', 'OK']
DEBUG: write: AT+CNMI=2,1,0,2
DEBUG: response: ['OK']
DEBUG: write: AT+CLIP=1
DEBUG: response: ['OK']
DEBUG: write: AT+CRC=1
DEBUG: response: ['OK']
DEBUG: write: AT+CVHU=0
DEBUG: response: ['OK']
DEBUG: write: AT+CREG?
DEBUG: response: ['+CREG: 0,1', 'OK']
DEBUG: write: AT+CSQ
DEBUG: response: ['+CSQ: 6,99', 'OK']
Sending USSD string: AA584D3602
DEBUG: write: AT+CUSD=1,"AA584D3602",15
DEBUG: response: ['OK']
Traceback (most recent call last):
  File "pygsm_modem_send_ussd.py", line 47, in <module>
    main()
  File "pygsm_modem_send_ussd.py", line 32, in main
    response = modem.sendUssd(USSD_STRING, 30) # response type:
  File "/usr/local/lib/python2.7/dist-packages/gsmmodem/modem.py", line 958, in sendUssd
    raise TimeoutException()
gsmmodem.exceptions.TimeoutException: None

Thanks.

babca commented 7 years ago

serial.serialutil.SerialException: Attempting to use a port that is not open It seems your modem unexpectedly restarts itself? (Maybe a bug in the modem firmware?) It fails at simple CLAC command, the response is cut after ~300 characters.

Could you start gsmterm from tools/ folder? python gsmterm.py -r /dev/ttyUSB2 type AT+CLAC, Enter is the command working properly? (Press CTRL+] to exit.)

menon92 commented 7 years ago

I run python gsmterm.py -r /dev/ttyUSB2 and it give me output:

Raw terminal connected to /dev/ttyUSB2 at 115200bps.
Press CTRL+] to exit.

ERROR
AT+CLAC
ERROR
AT+CLAC
+CLAC:&C,&D,&E,&F,&S,&V,&W,E,I,L,M,Q,V,X,Z,T,P,\Q,\S,\V,%V,D,A,H,O,S0,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S30,S103,S104,+FCLASS,+ICF,+IFC,+IPR,+GMI,+GMM,+GMR,+GCAP,+GSN,+DR,+DS,+WS46,+CLAC,+CCLK,+CBST,+CRLP,+CV120,+CHSN,+CS
+CPMS: 0,50,0,50,0,50
OK
AT+CLAC
Done.

I think AT+CLAC command sometimes works and sometimes not working.

If there is problem with modem firmware or modem unexpectedly restarts In that case what can i do ? should i try a new modem with the same code ? or anything else i can do .

Thanks @babca

babca commented 7 years ago

This seems to be related to the modem not to python-gsmmodem library. Please try it again and type ATZ command before AT+CLAC command. Both should output OK on the last line, every time. I don't think it will help, but you can also try to lower baudrate: python gsmterm.py -r /dev/ttyUSB2 -b 9600

Which modem do you use?

Try a different modem if you can. Huawei E372 and E160 are working fine with ussd_demo.py.