Utterly Random Third-Generation Mobile Python Hacks

Today I got fundamentally fed up with my HSDPA setup and, since I lacked the time to track down and set up a non-beta version of the controlling software (I vowed not to mess with my Windows laptop until April, and I'm sticking to it), I whipped up two quick Python scripts that use pyserial to initialize the data card and to monitor the signal level.

The 3G card I'm using follows the usual convention of having two virtual serial ports (one for data and one for monitoring), so the first script talks to the data port and sets the SIM PIN number and APN:

import serial
s = serial.Serial('COM4:', 38400,  rtscts=0, xonxoff=0)

...and the second script talks to the monitoring port (which is active even during a mobile connection) and grabs the signal level/quality info:

import serial,re
s = serial.Serial('COM5:', 38400,  rtscts=0, xonxoff=0, timeout=1)
m = re.search("CSQ: (\d+),(\d+)",s.read(64).replace("\r\n"," "))
print int(m.group(1))*100/31,int(m.group(2))*100/7

Both scripts are equally applicable to Linux or Mac OS X, provided you know which port(s) to talk to.