#!/usr/bin/python import socket, struct import time import math import random, re, thread class DummyNMEAServer: """ This is a fake NMEA Sentence server. """ HOST = '' PORT = 12345 def __init__(self): self.socket = socket.socket() self.c = None self.stop = False def stop_listener(self): while True: s = self.c.recv(1024) try: if s=="stop": self.stop=True break except socket.error: pass def checksum(self, sentence): """ Source: http://code.activestate.com/recipes/576789-nmea-sentence-checksum/ """ if re.search("\n$", sentence): sentence = sentence[:-1] calc_cksum = 0 for s in sentence: calc_cksum ^= ord(s) """ Return the nmeadata, the checksum from sentence, and the calculated checksum """ return format(calc_cksum, 'x') def run(self): """ Sends dummy location info of coordinates moving around (0, 0) in circular pattern """ s = self.socket s.bind((DummyNMEAServer.HOST, DummyNMEAServer.PORT)) s.listen(5) while True: c, addr = s.accept() self.c = c print 'Got connection from', addr lat = lon = 0 cy = cx = 0.0 r = 4.3 t = 0 latS = lonS = "" # thread.start_new_thread (self.stop_listener, ()) while True: for i in range(360): lat = cx + math.cos(i / 180.0 * math.pi) * r lon = cy + math.sin(i / 180.0 * math.pi) * r t = time.strftime("%H%M%S", time.gmtime(time.time())) latS = 'N' if lat >= 0 else 'S' lonS = 'E' if lon >= 0 else 'W' print t gga = "$GPGGA,%s,%02d%06.3f,%s,%03d%06.3f,%s,1,08,0.9,545.4,M,46.9,M,," % ( t, abs(lat), abs((lat-int(lat))*60), latS, abs(lon), abs((lon-int(lon))*60), lonS) gga = gga + "*" + str(self.checksum(gga)) size = struct.pack("!L",len(gga)) c.send(size) print(len(gga)) print gga c.send(gga) time.sleep(random.randint(1,10)) c.close() app = DummyNMEAServer(); app.run();