K8055 counter/input lagging

Hi, I was thinking of using a K8055 board for a project that requires a counter, but I’m having some trouble with the counter function.
It seems to lag, the value is not updated immediately as soon as it gets a pulse. I first tested this by displaying the counter value on screen and manually pressing the input button for counter 1. It did seem to lag there but no way to be sure with the human factor involved. So to test it in a better way, I connected digital output 1 to input 1, and send pulses using software.
My loop looks something like this:

[code]int sendCounter = 0;
SetCounterDebounceTime(1, 0);
ResetCounter(1);
while(!kbhit())
{
// Send a pulse by switching the output off and on again
WriteAllDigital(0);
WriteAllDigital(1);
sendCounter++;

int readCounter = ReadCounter(1);

printf("%d %d\n", sendCounter, readCounter);

Sleep(1000/10); // Send 10 pulses per second
}
Sleep(500);
printf("%d %d\n", sendCounter, ReadCounter(1));[/code]
If everything were as I expected it to be, then readCounter should equal sendCounter. It doesn’t though and it always lags behind a couple of pulses (depending at which rate at which I send pulses). Eventually it will catch up, the last line of code always shows equal values as long as the delay before it is long enough.
By putting a delay between the writes and the reads, I found that in this case the counter lags about 260 milliseconds. Any delay smaller than that and the values will differ.

The weirdness doesn’t end there… If I make the code wait for the counters to be synchronized by continuously reading the input counter without delay, it only takes 2 reads or 12 msec to synchronize. But if I add a small delay in that synchronizing loop, it needs more reads? For whatever reason, it seems that the larger the delay, the more reads it needs to synchronize? A delay of 10 msec between reads causes it to need 28 reads before it’s synchronized. With a delay of 0 msec it only needs 2.

int readCounter = ReadCounter(1); while(readCounter!=sendCounter) { numReads++; readCounter = ReadCounter(1); Sleep(0); // Increasing delay time here makes it need more reads before the value is correct?? }

All this seems to apply to inputs in general, not just the counter function.

I’m not sure what to make of all this, the safest bet for getting somewhat accurate readings seems to be to just hammer the board with reads, but that’s hardly optimal, I should not have to do multiple reads to get the correct value.
Could this be a bug in the DLL (k8055d.dll version 4.0.0.0)?

Any help would be appreciated.
Thanks :slight_smile:

The cause to this is that the K8055 sends the data to the USB bus continuously. The interval of the data packets sent is about 7ms…8ms.
There in the USB engine of the PC seems to be a scrolling buffer storing the received data packets.
When your PC software requests the data it doesn’t get it directly from the K8055, but from this buffer. This is why the received data seems to be “old”.
The DLL doesn’t cause this delay.

The only way to get “fresh” data is to repeat the requests to purge the USB buffer.

In the other USB interface board, K8061, there all the data from the card is received by sending a request from the PC software to the card. The card responds with the requested data. There is no latency in the K8061.

Please see this thread for some example results: viewtopic.php?f=15&t=3880

Thanks for your reply, I guess that explains it!
I’ve looked at the K8061 but the lack of counter unfortunately means I can’t use it for this project. No latency and lots of inputs/outputs does make it a very interesting board for another project though.

For this particular project (an engine dynamometer) the counter is the most important function for getting accurate results, it is what we will be using for reading a constantly changing RPM and the faster we can determine the current RPM the better. But if we have to guess at which point in time the counter had the value we’re reading, because of unpredictable latency, it’s not going to be very accurate :slight_smile:
I could really use the higher frequency counter of the VM167 board because then we can generate more pulses per rev and get more accurate results.
In the thread you linked to, you write that the VM167 uses full speed USB, does that mean no latency?

[quote]In the thread you linked to, you write that the VM167 uses full speed USB, does that mean no latency?[/quote]There is no latency in the VM167. Also it is much faster than the K8055.