Skip to content

Instantly share code, notes, and snippets.

@HarHar
Created November 18, 2012 22:31
Show Gist options
  • Save HarHar/4107897 to your computer and use it in GitHub Desktop.
Save HarHar/4107897 to your computer and use it in GitHub Desktop.
Cellphone control
import serial
from sys import argv
from time import sleep
from select import select
try:
filename, device = argv
except:
print 'Usage: ' + argv[0] + ' [device]'
exit()
def readSerial(se):
ret = ''
select([se], [], [])
while se.inWaiting() != 0:
ret += se.read()
ret = ret.replace('\r\n\r\n', '\n')
ret = ret.lstrip('\r\n').rstrip('\r\n')
return ret
def dial(se, number):
se.write('ATDT' + number + '\r\n')
return readSerial(se)
def getManufactor(se):
se.write('AT+GMI\r\n')
return readSerial(se)
def getModel(se):
se.write('AT+GMM\r\n')
return readSerial(se)
def getRevision(se):
se.write('AT+GMR\r\n')
return readSerial(se)
def getIMEI(se):
se.write('AT+GSN\r\n')
return readSerial(se)
def getOperators(se):
se.write('AT+COPN\r\n')
return readSerial(se)
def getSignal(se):
se.write('AT+CSQ\r\n')
return readSerial(se)
def writeMessage(se, number, body):
se.write('AT+CMGF=1\r\n')
ret = readSerial(se) + '\n'
sleep(1)
se.write('AT+CMGW="' + number + '",129,"REC UNREAD"\r\n')
ret += readSerial(se)
sleep(2)
se.write(body + chr(26))
return ret + '\n' + readSerial(se)
def getMessages(se):
se.write('AT+CMGF=1\r\n')
readSerial(se)
se.write('AT+CMGL="ALL"\r\n')
return readSerial(se)
if __name__ == '__main__':
s = serial.Serial(port=device, baudrate=9600)
while True:
try:
cmd = raw_input('>> ').strip('\n').strip('\r')
except:
print 'bye'
exit()
scmd = cmd.split(' ')
if scmd[0] == 'manufactor':
print getManufactor(s)
elif scmd[0] == 'model':
print getModel(s)
elif scmd[0] == 'rev':
print getRevision(s)
elif scmd[0] == 'imei':
print getIMEI(s)
elif scmd[0] == 'dial':
if len(scmd) > 1:
print dial(s, scmd[1])
else:
print 'Usage: dial [number]'
continue
elif scmd[0] == 'smsg' or scmd[0] == 'storemsg' or scmd[0] == 'storemessage':
nmb = raw_input('Number> ').strip('\r').strip('\n')
body = raw_input('Body> ').strip('\r').strip('\n')
print writeMessage(s, nmb, body)
elif scmd[0] == 'rmsg' or scmd[0] == 'rmsgs' or scmd[0] == 'readmsg' or scmd[0] == 'readmsgs' or scmd[0] == 'readmessages' or scmd[0] == 'readmessage':
print getMessages(s)
elif scmd[0] == 'raw':
s.write(cmd.lstrip('raw ') + '\r\n')
print readSerial(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment