-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsms.py
102 lines (91 loc) · 3.27 KB
/
sms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
"""
sms.py - Used to send txt messages.
"""
import config
import serial
from datetime import datetime
class Sms(object):
def __init__(self, recipient=config.recipient, message=config.message):
self.logfile = open("modem.log","w")
self.open()
self.recipient = recipient
self.content = message
def open(self):
self.logfile.write(str(datetime.now()))
self.logfile.write('open serial\n')
self.ser = serial.Serial(config.serial, 115200, timeout=5)
self.SendCommand('ATZ\r')
self.logfile.write(str(datetime.now()))
self.logfile.write('send ATZ\n')
self.SendCommand('AT+CMGF=1\r')
self.logfile.write(str(datetime.now()))
self.logfile.write('send ATZ\n')
def setRecipient(self, number):
self.recipient = number
def setContent(self, message):
self.content = message
def send(self):
self.ser.flushInput()
self.ser.flushOutput()
self.logfile.write(str(datetime.now()))
self.logfile.write('send CMGS\n')
command = '''AT+CMGS="''' + self.recipient.encode() + '''"\r\r'''
self.SendCommand(command,getline=True)
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 1 CMGS\n')
#data = self.ser.readall()
#print data
self.logfile.write(str(datetime.now()))
self.logfile.write('send CMGS\n')
command = self.content.encode() + "\r\r"
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 2 CMGS\n')
self.SendCommand(command,getline=True)
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 3 CMGS\n')
data = self.ser.readall()
print data
command = chr(26)
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 4 CMGS\n')
self.SendCommand(command,getline=True)
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 5 CMGS\n')
data = self.ser.readall()
print data
self.logfile.write(str(datetime.now()))
self.logfile.write('after send 6 CMGS\n')
def disconnect(self):
self.ser.close()
def SendCommand(self,command, getline=True):
self.ser.write(command)
data = ''
if getline:
data=self.ReadLine()
return data
def ReadLine(self):
data = self.ser.readline()
print data
return data
def unread(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL="REC UNREAD"\r\n'#gets incoming sms that has not been read
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def read(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL="REC READ"\r\n'#gets incoming sms that has not been read
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data
def all(self):
self.ser.flushInput()
self.ser.flushOutput()
command = 'AT+CMGL="ALL"\r\n'#gets incoming sms that has not been read
self.SendCommand(command,getline=True)
data = self.ser.readall()
print data