Communicating with GSM modems using PySerial in python

3.6k views Asked by At

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:

enter image description here 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:

  1. While I don't receive any response?
  2. Why in the third program it throw could not open port 'COM13' while I am trying to connect to COM12?
  3. 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.)
1

There are 1 answers

4
TessellatingHeckler On
  1. While I don't receive any response?

You never call serialPort.read() to read any response.

  1. Why in the third program it throw could not open port 'COM13' while I am trying to connect to COM12?

The Serial class comes from lib\site-packages\serial\serialwin32.py, and is a wrapper around the Win32Serial class.

Win32Serial inherits from SerialBase in lib\site-packages\serial\serialutil.py, and initialises itself with a call to the SerialBase initialiser.

SerialBase sets its port value by assigning the port value you gave to the port property, which calls SerialBase.setPort(port).

setPort(port) checks whether the value passed in is a string or a number. It's a number, so it calls makeDeviceName(port) in the Win32Serial class.

makeDeviceName(port) calls device(port).

device(port) is a function which adds 1 to a number and puts COM in front of it:

def device(portnum):
    """Turn a port number into a device name"""
    return 'COM%d' % (portnum+1) # numbers are transformed to a string

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 of serial.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.

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

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.