anchor.diff:    (setf (aref vector location) (ldb (byte 8 0) value))
     (setf (aref vector (1+ location)) (ldb (byte 8 8) value)))
   
  -(defun make-command (command-id &rest args)
  -  "Creates a nxt command of type indicated by `command-id'.
  -All the arguments are keyword value pairs.  These should
  -be matched in the command struct for the command.
  -
  -So for example:
  - 
  -  (make-command 'find-first :file-name \"*.*\")
  -
  -creates a valid command."
  -  (let* ((command-data (command-data-for-id command-id))
  -	 (command-length (second command-data))
  -	 (result (make-array command-length 
  -			     :element-type 'unsigned-byte
  -			     :initial-element 0)))
  -    ;; First write first few bytes.
  -    (write-uword-to-command result (first command-data) 0)
  -
  -    ;; Now fill in the rest.
  -    (loop :for (key value . rest) :on args :by #'cddr 
  -       :for key-datum = (getf command-data key) :do
  -       (progn
  -	 (unless key-datum (error "Can not find key: ~S back in data for command." key))
  -	 (case (first key-datum)
  -	   ('byte (write-byte-to-command result value (second key-datum)))
  -	   ('string (write-string-to-command result value (second key-datum) (third key-datum)))
  -	   ('uword (write-uword-to-command result value (second key-datum))))))
  -    result))
  -
  +
  +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  +;;; CONNECTION
  +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   (defvar *connection* nil)
   
   (defun open-connection (&optional (dev-name "/dev/tty.NXT-DevB-1"))
  
positive.code:   (setf *connection* nil))
  
  
  (defun write-to-nxt-byte (byte)
    "Write a raw byte to the nxt."
    (write-byte byte *connection*))
  
negative.code:   (def-nxt-command 'message-read         #x00 #x13 
  		   :remote-inbox '(byte 2) :local-inbox '(byte 3) :remove '(byte 4)))
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; FUNCTIONS THAT HELP DESTRUCTURING THE SPECIFICATION OF THE COMMANDS
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (defun type-of-spec (spec)
    "Returns the type of a single variable spec for the nxt command description.
  Returns one of: 'byte, 'uword, 'string."
    (first spec))
  
  (defun last-byte-of-spec (spec)
    "Returns the last byte in the command vector occupied by spec."
    (case (type-of-spec spec)
      ('uword (+ 1 (second spec)))
      (t (car (last spec)))))
      
  (defun command-length (command-spec)
    "Returns the length of the command buffer needed to hold
  all the variables specified in `command-spec'."
    (max 2
     (1+ (loop :for (name spec) :on command-spec :by #'cddr
       :maximize (last-byte-of-spec spec)))))
  
  (defun reply-expected-for-type-code (code)
      (eql 0 (ldb (byte 1 7) code)))
  
  ;; TRANSLATING THE FORMS INTO COMMANDS
  (defun create-nxt-command-form (name type-code command-code &rest args)
    "Creates the command function `name' which is a nxt command with the
  two command identification bytes `type-code' and `command-code'.  
  The `args' contains key value pairs that should be taken by the function together
  with a type specifier.  Type specifiers are lists of the form (`type' `position' rest).
  The type is one of 
  
    byte
    uword
    string  - has additional parameter, `final-position'.
  
  The result is something that looks like
  
    (create-nxt-command 'find-first           #x01 #x86 'file-name '(string 2 21))
  
  ==>
    (defun find-first (&key file-name)
        (let ((data-vector (make-array 22 :element-type 'unsigned-byte :initial-element 0)))
            (write-byte-to-command data-vector #x01)
            (write-byte-to-command data-vector #x86)
            (write-string-to-command data-vector file-name 2 21)
            (write-to-nxt data-vector)
            (read-nxt-reply)))
  
  
  "
    (let ((command-length (command-length args)))
      (list
       'defun name (cons '&key (loop :for key :in args :by #'cddr :collect key ))
       `(let ((data-vector (make-array ,command-length 
  				     :element-type 'unsigned-byte
  				     :initial-element 0)))
  	(write-byte-to-command data-vector ,type-code 0)
  	(write-byte-to-command data-vector ,command-code 1)
  	,(cons 'progn (loop :for (key spec) :on args :by #'cddr :collect
  	    (case (type-of-spec spec)
  	      (byte `(write-byte-to-command data-vector ,key ,(second spec)))
  	      (string `(write-string-to-command data-vector ,key ,(second spec) ,(third spec)))
  	      (uword `(write-uword-to-command data-vector ,key ,(second spec)))
  	      (t "Help"))))
  	(write-to-nxt data-vector)
  	,(when (reply-expected-for-type-code type-code)
  	       '(read-nxt-reply))))))
  
  (defmacro def-nxt-command (name name-code type-code &rest rest)
    (apply #'create-nxt-command-form name name-code type-code rest))
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; CREATING THE COMMANDS
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  (def-nxt-command get-firmware-version #x01 #x88)
  (def-nxt-command get-device-info      #x01 #x9b)
  (def-nxt-command find-first           #x01 #x86 file-name (string 2 21))
  (def-nxt-command find-next            #x01 #x87 handle    (byte 2))
  (def-nxt-command close                #x01 #x84 handle     (byte 2))
  (def-nxt-command play-tone            #x00 #x03 frequency  (uword 2)       duration (uword 4))
  (def-nxt-command get-input-values     #x00 #x07 port       (byte 2))
  (def-nxt-command get-output-state     #x00 #x06 port       (byte 2))
  (def-nxt-command get-battery-level    #x00 #x0b)
  (def-nxt-command stop-sound-playback  #x00 #x0c)
  (def-nxt-command set-input-mode       #x00 #x05 port       (byte 2) 
  		 sensor-type          (byte 3)  sensor-mode (byte 4))
  (def-nxt-command message-read         #x00 #x13 
  		 remote-inbox         (byte 2)  local-inbox (byte 3) remove (byte 4))
  
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;; HELPER FUNCTIONS TO BUILD THE COMMAND
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (defun write-byte-to-command (vector value location)
    (assert (arrayp vector) (vector) "First argument should be a vector!")
    (setf (aref vector location) value))
  