8061 c# Threading strange behaviour

I’ve developed an application using c# for reading temperatures and display them in a graph (16 sensor displayed and handled using a multiplexer).
Due to the slow nature of the device (Geode LX 500) using a standard c# timer for sensor reading lead to misalignment between real time and displayed time (the drawing procedures takes too much time and thus the timed procedure is not executed every 200ms but i.e. everuy 220 ms.)
To avoid this situation the reading procedure has been moved into a separate thread with locking on resource.
At this point a strange behaviour happens:
randomly the K8061 board seems to stop to answer to the thread and there is no way to recover the connection. Even trying to Close and init the card again result in nothing.
This doesn’t happen using winform timers.
The thread still runs but obvioulsy no data comes from the board.

Any hint?

[size=85] [code]
lock (locker)
{
if (!(iface.connected()))
{
label1.BeginInvoke(new UpdateTextDelegate(comm_err));
iface.CloseCard();
iface.InitCard();
int a = iface.getAddress();
label14.Text = "Card address: " + Convert.ToString(a);
} else {
threadNum += 1;
label1.BeginInvoke(new UpdateTextDelegate(beginTh));
//label1.BeginInvoke(new UpdateTextDelegate(beginTh));
//text.WriteLine(DateTime.Now.ToString());

                Random rnd = new Random();
                // 010
                iface.downLine(5);
                iface.powerLine(6);
                iface.downLine(7);[/code][/size]

Here is the K8061 mapping library.

[size=85][code]using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Thermostate
{
class k8061
{
private int cardaddress=255;

    [DllImport("k8061.dll")]
    private static extern int OpenDevice();
    [DllImport("k8061.dll")]
    private static extern void ClearAnalogChannel(int CardAddress, int Channel);
    [DllImport("k8061.dll")]
    private static extern void SetAnalogChannel(int CardAddres, int Channel);
    [DllImport("k8061.dll")]
    private static extern int ReadAnalogChannel(int CardAddress, int Channell);
    [DllImport("k8061.dll")]
    private static extern void ClearAllAnalog(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void ClearAllDigital(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void ClearDigitalChannel(int CardAddress, int Channel);
    [DllImport("k8061.dll")]
    private static extern void CloseDevices();
    [DllImport("k8061.dll")]
    private static extern bool Connected(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void OutputAllAnalog(int CardAddress, ref int[] data);
    [DllImport("k8061.dll")]
    private static extern void OutputAllDigital(int CardAddress, int data);
    [DllImport("k8061.dll")]
    private static extern void OutputAnalogChannel(int CardAddress, int Channel, int Data);
    [DllImport("k8061.dll")]
    private static extern void OutputPWM(int CardAddress, int Data);
    [DllImport("k8061.dll")]
    private static extern bool PowerGood(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void ReadAllAnalog(int CardAddress, ref int[] data);
    [DllImport("k8061.dll")]
    private static extern int ReadAllDigital(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern bool ReadDigitalChannel(int CardAddress, int Channel);
    [DllImport("k8061.dll")]
    private static extern void ReadVersion(int CardAddress, ref int[] Buffer);
    [DllImport("k8061.dll")]
    private static extern void SetAllAnalog(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void SetAllDigital(int CardAddress);
    [DllImport("k8061.dll")]
    private static extern void SetDigitalChannel(int CardAddress, int Channel);
    /*
     * 
     *  Metodi pubblici....
     * 
     * 
     */
    public bool InitCard()
    {
        try
        {
            if (cardaddress == 255)
            {
                cardaddress = OpenDevice();
            }
            else
            {
                OpenDevice();
            }
        }
        catch (DllNotFoundException e)
        {
            Console.WriteLine(e.ToString());
        }
        if (this.cardaddress == -1)
        {
            return false;
        }
        if (this.cardaddress == -2)
        {
            return false;
        }
        return true;
    }
    public int getAddress(){
        return cardaddress;
    }
    public int readLine(int line)
    {
        return ReadAnalogChannel(cardaddress, line);
    }
    public void downLine(int line)
    {
        ClearAnalogChannel(cardaddress, line);
    }
    public void powerLine(int line)
    {
        SetAnalogChannel(cardaddress, line);
    }
    public void powerLineStep(int line, int data)
    {
        OutputAnalogChannel(this.cardaddress, line, data);
    }
    public void clearDigitalLine(int line)
    {
        ClearDigitalChannel(this.cardaddress, line);
    }
    public void CloseCard()
    {
        CloseDevices();
    }
    public void powerDigitalLine(int line)
    {
        SetDigitalChannel(this.cardaddress, line);
    }
    public bool powerGood()
    {
        return PowerGood(this.cardaddress);
    }
    public bool connected()
    {
        return Connected(this.cardaddress);
    }
}

}
[/code][/size]

This is strange problem.
Your code looks OK.

You may try to use a new driver and new K8061.DLL:
Here is a link to download the package containing the new DLL, new driver, documentation and demo software: box.net/shared/p8hszo58sh
The driver version is 1.0.0.6 and it must be installed to use the new K8061.DLL version 3.0.0.1.
The driver is located in subfolder K8061_Driver64 of the package.

There is also a demo software included to the package to test the operation of this new driver and new K8061.DLL.

The file MPUSBAPI.DLL must be copied to the same folder with the K8061.DLL.
The best place to copy these two files is the Windows’ subfolder System32 or SysWOW64 in 64-bit environment.
These two files can be copied to your application (.exe) folder too.

Does drivers works also for i386?
Geode LX is an i386 pentium2 class processor.

I reply to myself… it doesn’t work on an i386.

OK.
Did you anyhow get the driver installed and checked from the Device Manager the version is 1.0.0.6 ?

This driver works only with: Windows 2000/XP/Vista and newer OS (both 32-bit and 64-bit).

Ok… i’ve succesfully installed the new driver… (previous was 1.0.0.5). To make it install correctly is necessary to deinstall previous version and then proceed with the new one.
I’m testing the application with new driver and DLL.

The problem still exists…
I’m trying to figure where the problem is… I’m tying to make an application with just a data raw presentation and a thread reading from the K8061.
Could it be that as the k8061.dll is not thread safe, even with locking on application reads, using in thread makes some conflict?

You may use the Connected(int CardAddress) function to check the DLL is working.
If it returns (true or false) then the DLL is “alive”.
The result also indicates if there has happened communication problem with the K8061 card and the card is disconnected.

I already apply a check on the connection status using my methon connected which is a wrapper on the Connected method of K8061.dll … and it is the connection that fails.

lock (locker) { if (!([color=#BF0000]iface.connected()[/color])) { label1.BeginInvoke(new UpdateTextDelegate(comm_err)); iface.CloseCard(); iface.InitCard();

I was expect should be possibile to close the card and re-initialiaze the card… but seems that it is not the case as calling the initCard, after the CloseCard, which calls OpenDevice returns -1 (all card opened) and not the cardaddress.

Here a link to download slightly modified, more robust K8061.DLL v3.0.0.2:
box.net/shared/lsoh24zr2h
This DLL does not disconnect the card if a USB communication error occurs.

This DLL seems to works. I’ll keep up to date.
Thanks