Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

NI-DAQmx Base : Synchronize PC clock (QueryPerformanceCounter) with PCI 6221 analog input

I'm running a loop in windows7 which periodically polls the parallel port for a TTL signal while continuously reading samples at 1KHz for approx 1 hour. As a test, I split the incoming TTL and *also* read it as an analog signal. To my amazement, when I put the results next to each other after the test was done, i noticed that the time of the TTL and the time inside the analog data was NOT THE SAME.

 

more specifically, the timestamp of the TTL-up in my mainloop was 100msec *before* the timestamp in the analog data where I saw the voltage go up !

 

after much teeth-grinding and googling, I found that this is most likely caused by the PC clock drifting : http://www.windowstimestamp.com/description

 

my question is now : how can I sync this ? I have several options :

 

- use the library from http://www.windowstimestamp.com/description but that is quite a bit of work and will require additional DLLs to be installed

- figure out a way to use the clock of the 6221 card inside my mainloop where I poll the parallel port. 

 

is that possible ? Is there a way to ask the time of a TaskHandle ? Or should I use the 

DAQmxRegisterEveryNSamplesEvent() routine to do this ?

0 Kudos
Message 1 of 16
(4,475 Views)

Heya Clarysse

 

 

I have some questions regarding your application: 

1) How and why are you polling the TTL signal? 

2) What do you mean by splitting the TTL signal?

3) Why do you want to use the windows 7 system clock?

 

The system clock on a windows 7 computer and the sample clock on the PCI card will be different due to them being fundamentally different sources. I would suggest, if you're aim is to monitor and acquire the TTL signal, I'd suggest doing this through DAQmx and using the PCI sample clock. 

 

Best Regards, 

JK 

 

Message 2 of 16
(4,439 Views)

1) I poll via the parallel port. The reason is that we have devices that are connected to that port and also that it is a realy easy way to test signals on a computer that does not have a DAQ card

2) physically split the cable. Out of the device comes a cable that goes into one pin of the parallel port, but the other end is a BNC cable. I just put a T-piece between it so I can read the signal that comes out of the device both as parallel port and as analog signal

3) because a LOT is happening on that PC : there are algorithms running that have to show images on the screen at a very specific moment (we have a 200Hz CRT monitor for that and an optical sensor). The deal is that the subjects give feedback via mouse and that decides the flow of the experiment. Rather complicated sometimes and I don't think it is feasible on labview. We run our own custom C++ code

 

about your remark : yes, you're absolutely right that it would be best to just use the DAQ clock, but I don't know how !! I wish there was a niDAQmx function to just say "fetch clock from dev1 into long" !! but I don't think it exists

 

right now I have a function GetGlobalTime() which uses QueryPerformanceCounter to return the time of my application in milliseconds. I could modify that function and return the number of read samples, since I sample continously at 1KHz, but I don't know if that will work...

 

also, I'm thinking about adding a DAQmxRegisterEveryNSamplesEvent function and call every millisecond (my sampling rate is 1Khz) and increment a counter... then use that as reference clock

 

any input is welcome ! I can't be the first one who NEEDS to sync a PC to a DAQ 

0 Kudos
Message 3 of 16
(4,435 Views)

For your application, it sounds like sampling the TTL signal through only the PCI 6221 card is feasible. This way, you would avoid any differences between the clocks impacting the measurements. 

 

The 6221 has 24 Digital ports that will take the TTL signal and the card can sample at a rate of 240kS/s so it should be able to keep up with anything going on in the PC. This way, the signal can be read and passed to the PC without having to run a process in Windows 7 at it's maximum speed (1kHz is about all Windows can handle).

 

Could this be an option? 

 

EDIT: To do this, there are various example pieces of code that'll provide a good starting point for building this into your application. 

0 Kudos
Message 4 of 16
(4,431 Views)

yes indeed : sampling the TTL signal is certainly feasible, but it doesn't solve the whole problem : there is stuff running on the PC that I just can not move to the DAQ. Algorithms, user input, eye-tracker software, polling of USB-hardware, much much more... 

 

as long as I can't write or read the daq clock, i'm in limbo when it comes to syncing these two worlds 😞

0 Kudos
Message 5 of 16
(4,424 Views)

With respect to synchronising the Windows clock and the PCI clock, unfortunately this can't really be done. On the system, the clock driving the Windows operations is not over writable and as such, any process you create will run at some derivation of this system clock.

 

The PCI card on the other hand possess its own clock which in essence allows you to acquire data from a port at much higher precision and rate than the Windows operating system can achieve using its own clock. That said, the clock on the PCI card cannot be used to synchronise processes running on the Windows OS as it's not a Real Time Operating System (RTOS). 

 

That said, whilst synchronisation can't necessarily be achieved, making the data acquisition task on the PCI card and the processes happening on the operating system simultaneous should be possible. To do this, you could start the DAQ and the other process at the same time, then use some kind of event/interrupt coming from the DAQ task to trigger an operation in Windows.

 

Alternatively, you may consider simple acquiring single point data from the PCI card based on the iteration speed of the loop in the OS. Although, this is essentially the same as polling the parallel port straight from windows. 

 

Given that there isn't a simple way to synchronise the PCI card with everything else going on in Windows, can I ask if there is a specific reason why you're using the PCI card to do this rather than polling the required ports as you are with the USB hardware? 

 

 

0 Kudos
Message 6 of 16
(4,404 Views)

i don't fully understand your last question... we use the nidaq card because we have a few devices that pump out an analog signal at 1KHz via a coax cable (breathing pressure for instance). We need that signal digitized. A parallel port can't do that and these devices do not have USB out. 

 

A solution that I am contemplating at the moment is to sync the CPU clock on the number-of-read-samples. Since the DAQ cards are very accurate, I can assume that, if I have read 1234 samples, the time of the DAQ is 1234 milliseconds. So I would just replace the QueryPerformanceCounter call with an inline function that returns the number-of-samples-acquired-so-far. 

 

I have no idea if this is a solid plan or not 😞

0 Kudos
Message 7 of 16
(4,400 Views)

Hi clarysse.jeroen

 

Thanks for clarifying this; your suggested method is a reasonable one. You'll be essentially implicitly time stamping the samples coming from the PCI card for use by the computer. I'm not familiar with the implementation with this in C++ however there should be a DAQmx example available here to help point in the right direction with respect to implementation - http://www.ni.com/example/6999/en/  

 

 

0 Kudos
Message 8 of 16
(4,393 Views)

it is indeed the only way forward that I see. I do wish though that the niDAQmx API would have a call like this :

 

DAQmxGetSampClk(TaskHandle taskHandle);

 

which would simply return the current time of the clock. That would solve ALL synchronization problems for everyone 😞

0 Kudos
Message 9 of 16
(4,391 Views)

Hi clarysse.jeroen

 

There is a DAQmxGetSampClkSrc function (see here http://zone.ni.com/reference/en-XX/help/370471AE-01/mxcprop/attr1852/) however I don't think this can be used to synchronise the CPU clock with the PCI data acquisition.  

0 Kudos
Message 10 of 16
(4,385 Views)