Advertisement
Guest User

try to read gpsfake

a guest
Feb 23rd, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. from twisted.application import internet, service
  2. from twisted.web import server
  3. from twisted.internet import ssl
  4.  
  5. from twisted.internet import reactor
  6. from twisted.internet.serialport import SerialPort
  7. from twisted.protocols.basic import LineReceiver
  8.  
  9. import operator
  10. from twisted.python.compat import reduce
  11.  
  12. import traceback
  13. import string
  14. import time
  15. import jsonrpclib
  16. import json
  17.  
  18. from math import *
  19.  
  20.  
  21. lastgpsloc = 'empty'
  22. lastrpt = time.time()
  23.  
  24.  
  25. class gpsReceiver(LineReceiver):
  26.     def __init__(self):
  27.         print 'debug : gpsreceiver init'
  28.  
  29.     def gpsValid(self, line):
  30.         if not line.startswith('$GPRMC'):
  31.             return False
  32.         strmessage, checksum = line[1:].strip().split('*')
  33.         message = strmessage.split(',')
  34.         checksum, calculated_checksum = int(checksum, 16), reduce(operator.xor, map(ord, strmessage))
  35.         if checksum != calculated_checksum:
  36.             return False
  37.         return True
  38.  
  39.     def lineReceived(self, line):
  40.         print 'GPS RECEIVED !'
  41.         global lastrpt
  42.         global lastgpsloc
  43.         if self.gpsValid(line) :
  44.             lastgpsloc = line
  45.             print lastgpsloc
  46.             if time.time() - lastrpt >= 1 :
  47.                 print 'TIME TO REPORT'
  48.         else :
  49.             print 'Invalid GPS -> %s' % (line)
  50.  
  51.  
  52. class SerialService(service.Service):
  53.     def startService(self):
  54.         print 'debug : Serial Service start'
  55.         try:
  56.             self.gpsport = gpsport
  57.         except :
  58.             print 'GPSport failed'
  59.  
  60.  
  61. #Create Application
  62. application = service.Application("Serial MultiService Example")
  63.  
  64. #Seting Serial
  65. gpsport = SerialPort(gpsReceiver(), '/dev/pts/4', reactor,baudrate=4800)
  66.  
  67. #Create serialService
  68. #as told at http://www.mentby.com/lucas-taylor/serialport-protocol-as-a-service.html
  69. serialService = SerialService()
  70.  
  71. #Creating Multi Service
  72. multiService = service.MultiService()
  73. #Add our Services to multiservice
  74. serialService.setServiceParent(multiService)
  75.  
  76. #Set created application to be serviceparent of multiservice
  77. multiService.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement