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]