Index: trunk/cardbox/rs485_int.c
===================================================================
--- trunk/cardbox/rs485_int.c	(revision 284)
+++ trunk/cardbox/rs485_int.c	(revision 285)
@@ -84,5 +84,5 @@
 
 ISR(USART_TX_vect) {
     // Re-enable reads
-    //rs485_toggle_transmit(RS485_TX_OFF);
+    rs485_toggle_transmit(RS485_TX_OFF);
 }
Index: trunk/cardbox/cardbox.c
===================================================================
--- trunk/cardbox/cardbox.c	(revision 284)
+++ trunk/cardbox/cardbox.c	(revision 285)
@@ -148,6 +148,7 @@
     req,
     press,
     send,
+    ack,
     rsp
 } state_t;
 
@@ -154,11 +155,10 @@
 
 int main(void) {
     uint8_t mbuf[PROGD_PACKET_SIZE];
-    uint8_t cbuf[20];
+    uint8_t cbuf[21];
     uint8_t clen;
-    int8_t cret;
     uint8_t resp;
-    uint8_t  c;
+    uint8_t c;
     uint8_t len;
     uint8_t retries = 0;
     state_t state = req;
@@ -169,62 +169,57 @@
     card_reader_setup();
     sei();
 
-    toggle_led(LED_GREEN|LED_YELLOW|LED_RED, OFF);
-    read_card = 0;
-
     while(1) {
-        while(1) {
-            read_card = 1;
-            if (cr_flag != CR_NONE) {
-                read_card = 0;
-                if ((cret = parse_card(cbuf, &clen)) < 0) {
-                    for(c=0;c < 4;c++) {
-                        toggle_led(LED_RED,ON);
-                        _delay_ms(250);
-                        toggle_led(LED_RED,OFF);
-                        _delay_ms(250);
-                    }
+        switch(state) {
+            case req:
+                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
 
-                } else {
-                    for (c = 0; c < clen; c++) {
-                        rs485_send_byte(cbuf[c]);
-                    }
-                }
-                cr_flag = CR_NONE;
-            }
-        }
+                // Start reading cards
+                read_card = 1;
 
+                // See if we have a card, otherwise check if we got
+                // a message
+                if (cr_flag != CR_NONE) {
+                    read_card = 0;
 
+                    // Reading failed
+                    if (parse_card(&(cbuf[1]), &clen) < 0) {
+                        for(c=0;c < 4;c++) {
+                            toggle_led(LED_RED,ON);
+                            _delay_ms(250);
+                            toggle_led(LED_RED,OFF);
+                            _delay_ms(250);
+                        }
+                    // Reading is good, wait for a key
+                    } else {
+                        start_timer();
+                        reset_timer();
+                        reset_timeout_flag();
+                        state = press;
+                    }
 
-        switch(state) {
-            case req:
-                toggle_led(LED_RED|LED_GREEN|LED_YELLOW, OFF);
-                      
-                // Wait for a packet
-                resp = parse_packet(mbuf, &len, ADDR);
+                    // Reset the card reader flag
+                    cr_flag = CR_NONE;
+                } else {
+                    // Wait for a packet
+                    resp = parse_packet(mbuf, &len, ADDR);
 
-                if (resp == TT_GET_KEY) {
-                    _delay_ms(50);
-                    send_packet(TT_ACK, ADDR, NULL, 0);
-                    toggle_led(LED_YELLOW, ON);
-                    reset_timer();
-                    reset_timeout_flag();
-                    start_timer();
-                    c = ' ';
-                    state = press;
-                } else if(resp == TT_PING){
-                    _delay_ms(50);
-                    send_packet(TT_ACK, ADDR, NULL, 0);
+                    // Ping Request
+                    if(resp == TT_PING){
+                        _delay_ms(50);
+                        send_packet(TT_ACK, ADDR, NULL, 0);
+                    }
                 }
                 break;
             case press:
-                c = get_button();
+                toggle_led(LED_YELLOW,ON);
+                cbuf[0] = get_button();
                 retries = 0;
         
                 if (seconds > TIMEOUT_SECONDS) {
                     set_timeout_flag();
                     state = send;
-                } else if (c != ' ') {
+                } else if (cbuf[0] != ' ') {
                     state = send;
                 } else {
                     _delay_ms(100);
@@ -233,34 +228,57 @@
                 break;
             case send:
                 if (get_timeout_flag() == 1) {
-                    _delay_ms(50);
-                    send_packet(TT_TIMEOUT, ADDR, NULL, 0);
+                    toggle_led(LED_RED|LED_YELLOW,ON);
+                    _delay_ms(500);
+                    toggle_led(LED_RED|LED_YELLOW,OFF);
+                    _delay_ms(500);
                     state = req;
-                    break;
                 } else {
                     _delay_ms(50);
-                    send_packet(TT_SEND_KEY, ADDR, &c, 1);
+                    send_packet(TT_KC, ADDR, cbuf, 1 + clen);
+                    state = ack;
                 }
 
-                state = rsp;
                 break;
-            case rsp:
-                resp = parse_packet(mbuf, &len,  ADDR);
+            case ack:
+                resp = parse_packet(mbuf, &len, ADDR);
 
                 if (resp == TT_ACK) {
-                    toggle_led(LED_GREEN, ON);
-                } else if ((resp == TT_NACK) || (retries == TT_MAX_RETRY)) { 
+                    state = rsp;
+                    start_timer();
+                    reset_timer();
+                    state = rsp;
+                } else if (retries == TT_MAX_RETRY) {
                     toggle_led(LED_RED, ON);
+                    _delay_ms(1000);
+                    toggle_led(LED_RED, OFF);
+                    state = req;
                 } else {
                     retries++;
                     state = send;
-                    break;
                 }
+                break;
 
-                _delay_ms(1000);
-                toggle_led(LED_RED, OFF);
-                toggle_led(LED_GREEN, ON);
-                state = req;
+            case rsp:
+                resp = parse_packet(mbuf, &len,  ADDR);
+
+                if (resp == TT_GRANT) {
+                    toggle_led(LED_GREEN, ON);
+                    _delay_ms(1000);
+                    toggle_led(LED_GREEN, OFF);
+                    state = req;
+                } else if (resp == TT_DENY) { 
+                    toggle_led(LED_RED, ON);
+                    _delay_ms(1000);
+                    toggle_led(LED_RED, OFF);
+                    state = req;
+                } else if (seconds > TT_MAX_RETRY) {
+                    toggle_led(LED_RED,ON);
+                    _delay_ms(1000);
+                    toggle_led(LED_RED, OFF);
+                    state = req;
+                }
+
                 break;
             default:
                 state = req;
