A C# Wrapper for the Velleman K8055 (SDK v4) DLL that uses events for digital and analog input and a job queue for digital and analog output. Build to be used with WPF, not tested with Windows Forms.
##Table of Contents##
- Build the DLL.
- Add a reference to the DLL to your project.
- Download the Velleman K8055 (v4) SDK
- Extract the K8055D.dll from the "DLL_v4.0.0.0" folder.
- Make 4 copies of the DLL file in your project folder and name like noted below*
- K8055-1.dll
- K8055-2.dll
- K8055-3.dll
- K8055-4.dll
- Add the DLL files to your project and change the associated property "Copy to Output Directory" to "Copy if newer" or "Copy always".
- Check the examples below to see how the DLL can be used.
* Copying the dll file 4 times is needed to be able to connect to multiple devices at once without having to open and close connections each time.
K8055Communicator kcCommunicator = new K8055Communicator();
// The StartCommunnication method can be called multiple times if needed
kcCommunicator.StartCommunication(0); // This can be any of the available devices (0, 1, 2 or 3)
// And close the connection when not needed anymore
kcCommunicatior.StopCommunication(0); // This can be any of the available devices (0, 1, 2 or 3)
It is advised to do this before calling the StartCommunication()
method.
Register the event handler:
kcCommunicator.Error += HandleDeviceConnectionError;
Handle the event (showing a messagebox to indicate to the user that the device may not be connected):
private void HandleDeviceConnectionError(object oSender, EventArgs eaEventArguments)
{
MessageBox.Show("Device connection failed!");
}
NOTE: This event is triggered for input on all devices, you will need to check the event arguments to check wich device triggered the event.
Register the event handler:
kcCommunicator.DigitalInput += HandleInput;
Handle the event (logging the input to the console output):
private void HandleInput(object oSender = null, DigitalInputEventArgs dieaEventArguments = null)
{
Console.WriteLine();
Console.WriteLine("Device: " + dieaEventArguments.Input.Device.ToString());
Console.WriteLine("Port: " + dieaEventArguments.Input.Port.ToString());
Console.WriteLine("Value: " + dieaEventArguments.Input.Value.ToString());
}
WARNING: Make sure you have called the
StartCommunication()
method to open the connection to the device before trying to write to any of its digital outputs.NOTE: There are multiple overloads of the
AddDigitalOutput()
method. You can use wichever one you prefer, all overloads add the digital output as individual (per channel) outputs to the queue. The output is written to the device one channel at a time (the current version of the library does not use the K8055 SDK providedWriteAllDigital()
method).
In the current version of the library all methods that add digital output to the queue use "Method one" eventually even the methods that add multiple digital values.
kcCommunicator.AddDigitalOutput(new DigitalIO()
{
Device = 0,
Port = 1,
Value = true
});
A more straightforward method that calls the above method eventually.
kcCommunicator.AddDigitalOutput(0, 1, true);
You can add as many outputs as you want, they are added to the queue in the same order as they are added to the list.
kcCommunicator.AddDigitalOutput(new List<DigitalIO>()
{
new DigitalIO()
{
Device = 0,
Port = 1,
Value = true
},
new DigitalIO()
{
Device = 0,
Port = 2,
Value = false
}
});
The second parameter accepts a boolean array with a max size of 8 (the number of ports). The index in the array is mapped to the ports on the device, if you only pass an array with a size of 3 only the values of the first 3 ports will be changed.
kcCommunicator.AddDigitalOutput(0, new bool?[4] { false, true, false, false });
MORE COMMING SOON