I have a DWM-156 GSM modem. Below you can see the list of devices that added to my computer after plugging this GSM modem to the USB port:
Note that the every time that I connect the modem to my computer, it use different COM port numbers.
Now I want to send some AT commands to this modem using Python or any other language. Actually I want to answer/make a call from/to a dial up phone and record the raw data that transfers during that communication. After doing a search I found this question in SO. One of the answerers was suggested the below code:
import serial
serialPort = serial.Serial(port=PORT_NUMBER,baudrate=115200,timeout=0,rtscts=0,xonxoff=0)
def sendatcmd(cmd):
serialPort.write('at'+cmd+'\r')
print 'Loading profile...',
sendatcmd('+npsda=0,2')
I replace PORT_NUMBER with 9 , 10 and 12. These are results:
>>> ================================ RESTART ================================
>>>
Loading profile...
>>> #for port = 9
>>> ================================ RESTART ================================
>>>
Loading profile...
>>> #for port = 10
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\Users\ghasemi.IT\Desktop\testGSMModem.py", line 3, in <module>
serialPort = serial.Serial(port=12,baudrate=115200,timeout=0,rtscts=0,xonxoff=0)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
SerialException: could not open port 'COM13': WindowsError(2, 'The system cannot find the file specified.')
>>> #for port = 12
>>>
My questions:
- While I don't receive any response?
- Why in the third program it throw could not open port 'COM13' while I am trying to connect to COM12?
- Is there any more efficient and better way to use GSM modem to sniff a call? (I want to call to the SIM card that I inserted in my GSM modem using a dial up phone set and log the raw data that transfers during this communication.)
You never call
serialPort.read()
to read any response.The
Serial
class comes fromlib\site-packages\serial\serialwin32.py
, and is a wrapper around theWin32Serial
class.Win32Serial
inherits fromSerialBase
inlib\site-packages\serial\serialutil.py
, and initialises itself with a call to theSerialBase
initialiser.SerialBase
sets its port value by assigning the port value you gave to theport
property, which callsSerialBase.setPort(port)
.setPort(port)
checks whether the value passed in is a string or a number. It's a number, so it callsmakeDeviceName(port)
in theWin32Serial
class.makeDeviceName(port)
callsdevice(port)
.device(port)
is a function which adds 1 to a number and putsCOM
in front of it:Why? I don't know. But if you pass a string instead, it won't change it. Try giving it a string instead:
serial.Serial(port="COM12"...
instead ofserial.Serial(port=12
- but I suspect this is background distraction, and it's related to it being described in the device manager screenshot as a 'debug' port and maybe it can't be opened normally.I don't understand this. What does it mean to "call a SIM card"? What raw data? You aren't going to be able to record phonecall audio data using a serial link... and you probably aren't going to be able to record modem data sent by something else because Python will have the serial port open and the other program won't be able to open it.