Don, the documentation for the protocol is here.
If you look in my Train Control program under the _interface tab, you will see routines for transmitting TMCC and the various kinds of Legacy commands.
I find it useful to have all the routines deposit commands in a circular buffer; another routine called from loop() sends commands out the serial port if at least 30 milliseconds have elapsed since the last command sent.