faucamp / python-gsmmodem

Python module to control a GSM modem attached to the system: send/receive SMS messages, handle calls, etc
GNU Lesser General Public License v3.0
384 stars 302 forks source link

gsmmodem.exceptions.TimeoutException #24

Open qosimmax opened 10 years ago

qosimmax commented 10 years ago

Hi, i installed python-gsmmodem module for python3.2 and after connecting to modem show this error: File "C:\Python32\lib\site-packages\gsmmodem\serial_comms.py", line 135, in write raise TimeoutException() gsmmodem.exceptions.TimeoutException

why show such a error? where is problem?

my python code :

from future import print_function

import logging

PORT = 'COM30' BAUDRATE = 115200 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.close()

if name == 'main': main()

model of the modem : WAVECOM MODEM MULTIBAND 900E 1800

faucamp commented 10 years ago

Thanks for the report! It sounds like the modem isn't responding to any of the commands sent to it during modem.connect(), but I would need a bit more info to be sure. Please make sure that the modem actually responds on "COM30" at a baud rate of 115200 - connect to it using a serial terminal such as Hyperterminal using those settings (my Wavecom modems are picky about baudrate, so try a few others) and type in "AT".

Next, to see what the modem is doing in python-gsmmodem, please add the following line to the beginning of your main() function:

logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
qosimmax commented 10 years ago

I connected modem to Hyperterminal to "COM30" at a baud rate of 115200 and typed some AT commands. In Hyperterminal normal worked

Fragment from Hyperterminal: ATZ OK AT OK ATI WAVECOM MODEM

MULTIBAND 900E 1800

OK

So in python successfully connected with pyserial 2.6 module and worked AT commands.

Next, i added logging function in main() function and compiled:

INFO: Connecting to modem on port COM30 at 57600bps DEBUG: write: ATZ

Traceback (most recent call last): File "D:/Projects/modemtest/modemtest.py", line 17, in main() File "D:/Projects/modemtest/modemtest.py", line 13, in main modem.connect() File "C:\Python32\lib\site-packages\gsmmodem\modem.py", line 173, in connect self.write('ATZ') # reset configuration File "C:\Python32\lib\site-packages\gsmmodem\modem.py", line 403, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "C:\Python32\lib\site-packages\gsmmodem\serial_comms.py", line 135, in write raise TimeoutException() gsmmodem.exceptions.TimeoutException: None

faucamp commented 10 years ago

From those logs it looks like python-gsmmodem is set to connect at 57600bps, not 115200bps, and the modem is definitely not responding to the initial ATZ - please make sure that your code is setting the correct baud rate and that Python isn't using stale .pyc files (simply delete any that you find in your project dir).

qosimmax commented 10 years ago

In baudrate 115200bps still shows this error:

INFO: Connecting to modem on port COM30 at 115200bps DEBUG: write: ATZ Traceback (most recent call last): File "D:/Projects/modemtest/modemtest.py", line 18, in main() File "D:/Projects/modemtest/modemtest.py", line 13, in main modem.connect() File "C:\Python32\lib\site-packages\gsmmodem\modem.py", line 177, in connect self.write('ATZ') # reset configuration File "C:\Python32\lib\site-packages\gsmmodem\modem.py", line 410, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "C:\Python32\lib\site-packages\gsmmodem\serial_comms.py", line 139, in write raise TimeoutException() gsmmodem.exceptions.TimeoutException: None

my python code:

from future import print_function from gsmmodem.modem import GsmModem import logging import time PORT = 'COM30' BAUDRATE = 115200 PIN = None # SIM card PIN (if any) def main(): logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)

print('Initializing modem...')

modem = GsmModem(PORT, BAUDRATE)
modem.connect()
time.sleep(1)
modem.close()

if name == 'main': main()

qosimmax commented 10 years ago

In baudrate 115200bps still shows this error:

INFO: Connecting to modem on port COM30 at 115200bps DEBUG: write: ATZ

how to solved this problem?

faucamp commented 10 years ago

This is very strange indeed; it looks like pyserial is simply not communicating with the modem. Could you please try to connect to the modem using pyserial's miniterm.py program and testing it like you did with hyperterminal?

python -m serial.tools.miniterm -p COM30 -b 115200

Also, please create and run the following script:

