Skip to content

Instantly share code, notes, and snippets.

@asakasinsky
Created April 22, 2014 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asakasinsky/11178384 to your computer and use it in GitHub Desktop.
Save asakasinsky/11178384 to your computer and use it in GitHub Desktop.
Python script to send sms via Siemens CX65 with Prolific PL2303 USB-to-Serial port adapter. Source: http://habrahabr.ru/post/58348/
#!/usr/bin/python
# COM-port settings
# Add this string
# KERNEL=="ttyUSB[0-9]", RUN+="/bin/stty -F /dev/%k speed 9600 -brkint -icrnl ixoff -imaxbel -opost -onlcr -isig -icanon -echo -echoe"
# in /etc/udev/rules.d/50-udev.rules
import os
import sys
import time
def dectobin(i):
b = ''
while i > 0:
j = i & 1
b = str(j) + b
i >>= 1
return b
def SendSMS(dev,number,text):
pdu_data = '00'
pdu_data += '11' #SMS-SUBMIT
pdu_data += '00' #TP-Message-Reference
pdu_data += '0B' #Address-Length
pdu_data += '91' #Address-Length
'''Convert telephone number'''
number += 'F'
pdu_number = ''
for i in range(0,len(number)):
if i%2==0:
continue
pdu_number += number[i] + number[i-1]
pdu_data += pdu_number
pdu_data += '00' #TP-Protocol identifier
pdu_data += '00' #TP-Data coding scheme
pdu_data += 'AA' #TP-Validity-Period
'''Convert text to binary format'''
pdu_text_bin = []
for i in text:
dec_s=ord(i)
if dec_s == 95:
dec_s = 17
if dec_s == 94:
dec_s = 1
if dec_s == 64:
dec_s = 0
if dec_s == 36:
dec_s = 2
if dec_s == 123:
dec_s = 40
if dec_s == 125:
dec_s = 41
if dec_s == 124:
dec_s = 64
if dec_s == 126:
dec_s = 61
if dec_s == 92:
dec_s = 47
if dec_s == 91:
dec_s = 60
if dec_s == 93:
dec_s = 62
bin = dectobin(dec_s)
le = len(bin)
while le<7:
bin='0'+bin
le = len(bin)
pdu_text_bin.append(bin)
'''Encode binary to PDU format'''
pdu_text_bin_cp = []
n=0
for i in range(0,len(text)):
if (i>0) & ((i+1)%8==0):
continue
n+=1
if n==8:
n=1
if i==len(text)-1:
cp = pdu_text_bin[i][0:8-n]
else:
cp = str(pdu_text_bin[i+1][7-n:7] + pdu_text_bin[i])[0:8]
pdu_text_bin_cp.append(cp)
'''Convert PDU to hex'''
pdu_text=''
for i in pdu_text_bin_cp:
hexi = str(hex(int(i,2)))[2:4].upper()
if len(hexi) == 1:
hexi = '0' + str(hexi)
pdu_text += hexi
'''Calculate text length'''
len_hex = hex(len(text))[2:4].upper()
if len(len_hex) == 1:
len_hex = '0' + str(len_hex)
'''Calculate PDU length'''
pdu_data+=len_hex+pdu_text
pdu_len = str(len(pdu_data)/2-1)
if True:
fd = os.open(dev, os.O_RDWR)
os.write(fd, "AT+CMGF=0 \015")
time.sleep(1)
os.write(fd, "AT+CMGS=" + pdu_len + "\015")
time.sleep(1)
os.write(fd, pdu_data + "\032")
os.close(fd)
def main(argv):
SendSMS("/dev/ttyUSB0",argv[1],argv[2] + '\r\n' + argv[3])
return 0
if __name__ == '__main__': main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment