1.5.4. fejezet, SMS küldése

SMS küldése soros portra csatlakoztatott mobiltelefonnal. A kód futtatásához a pySerial modul szükséges.

fsms.py

# -*- coding: iso8859_2 -*-
def Cserebere(cserelendo):
  iH=divmod(len(cserelendo),2)
  if iH[1]>0:
    cserelendo += 'F'
    cx=iH[0]+1
  else:
    cx=iH[0]
  sUj = ''
  for i in range(0,cx):
    sUj += cserelendo[i*2+1]
    sUj += cserelendo[i*2]
  if ((len(sUj)>0) and (sUj[len(sUj)-1] == 'F')):
    sUj = sUj[:len(sUj)-1]
  return sUj
 
def HexaWordToInt(hexBem):
  return eval('0x'+hexBem)
 
def ByteToBin(byteBem):
  s='';
  if (byteBem == 0): s = '0'
  else:
    while 0 < byteBem:
        s = str(byteBem%2) + s
        byteBem /= 2
  while len(s) < 8:
  	s = '0'+s;
  return s;
 
def BinToInt(sBinBem):
  iEredm = 0;
  j = 1;
  for i in reversed(range(0,len(sBinBem))):
      if sBinBem[i] == '1':
        iEredm += j;
      j *= 2;
  return iEredm
 
def BinByteToHex(binBem):
  return "%.2x"%BinToInt(binBem)
 
def Ekezettelenit(sEkezetes):
  result = sEkezetes
  result = result.replace('á','a')
  result = result.replace('Á','A')
  result = result.replace('é','e')
  result = result.replace('í','i')
  result = result.replace('Í','I')
  result = result.replace('ú','u')
  result = result.replace('Ú','U')
  result = result.replace('ü','u')
  result = result.replace('Ü','U')
  result = result.replace('ű','u')
  result = result.replace('Ű','U')
  result = result.replace('ó','o')
  result = result.replace('Ó','O')
  result = result.replace('ö','o')
  result = result.replace('Ö','O')
  result = result.replace('ő','o')
  result = result.replace('Ő','O')
  return result

7bitsms.py

# -*- coding: iso8859_2 -*-
import fsms,datetime,time
import serial
 
def SMS7BitToString(sSMSbem):
  sT = sSMSbem
  iHossz = fsms.HexaWordToInt(sT[:2])
  sT = sT[4:]
  sT = sT[int((iHossz*7) / 4):]
 
  if (sT[:2] != '04'):
      sUz = 'Hibásan kiolvasott SMS.. (1)'
      return ''
 
  sT = sT[2:]
  iHossz = fsms.HexaWordToInt(sT[:2])
  sT = sT[2:]
 
  if (sT[:2] == '91'):
    sOrgTel = '+'
  else:
    sOrgTel = ''
 
  sT = sT[2:]
 
  sOrgTel = sOrgTel + fsms.Cserebere(sT[:2* int((iHossz+1) / 2)])
 
  sT = sT[2* int((iHossz+1) / 2):]
  sT = sT[4:]
  iYear = int(fsms.Cserebere(sT[:2]))
  if (iYear < 80):
    iYear = iYear + 2000
  else:
    iYear = iYear + 1900
 
  iMonth = int(fsms.Cserebere(sT[2:4]))
  iDay = int(fsms.Cserebere(sT[4:6]))
  iHour = int(fsms.Cserebere(sT[6:8]))
  iMinute = int(fsms.Cserebere(sT[8:10]))
  iSecond = int(fsms.Cserebere(sT[10:12]))
  iMilliSec = int(fsms.Cserebere(sT[12:14]))
  try:
     dIdopont=datetime.datetime(iYear,iMonth,iDay,iHour,iMinute,iSecond,iMilliSec)
  except:
     dIdopont = time.time()
  sT= sT[14:]
  iHossz = fsms.HexaWordToInt(sT[:2])
  sT = sT[2:]
  #most jön az üzenet dekodolása
  sBinData = ""
  for i in range(1, int(len(sT)/2)+1):
    sBinData = fsms.ByteToBin(fsms.HexaWordToInt(sT[2*i-2:2*i])) + sBinData
  iHossz2 = len(sBinData)
  sT = ''
  for i in range(1, int(iHossz2/7)+1):
    sT = sT + "%c"%fsms.BinToInt(sBinData[iHossz2-i*7:(iHossz2-i*7)+7])
  sUz = sT
  if (iHossz != int(iHossz2/7)):
    sUz = sUz + " [A kódolt üzenet-hossz nem egyenlő a számítottal!]"
  res = {'uzenet':sUz,'idopont':dIdopont,'telefonsz':sOrgTel}
  return res
 
 
def StringToSMS7Bit(StringBem):
  Nullak="00000000"
  BinData=""
  HexData=""
  for i in reversed(range(0,len(StringBem))) :
     BinData = BinData+(fsms.ByteToBin(ord(StringBem[i]))[1:8])
  kiegesz=len(BinData)-(int(len(BinData)/8)*8)
  if (kiegesz!=0):
     BinData=Nullak[:(8-kiegesz)]+BinData
  for i in reversed(range(0,int(len(BinData)/8))):
     HexData=HexData+fsms.BinByteToHex(BinData[i*8:(i*8)+8])
  return HexData.upper()
 
def openComPort():
  ser.open()
  if ser.isOpen():
    print("Port opened...")
    ser.write("ATZ%c"%13)
    print ser.read(50)
 
def Write2ComPort(cmd):
  ser.write(cmd)
  print ser.read(50)
 
def SMSKuldes7Bit(ForrasTelSzam, CelTelSzam, SMSUzenet):
  AtParancs = "AT+CMGS=%s%c"%(int((len(CelTelSzam+SMSUzenet)+16)/2),13)
  print AtParancs
  Write2ComPort(AtParancs)                             #0010A7 instant message (aktívan megjelenik a telefon kijelzőn)
  SMSAdat = '0791'+ForrasTelSzam+'11000B91'+CelTelSzam+'0000A7'+("%.2x"%int((len(SMSUzenet)*4) / 7)).upper()+SMSUzenet+"%c"%26
  print SMSAdat
  # wait until '>' sign readed from comport
  Write2ComPort(SMSAdat)
 
mit = 'szervusz filibus'
mit = fsms.Ekezettelenit(mit)
hova='36301234567'
honnan='36309888000' #Westel SMS center
#honnan='36209300099' #Pannon SMS center
 
ser = serial.Serial(port=39, timeout=4) #Prolific usb-to-serial Comm Port
openComPort()
SMSKuldes7Bit(fsms.Cserebere(honnan), fsms.Cserebere(hova), StringToSMS7Bit(mit))
print ser.read(50)
ser.close()