import serial
ser = serial.Serial(port='COM30', baudrate=115200, timeout=3)
ser.write("AT\r")
print('ser.inWaiting(): {0}'.format(ser.inWaiting())
while ser.inWaiting():
    print(ser.read())
ser.close()     

This should give you an output such as:

ser.inWaiting(): 4
O
K

If that does not work, please try setting the port parameter to 0 (the integer value) and retry - this will make pyserial use the first available serial port:

import serial
ser = serial.Serial(port=0, baudrate=115200, timeout=3)
print('Using port: '+ser.portstr)
ser.write("AT\r")
print('ser.inWaiting(): {0}'.format(ser.inWaiting())
while ser.inWaiting():
    print(ser.read())
ser.close()     
qosimmax commented 10 years ago

I tested with miniterm.py , successfully connected to on port COM30 at 115200bps and successfully executed AT commands.

Then i run above scripts: 1)... import serial ser = serial.Serial(port='COM30', baudrate=115200, timeout=3) ser.write("AT\r".emcode()) print('ser.inWaiting(): {0}'.format(ser.inWaiting()) while ser.inWaiting(): print(ser.read()) ser.close()

printed this: ser.inWaiting(): 0

2)... import serial ser = serial.Serial(port=0, baudrate=115200, timeout=3) print('Using port: '+ser.portstr) ser.write("AT\r".encode()) print('ser.inWaiting(): {0}'.format(ser.inWaiting()) while ser.inWaiting(): print(ser.read()) ser.close()

printed this: File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 59, in open raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port COM1: [Error 2] Не удается найти указанный файл.

3).. import serial ser = serial.Serial(port=29, baudrate=115200, timeout=3) print('Using port: '+ser.portstr) ser.write("AT\r".encode()) print('ser.inWaiting(): {0}'.format(ser.inWaiting())) while ser.inWaiting(): print(ser.read()) ser.close()

printed this: Using port: COM30 ser.inWaiting(): 0

qosimmax commented 10 years ago

py thon code: 1) import serial ser = serial.Serial(port=29, baudrate=115200, timeout=10) print('Using port: '+ser.portstr) ser.write("AT\r") print('ser.inWaiting(): {0}'.format(ser.inWaiting())) while ser.inWaiting(): print(ser.read()) ser.close()

printed this: Using port: COM30 ser.inWaiting(): 0

when i replaced while ser.inWaiting() to while True: 2) import serial ser = serial.Serial(port=29, baudrate=115200, timeout=10) print('Using port: '+ser.portstr) ser.write("AT\r") print('ser.inWaiting(): {0}'.format(ser.inWaiting())) while True: print(ser.read()) ser.close()

this code printed: O K

please help..

ibrahimgha commented 10 years ago

I had this same problem, turned out that I had no credit to send an sms. Because I had recently bought the sim card, all the credit I had bought was transformed into data and I had no credit left to send an SMS. It would be nice if the error said notEnoughCredit instead of TimeoutException. Hope this helps.

romabysen commented 10 years ago

I had the same problem and it seems to be due to GsmModem.connect trying to unlock the SIM using a PIN even if the PIN is None. Only trying to unlock the SIM if a PIN was given solves it for me. I'll put together a pull-request asap for this.

dengyw2003 commented 10 years ago

I had the same problem, any update?

Initializing modem... INFO: Connecting to modem on port COM151 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

Traceback (most recent call last): File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 69, in main() File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 32, in main modem.connect(PIN) File "build\bdist.win32\egg\gsmmodem\modem.py", line 201, in connect commands = self.supportedCommands File "build\bdist.win32\egg\gsmmodem\modem.py", line 491, in supportedCommands response = self.write('AT+CLAC') File "build\bdist.win32\egg\gsmmodem\modem.py", line 398, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "build\bdist.win32\egg\gsmmodem\serial_comms.py", line 135, in write raise TimeoutException() TimeoutException

dengyw2003 commented 10 years ago

I had the same problem, any update?

Initializing modem... INFO: Connecting to modem on port COM151 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

Traceback (most recent call last): File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 69, in main() File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 32, in main modem.connect(PIN) File "build\bdist.win32\egg\gsmmodem\modem.py", line 201, in connect commands = self.supportedCommands File "build\bdist.win32\egg\gsmmodem\modem.py", line 491, in supportedCommands response = self.write('AT+CLAC') File "build\bdist.win32\egg\gsmmodem\modem.py", line 398, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "build\bdist.win32\egg\gsmmodem\serial_comms.py", line 135, in write raise TimeoutException() TimeoutException

dengyw2003 commented 10 years ago

I had the same problem, any update?

Initializing modem... INFO: Connecting to modem on port COM151 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

Traceback (most recent call last): File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 69, in main() File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 32, in main modem.connect(PIN) File "build\bdist.win32\egg\gsmmodem\modem.py", line 201, in connect commands = self.supportedCommands File "build\bdist.win32\egg\gsmmodem\modem.py", line 491, in supportedCommands response = self.write('AT+CLAC') File "build\bdist.win32\egg\gsmmodem\modem.py", line 398, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "build\bdist.win32\egg\gsmmodem\serial_comms.py", line 135, in write raise TimeoutException() TimeoutException

faucamp commented 10 years ago

@dengyw2003

DEBUG: write: AT+CPIN? DEBUG: response: ['+CPIN: READY', 'OK'] DEBUG: write: AT+CLAC

Traceback (most recent call last): File "E:\MTBF\FT自动化\AT Project\Python-gsmmodem\python-gsmmodem-0.9\examples\dial_polling_demo.py", line 69, in main()

This looks like the modem isn't responding to the AT+CLAC call at all - could you please run tools/identify-modem.py -d and post the output? Also, could you kindly connect to the modem using hyperterminal, issue the AT+CLAC command, and post the ouptut of that? Thanks!

valerycolong commented 9 years ago

Hello Francois, great work again with python gsmmodem. I am porting my SMS application to Python which has gone so well until now. I ran into the TypeError: an integer is required which occurred at "C:\Python\34\lib\site-packages\gsmmodem\serial_comms.py", line 127, in write self.serial.write(data) I am using Python 3.4 and python gsmmodem 0.9 with a Sierra Wireless GSM Modem. I was able to solve the issue above by extending the GsmModem class and override the _readLoop and write methods with the patches suggested by youtux on https://github.com/faucamp/python-gsmmodem/issues/39. Beyond that, I ran again into TimeoutException; gsmmodem.exceptions.TimeoutException which is raised from File "C:\Python\34\lib\site-packages\gsmmodem\modem.py", line 177, in connect self.write('ATZ') # reset configuration Is there any solution you can propose for me?

[Edit] I forgot to mention that I was able to connect and send SMS through my modem using pySerial and pyGSM but I do not fancy AT Commands.

binhvq commented 9 years ago

i have same error! bash-4.2$ sudo python sms_handler_demo.py [sudo] password for binhvq: Initializing modem... INFO: Connecting to modem on port /dev/ttyUSB2 at 115200bps DEBUG: write: ATZ Traceback (most recent call last): File "sms_handler_demo.py", line 40, in main() File "sms_handler_demo.py", line 32, in main modem.connect(PIN) File "/usr/lib/python2.7/site-packages/python_gsmmodem-0.9-py2.7.egg/gsmmodem/modem.py", line 177, in connect self.write('ATZ') # reset configuration File "/usr/lib/python2.7/site-packages/python_gsmmodem-0.9-py2.7.egg/gsmmodem/modem.py", line 398, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "/usr/lib/python2.7/site-packages/python_gsmmodem-0.9-py2.7.egg/gsmmodem/serial_comms.py", line 135, in write raise TimeoutException() gsmmodem.exceptions.TimeoutException

Please help me! Thanks!

dwaynez commented 8 years ago

I am having the timeout error on the ATZ command as described above. I have isolated the problem to be caused by running under python3 (works fine under python). I have already invested a lot into the rest of my program to switch back to python 2. Any thoughts on why this problem occurs? I am able to run miniterm.py under python3 successfully.

marutichintan commented 8 years ago

Just Increase the Timeout in modem.py file class Gsmmodem with def write

Because this "AT+CLAC" size is more, So it Takes time to Transfer.

mpeirwe commented 8 years ago

I was experiencing the timeout issue and it turns out the problem is trying to unlock even if no PIN is provided just like @romabysen mentioned. For my case I did not need the PIN at all. Below is the error I was getting:

$ /opt/env/gsm/bin/python /opt/env/gsm/bin/identify-modem.py -b 115200 /dev/ttyS0 args: Namespace(baud='115200', debug=False, pin=None, port='/dev/ttyS0') Connecting to GSM modem on /dev/ttyS0... Traceback (most recent call last): File "/opt/env/gsm/bin/identify-modem.py", line 88, in main() File "/opt/env/gsm/bin/identify-modem.py", line 49, in main modem.connect(args.pin) File "/opt/env/gsm/local/lib/python2.7/site-packages/gsmmodem/modem.py", line 198, in connect self._unlockSim(pin) File "/opt/env/gsm/local/lib/python2.7/site-packages/gsmmodem/modem.py", line 366, in _unlockSim if self.write('AT+CPIN?')[0] != '+CPIN: READY': File "/opt/env/gsm/local/lib/python2.7/site-packages/gsmmodem/modem.py", line 398, in write responseLines = SerialComms.write(self, data + writeTerm, waitForResponse=waitForResponse, timeout=timeout, expectedResponseTermSeq=expectedResponseTermSeq) File "/opt/env/gsm/local/lib/python2.7/site-packages/gsmmodem/serial_comms.py", line 135, in write raise TimeoutException() gsmmodem.exceptions.TimeoutException

I then applied the patch below: modem.py-patch.txt

and was successful:

$ /opt/env/gsm/bin/python /opt/env/gsm/bin/identify-modem.py -b 115200 /dev/ttyS0 args: Namespace(baud='115200', debug=False, pin=None, port='/dev/ttyS0') Connecting to GSM modem on /dev/ttyS0...

== MODEM INFORMATION ==

Manufacturer: WAVECOM WIRELESS CPU Model: MULTIBAND 900E 1800 Revision: R74_00gg.FXT002 2120060 041709 19:18

IMEI: 354663030007998 IMSI: 641109878331782

Network: MTN-UGANDA Signal strength: 23

Hope the above helps.

havefunworkhard commented 8 years ago

Python 3.x Seems like self.serial.read(1) in serial_comms.py returns bytes in Python 3.x. But code expects strings and compares returned value with strings like RX_EOL_SEQ = '\r\n' or ''. The comparison result will always be False. In this case SerialComms will never _handleLineRead and TimeoutException will be raised.

MulajEgzon commented 6 years ago

Send Command "AT+CMEE=2" to change the way error is shown, From number to words returns "OK" as you can see below nr "58" is "invalid command line"

sent: AT+IPR? read :AT+IPR?

+CME ERROR:invalid command line