summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/Old/15-07-2011/gsmincom_.py
blob: 413db2599fb62f4fdbe5167bc535878cc1d807fd (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

import atexit
import signal

import sys
import socket
import time
import os
import string
import classServer
from time import sleep
from serial import *


success = None
stop	= False

try:

	global ser
	global server
	portAddress = '/dev/ttyUSB0'
	portName = portAddress[-4:]
	portExist = os.popen('dmesg | grep ' + portName).read()

	if portExist == '':
		print 'The serial port does not exist'
		sys.exit()


	
	server = classServer.ServerHandler(50105) 
	tried = server.openSocket()

	if server.error != 'No error':
		print server.error
		if server.error == '[Errno 98] Address already in use':
			print 'one should try to kill the port'
			print server.killPort()
			server.closeConection()

			
	print "here:", server.connected
	if server.connected == 1:

		server.sendData('ready')
		server.closeConnection()

		ser = Serial(
			port=portAddress,
			baudrate=19200,
			bytesize=EIGHTBITS,
			parity=PARITY_NONE,
			stopbits=STOPBITS_ONE)

		ser.close()
		ser.open()

		signalQuality = ser.write('AT+CSQ\r')
		print "signal Quality ", signalQuality
		print ""
		
		

    		while stop <> True:
			
			global pickUp
			pickUp = None
			buffer = ""
			

			buffer = buffer + ser.read(ser.inWaiting()) #read the serial port and add it to the buffer variable
			if '\n' in buffer:
				 #if a new line character is found in the buffer then the cellphone has sent something
		    		lines = buffer.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)
				print "apa iyi", buffer
		    		#buffer = "".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), '')
			
			   	print "buffer:", buffer
			   	print "line:", line
				linesa = ser.read(ser.inWaiting())
				print "new line:", linesa
				sleep(1)

			   	if line =="RING" or linesa =="RING": 
				

			   		if line =="RING" or lines =="RING":  
				
						print "Somebody calling"
						print ""
						sleep(1)
						ser.write('ATA\r')  

					
						print "I will hangup the call"
						print ""
						sleep(1)

						ser.write('AT+CHUP\r') 
						pickUp = 0
						ser.close()
	
						print "here I am"
						tried = server.openSocket()

						sleep(1)
						server.sendData('success')
						stop = True
						sleep (5)
						break
						


		server.closeConnection()
		del server
	else:

		print "not connected"

		del server

	ser.close()
except ValueError:
    print "Exception: "
    server.closeConnection()
    del server

print "GOODBYE"
sleep(5)