You can try the attached program. I essentially monitors the CTS pin on the serial port (pin 8 on a 9-pin connector, pin 5 on a 25-pin connector) until it gets a low to high transition. You would add the other code you want to run in the sequence area.
Randall Pursley