RS232 for real time control

I'm looking for a sketch that uses serial commands from a PC to control the dig out
lines in real time. Specifically, i'm having a hard time understanding how to
coordinate the PC and arduino at high speeds so that the arduino is constantly sending
analog sensor data to to PC, which displays the data in real time, as well as being able to use
software controls to switch the dig pins without too much delay (~20ms ok).

Whenever i try dropping the delay times, the send and receive get out of synch and the message gets garbled.

Is there a simple way of doing this? like instructing the arduino to 'listen' constantly for incoming instruction as opposed to checking, at a given interval, for instruction?

An example sketch of a real-time software switch control and sensor read (like a thermostat) should answer my questions.

Anyone seen something like this around?

Did you look into that: shiftOut(dataPin, clockPin, bitOrder, value) yet? See

Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a pin, after which another pin is toggled to indicate that the bit is available.

This is known as synchronous serial protocol and is a common way that microcontrollers communicate with sensors, and with other microcontrollers. The two devices always stay synchronized, and communicate at close to maximum speeds, since they both share the same clock line. Often referred to as SPI (synchronous protocol interface) in hardware documentation.

HTH, Patrick.