K8055 time delay

Windows XP, SP3, USB 2.0, K8055D.dll version 3.0.2.0

Connect (wire) 1 output to 1 input (O1 to I1) on the k8055 board and try the following code.
(timer interval was set to 5 ms)
API is a project namespace that “wraps” unmamage K8055D.dll using DllImport.
But I think, for other languages it should be the same.
If the function UpdateInputs does not call ReadCounter()…, there is a delay 260 ms (instead of cca 45 ms)!

Could you explain this behaviour?

Thanks!

Milos

int stateDI = 0, stateDO = 0;
bool doInput = false;

private void timer_Tick(object sender, EventArgs e)
{
  timer.Enabled = false;
  UpdateInputs();
  UpdateOutputs();
  timer.Enabled = true;
}

private void UpdateOutputs()
{
  if (doInput == false)
  {
    if (stateDO ==1)
      stateDO = 0;
    else
      stateDO = 1;

    API.WriteAllDigital(stateDO);
    writeTime = DateTime.Now;
    doInput = true;
  }
}

private void UpdateInputs()
{
  stateDI = API.ReadAllDigital();
  if (doInput && stateDI ==stateDO)
  {
    doInput = false;
    System.Diagnostics.Debug.WriteLine((DateTime.Now - writeTime).TotalMilliseconds.ToString());
  }

#if DELAY
// delay cca 260 ms
#else
// delay cca 45 ms
int c1 = API.ReadCounter(1);
int c2 = API.ReadCounter(2);
int ai1 = 0, ai2 = 0;
API.ReadAllAnalog(ref ai1, ref ai2);
#endif
}

It seems the timer confuses the USB operation. Maybe there is too long delay and the USB communication “loses track”.
To test the operation I added a button handler with a for loop.
Using the for loop the delay time is about 12ms.

Adding similar loop to the timer the result is also good.

Here is the modified code:

[code] private void timer_Tick(object sender, EventArgs e)
{
timer.Enabled = false;
for (int i = 0; i < 100; i++)
{
UpdateInputs();
UpdateOutputs();
}
timer.Enabled = true;
}

    private void UpdateOutputs()
    {
        if (doInput == false)
        {
            if (stateDO == 1)
                stateDO = 0;
            else
                stateDO = 1;
            API.WriteAllDigital(stateDO);
            writeTime = DateTime.Now;
            doInput = true;
        }
    }

    private void UpdateInputs()
    {
        stateDI = API.ReadAllDigital();
        if (doInput && stateDI == stateDO)
        {
            doInput = false;
            System.Diagnostics.Debug.WriteLine((DateTime.Now - writeTime).TotalMilliseconds.ToString());

        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            UpdateInputs();
            UpdateOutputs();
        }
    }[/code]

I don’t understand, but it really works! :smiley:

Superb product, excellent support.

Thank

Indeed, the USB behaviour is quite strange. :confused:
Please check this thread where another USB topic was discussed: viewtopic.php?f=15&t=3120&view=previous