Random delay latency: IOPort('Write', Handle, Data)

Hello,

I am using PsychTool Box to send TTL to BrainVision TriggerBox via USB. It appears that that the delay in sending the TTL is anywhere between 30ms and 100ms and it’s random. Even if I can’t reduce the delay, is their anyway to at least make it more consistent?

Here is the code I’m using:

TB = IOPort(‘OpenSerialPort’, ‘COM3’);
for i=0:100
pause(.5);
gLCDoc.AppendComment(‘26’, 0); %This is an Event Marker I’m sending to my DAQ
IOPort(‘Write’, TB, uint8(255), 0);
pause(0.01);
IOPort(‘Write’, TB, uint8(0), 0);
i = i+1;
end

Thanks!

A good start would be to not use non-blocking IOPort(‘write’)s, ie. remove that zero at the end, don’t use pause() but WaitSecs() if you think you need to wait, and use the timestamps returned by IOPort to assess the timing on the host computer. Also use Priority() for improved scheduling precision.

Thank you for the advice! I removed the non-blocking to no avail.

I hope this doesn’t trouble you too much, but where (and how) in the code and how would you use WaitSecs() and Priority(). Is it okay to swap pause() with WaitSecs() and place Priority in the loop vs. out of the loop? Also, i’m not quite sure what goes in the parentheses for Priority(). Am I right in that WaitSecs() is looking for WaitSecs(S,1) (i.e. S = seconds and 1 = 1 second)?

Thanks

I see you just claim random delay by IOPort. How did you measure the delay? I am almost sure that the error is from somewhere else, not IOPort(‘Write’). BTW your line of “i=i+1” is trying to mess up things, but Matlab will ignore it anyway.
-Xiangrui

Yup, never mind. You are right. The problem was with how I was sending my event,not psychtoolbox. I got rid of the event and the precision is ~1ms.

Thanks for pointing out a different way of looking at it!