summaryrefslogtreecommitdiffstats
path: root/Code/Box1-Code/GSMClass.py
diff options
context:
space:
mode:
Diffstat (limited to 'Code/Box1-Code/GSMClass.py')
-rw-r--r--Code/Box1-Code/GSMClass.py310
1 files changed, 310 insertions, 0 deletions
diff --git a/Code/Box1-Code/GSMClass.py b/Code/Box1-Code/GSMClass.py
new file mode 100644
index 0000000..b932ab5
--- /dev/null
+++ b/Code/Box1-Code/GSMClass.py
@@ -0,0 +1,310 @@
+from serial import * #serial port library
+import string
+import sys
+import signal
+
+class TimeoutException(Exception):
+ pass
+
+class serialPort():
+
+ def __init__(self, portAddress, baudRate, timeout):
+ self.portAddress = portAddress
+ self.portExist = 0
+ self.ser = 0
+ self.baudRate = baudRate
+ self.signalStrength = 0
+ #self.callerConnected = -1
+ self.timer = timeout
+ self.timer1 = timeout
+ self.start = 0
+
+ self.debugMode = 0
+
+ def portInit(self, timeout1):
+ portName = self.portAddress[-4:]
+ portExist = os.popen('dmesg | grep ' + portName).read()
+ self.timer = timeout1
+ if portExist == '':
+ return 0
+ else:
+ try:
+ self.ser = Serial(
+ port=self.portAddress,
+ baudrate=self.baudRate,
+ bytesize=EIGHTBITS,
+ parity=PARITY_NONE,
+ stopbits=STOPBITS_ONE
+ #timeout=0,
+ #writeTimeout=0
+ #xonxoff=0,
+ #rtscts=0
+ #interCharTimeout=None
+ #I didn't need to set these variables :)
+ )
+ self.ser.flushOutput() #clean the output buffer from the serial port
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.write('AT\r')
+
+ received = self.__receiveData()
+ self.start=1
+ if received == 'OK':
+ self.portExist = 1
+ return 1 #cellphone is OK, receives commands
+ elif received == 'TIMEOUT':
+ return 'TIMEOUT'
+ else:
+ return 2 #cellphone has an error
+
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+
+ self.portExist = 3
+ return 3
+
+ def callNumber(self,numberToCall):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.write('ATD'+str(numberToCall)+';\r')
+ if self.__receiveData() == 'OK':
+ return 1
+ else:
+ return 4
+
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+
+ return 3
+ else:
+ return 0
+
+
+ def hangUp(self):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.write('AT+CHUP\r')
+ received = self.__receiveData()
+ if received == 'OK':
+ return 1
+ elif received == 'ERROR':
+ return 2 #other side hang up the call
+ else:
+ return 4
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+ return 3
+ else:
+ return 0
+
+
+ def closePort(self):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.close()
+ return 1
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+ return 3
+ else:
+ return 0
+
+ def getSignalQuality(self):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.write('AT+CSQ\r')
+ if self.__receiveData() == 'OK':
+ if self.__receiveData == 'SIGNAL':
+ return 1
+ else:
+ return 4
+ else:
+ return 2
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+ return 3
+
+ else:
+ return 0
+
+ def receiveCall(self):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ if self.__receiveData() == 'RING':
+ self.ser.write('ATA\r')
+ if self.__receiveData()=='OK':
+ return 1
+ else:
+ return 4
+ else:
+ return 2
+
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+ return 3
+ else:
+ return 0
+
+ def currentCall(self):
+ if self.portExist == 1:
+ try:
+ self.ser.flushInput() #clean the input buffer for serial port
+ self.ser.write('AT+CLCC\r')
+ received = self.__receiveData()
+ if received == 'OK':
+ return 2 #not yet in a call
+ elif received == 'ACTIVE':
+ return 1 #in a call
+ elif received == 'HELD':
+ return 5 #held call
+ elif received == 'DIALING':
+ return 6 #dialing
+ elif received == 'ALERTING':
+ return 7 #alerting the call
+ elif received == 'INCOMING':
+ return 8 #incoming call
+ elif received == 'WAITING':
+ return 9 #waiting for a call
+
+ else:
+ return received #in some other state
+ except Exception, e:
+ import traceback
+ if self.debugMode == 1:
+ print e
+ print traceback.format_exc()
+ return 3
+ else:
+ return 0
+
+ def __receiveData(self):
+ def timeout_handler(signum, frame):
+ raise TimeoutException()
+ if self.start == 0:
+ self.timer = 5
+ else:
+ self.timer = self.timer1
+
+ old_handler = signal.signal(signal.SIGALRM, timeout_handler)
+ signal.alarm(self.timer)
+
+ bufferData = ''
+ lines = ''
+ line = ''
+
+ try:
+ while True:
+ bufferData = bufferData + self.ser.read(self.ser.inWaiting()) #read the serial port and add it to the buffer variable
+ if '\n' in bufferData: #if a new line character is found in the buffer then the cellphone has sent something
+ lines = bufferData.split('\n') #parse the buffer variable with the new line character
+ last_received = lines.pop(0) #put into last_received variable the first content from lines (FIFO)
+
+ bufferData = '\n'.join(lines) #add a new line to the buffer variable
+
+ last_received=last_received.split('\n') #parse the last received value with new lines
+ line = last_received[0].replace(chr(13), '') #remove \r from the first parsed value in last_received and return me a nicely parsed value :)
+ if self.debugMode == 1:
+ if len(line) > 0:
+ print line
+
+ if line == 'OK':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'OK'
+
+ elif line == 'ERROR':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'ERROR'
+
+ elif line[0:11] == '+CME ERROR:':
+ if self.debugMode == 1:
+ print 'ERROR:', line
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'ERROR'
+
+ elif line == 'RING':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'RING'
+
+ elif line[0:5] == '+CSQ:': #+CSQ:
+ space = int(string.find(line,' '))+1 #find the (space) sign
+ coma = int(string.find(line,',')) #find the , (coma) sign
+ self.signalStrength = (int(line[space:coma])*2)-113
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'SIGNAL'
+
+ elif line == 'NO CARRIER':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'NO CARRIER'
+
+ elif line == 'BUSY':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'BUSY'
+
+ elif line[0:6] == '+CLCC:':
+ #+CLCC: 1,0,
+ #self.ser.flushInput()
+ if line[11:12] == '0':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'ACTIVE'
+ elif line[11:12] == '1':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'HELD'
+ elif line[11:12] == '2':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'DIALING'
+ elif line[11:12] == '3':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'ALERTING'
+ elif line[11:12] == '4':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'INCOMING'
+ elif line[11:12] == '5':
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'WAITING'
+
+ except TimeoutException:
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ return 'TIMEOUT'
+
+ except Exception, e:
+ signal.signal(signal.SIGALRM, old_handler)
+ signal.alarm(0)
+ print 'NO GOOD'
+ print e
+ return 3