1 2 import thread, socket 3 4 class BTGPSPoller(object): 5 def __init__(self, address): 6 address, services = socket.bt_discover(address) 7 self.target = (address, services.values()[0]) 8 self.active = True 9 self.connected = False 10 self.lock = thread.allocate_lock() 11 self.sentances = list() 12 13 def connect(self): 14 if not self.connected: 15 thread.start_new_thread(self.run, ()) 16 17 def run(self): 18 print "BTGPSPoller thread activated" 19 try: 20 conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM) 21 conn.connect(self.target) 22 self.connected = True 23 except: 24 print "Unable to connect" 25 26 if self.connected: 27 try: 28 to_gps = conn.makefile("r", 0) 29 except: 30 print "Failure calling conn.makefile()" 31 while self.active: 32 #e32.ao_sleep(1) 33 msg = None 34 try: 35 msg = to_gps.readline() 36 if not msg == None and msg.startswith("$GPGGA"): 37 gps_data = msg.split(",") 38 if not gps_data[2] == "": 39 self.lock.acquire() 40 self.sentances.append(msg) 41 if len(self.sentances) > 10: 42 self.sentances.pop(0) 43 self.lock.release() 44 except: 45 self.active = False 46 47 try: 48 to_gps.close() 49 conn.close() 50 self.connected = False 51 print "Closed and disconnected" 52 except: 53 self.connected = False 54 print "Unable to close" 55 56 def disconnect(self): 57 self.active = False 58 print "Disconnecting from GPS" 59 60 def getSentances(self): 61 self.lock.acquire() 62 l = self.sentances[:] 63 self.lock.release() 64 return l
You need to create an account or log in to post comments to this site.