#!/usr/bin/env python

import serial
import select
import sys, os
import termios
import time

#
# Reset the Arduino board into the bootloader, then use the 
# serial port to tell the bootloader to restart in ethernet
# mode.
#

if len(sys.argv) < 2:
    sys.stderr.write("Which serial port do you want me to talk to?\n")
    sys.stderr.write("Syntax: %s [device]\n" % sys.argv[0])
    sys.exit(1)

port = serial.Serial(sys.argv[1], 115200, timeout = 0.05)
port.setDTR(0)
time.sleep(0.500)
port.setDTR(1)

out = ''

while 1:
    port.write('N')   # WRS extension
    b = port.read(1)
    if b:
        out += b
    if out.endswith('OK'):
        print "OK! It's ready!"
        break

