# The following while loop keeps restarting the program if there is an error
# It will stop if errors occur too frequently

FREQ_LIMIT = 4     # limit on frequency of errors in seconds

START = 0

DELTA = FREQ_LIMIT  # done so first error doesn't count towards limit

while DELTA >= FREQ_LIMIT:
    try:
        Setup.setup()
        try:
            Setup.serial_setup()
        except SerialException:
            print "serial error"
            time.sleep(2)
            if var.serial_start == 0:
                var.serial_start = 2
            elif var.serial_start ==2:
                var.serial_start = 0
            Setup.closedown()
            Setup.serial_setup()
        SCHED = Scheduler()
        SCHED.new(server(var.port))
        SCHED.mainloop()
        
    except StandardError:
        print "standard error"