Hello. Does someone know if I can program the K8000 board in C# ? And if it can be done please give me an example how I can read data from it in C#. Thank you.
The K8000 works fine in Visual C#.
I made a simple test program to check some of the card’s functions.
And here is the code:
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace K8000_Demo1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport(“k8d.dll”)]
public static extern void Start_K8000();
[DllImport("k8d.dll")]
public static extern void Stop_K8000();
[DllImport("k8d.dll")]
public static extern void SelectI2CprinterPort(int port);
[DllImport("k8d.dll")]
public static extern void ConfigAllIOasInput();
[DllImport("k8d.dll")]
public static extern void ConfigAllIOasOutput();
[DllImport("k8d.dll")]
public static extern void ClearAllIO();
[DllImport("k8d.dll")]
public static extern void SetAllIO();
[DllImport("k8d.dll")]
public static extern int ReadIOchip(int chip_no);
[DllImport("k8d.dll")]
public static extern void SetAllDAC();
[DllImport("k8d.dll")]
public static extern void ClearAllDAC();
[DllImport("k8d.dll")]
public static extern int ReadADchannel(int Channel_no);
private void button1_Click(object sender, EventArgs e)
{
Start_K8000();
SelectI2CprinterPort(1);
ConfigAllIOasOutput();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Stop_K8000();
}
private void button2_Click(object sender, EventArgs e)
{
SetAllIO();
}
private void button3_Click(object sender, EventArgs e)
{
ClearAllIO();
}
private void button4_Click(object sender, EventArgs e)
{
SetAllDAC();
}
private void button5_Click(object sender, EventArgs e)
{
ClearAllDAC();
}
private void button6_Click(object sender, EventArgs e)
{
int i;
ConfigAllIOasInput();
i = ReadIOchip(0);
label1.Text = i.ToString();
}
private void button7_Click(object sender, EventArgs e)
{
int i;
i = ReadADchannel(1);
label2.Text = i.ToString();
}
}
}[/code]
Thank you very much for the help. But I still have a problem. When I try to add a reference to K8D.DLL in C# I get the following error: “A reference to ‘D:\K8000C#\Test\Test\K8D.DLL’ could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.” I used the K8D.DLL file that came with the board and I also tried the dll from Velleman’s web site. I tried adding a different DLL to the project to see if the problem is with my VS C# but the DLL worked ok. Do you have any suggestions? Thank you.
K8D.DLL is a standard Windows DLL - cannot reference it.
Runtime DLL linking is used instead.
You just need to copy the following files to a folder where they are found.
Just copy the files to the Windows’ subfolder System32.
You’ll find these files to copy from the Visual Basic 6 example for the K8000:
K8E.exe
K8D.dll
DLPORTIO.SYS
DLPORTIO.DLL
FASTTime32.dll
You may also copy the files to the C# project’s subfolder \bin.
See also: “Dynamic-Link Library Search Order”:
msdn.microsoft.com/en-us/library/ms682586.aspx
Hello.
It works fine now. Thank you very much for your assistance.
Thank you for the good news!
“DllImport” was used to call the functions exported from the DLL.
“Calling Win32 DLLs in C# with P/Invoke”:
msdn.microsoft.com/en-us/magazine/cc164123.aspx
“DllImportAttribute Class”:
msdn.microsoft.com/en-us/library/e4takf5s.aspx