Ejemplo n.º 1
0
def send_msg(s, msg, timeout, retries, ack):
    buff = ""
    for i in range(retries):
        s.write(msg)
        Logger.log_write("Sending out '{0}' to arduino, retry {1}".format(msg, i))
        s.flush()
        start_time = time.time()
        end_time = time.time()
        while end_time - start_time < timeout:
            r = s.readline()
            Logger.log_write(
                "Got back '{0}' from arduino, time left till timeout: {1:f}s".format(
                    r, timeout - time.time() + start_time
                )
            )
            buff += r
            if r:
                print 'Got msg "{0}" from upstream'.format(r)
                if "FAIL" in r:
                    print "Found failure condition in response, resending"
                    Logger.log_write("Matched failure condition, resending")
                    break

                if ack in buff:
                    # print 'Found ACK condition in response, accepting'
                    Logger.log_write("Found ACK condition, accepting")
                    break
            end_time = time.time()
        if ack in buff:
            return True
    else:
        Logger.log_write("no msg was ack'd, giving up")
        return False
Ejemplo n.º 2
0
def msg_sender(pipe, avail, port, rate, timeout, retries):
    # establish serial connection
    try:
        s = serial.Serial(port, rate, timeout=timeout)
        print "Established connection, commencing initial wait for handshakes(5s)"
        time.sleep(5)
        print "Connection should be up now, testing with ready"
        if not ready_waiter(s, avail, timeout):
            print "Arduino not ready, what is going on??"
            print "Waiting another 10 seconds"
            time.sleep(10)
            if not ready_waiter(s, avail, timeout):
                raise Exception("Arduino down for good.")

    except IOError:
        print "Could not open port. Is Arudino running?"

    # we should be fine now
    while True:
        t, msg = pipe.recv()
        if "HEARTBEAT" in msg:
            # special case, try to update avail value if it's not available
            if avail.value == 0:
                ready_waiter(s, avail, timeout, retries=10)
                if avail.value == 1:
                    avail.value = 0
                    time.sleep(1.0 / 10.0)
                    avail.value = 1
            continue

        if "TERMINATE" in msg:
            print "exitting..."
            return
        if time.time() - t > timeout * 4:
            print "Got msg {0} which is {1:.2f}s old, rejecting".format(msg, time.time() - t)
            Logger.log_write("Rejected msg '{0}' as it was {1:.2f}s old".format(msg, time.time() - t))
            continue
        # print 'Trying to send msg "{0}"'.format(msg)

        if "STOP" not in msg:
            ready_waiter(s, avail, timeout, retries=2)
            if avail.value == 0:
                print "Can't send msg to arduino as it is not available, try again"
                continue
        avail.value = 0
        if send_msg(s, msg, timeout, retries, "ACK"):
            # print 'msg properly sent to arduino'
            continue