Hi Peter,

If it where me and I had the RAM to spare I would probably go with a dual buffer, one for transmitting and one for receiving, and maintain "pointers" into both indicating where to get and put the next byte.

You can have a 'start of packet flag' which you set in your receive routine if the received byte is either $87 or $AA. Each time you enter the receive routine you check if the flag is set or not, if it is set you write the received byte to the buffer, if it's not set you check the byte for being $87 or $AA.

Another idea is to always write the received byte to the buffer and have a 'start of packet pointer' - a byte variable containing the packets 'start adress' in the buffer array. You then use that 'adress' as the starting point for your manipulating.

You can have a interrupt driven transmit routine. When enabled the TX interrupt fires as soon as there's room in the USARTS TX register. In the interrupt rounting you take your character from your ring buffer and stuff it into the TX-reg, increment your 'pointer' so you know which character is next, decide if you've sent everything or not and either disable the interrupt or leave it enabled. Then you exit the interrupt service routine. As soon as the USART is finished sending the character the interrupt fires again and the process repeats.

Mostly 'general' advice and not much specifics but hopefully you'll get some ideas from it - as always there are several ways to achieve the same thing.

Good luck!
/Henrik.