Send RS-232 output as a stimulus waveform

I was wondering if anyone could provide advice as to how best accomplish the following:

I have a light source that can be turned on and off by two separate RS-232 command strings using:
VDTWriteBinary2 /TYPE=(0x48) 0x4F, 0x00, 0x50 //Full On

VDTWriteBinary2 /TYPE=(0x48) 0x4F, 0xFF, 0x50  //Full Off

I would like to turn this light source on and off as a square-wave stimulus within an experiment. However, I don't know the best was to control timing within Igor. Currently, my thinking is to use
stimticktime=((stimInterval)*60.15) //converts to 60ths of a second for "ticks" timer
elapsetime = ticks
//code to turn light on
while (ticks<=elapsetime + stimticktime)

interticktime=((interInterval)*60.15) //converts to 60ths of a second for "ticks" timer
elapsetime = ticks
//code to turn light off
while (ticks<=elapsetime + interticktime)

I imagine there is a better way to do this with more precise timing. And ideally, the code would allow an input of variables that define the length of a stimulus interval and the frequency of the stimulation.

Many thanks in advance,

I don't know about precise timing, but a better (and more difficult to program) way would be to use a background task. Read about it in our help:

DisplayHelpTopic "Background Tasks"

You might have to run the background task at some fraction of the overall period if the on time and off time are different.

If a high degree of timing accuracy is necessary, you might need to use a hardware solution.

John Weeks
WaveMetrics, Inc.