K8090 Relay Board C++ programming load DLL library problem

Hello,

First off, let me say I am a very noobish programmer and have a very basic understanding of C++ code.

I have tried to load the K8090D.dll library into a C++ program and execute one of the functions contained within it.

My code is below:

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <windows.h>

using namespace std;

#define COMMAND_SWITCH_RELAY_ON 0x11;

int main(int argc, char** argv)
{
    cout << "Hello World!";

    DWORD err;

    HINSTANCE hDLL = LoadLibrary("K8090D.dll"); // Handle to DLL

    if(hDLL != NULL)
    {
        printf("Library has been loaded\n");
    }
    else
    {
        err = GetLastError();
        printf("Couldn't load dll\n");
    }

    HANDLE hDevice;
    hDevice = OpenDevice("COM6");
    if (hDevice == NULL)
    {
        printf("Connection failed");
        return 1;
    }

    SendCommand(hDevice, COMMAND_SWITCH_RELAY_ON, 0x03, 0x00, 0x00);
    
    return 0;
}

This program compiles without the OpenDevice() and SendCommand functions commented out but does not with them in. I just wanted to get it to compile so I can play with the commands in the dll using C++.

My enviroment is as follows:

PC
Windows 7 OS
Cygwin g++ compiler set + MinGW and make.
Netbeans IDE

Any help or a minimum working example of how to call the functions in the .dll using C++ would be really helpful.

Kind Regards,

eenzc

Try this:
Download demo application in C

It contains a header file and its corresponding lib file so you can use the K8090 DLL’s functions.

#include <windows.h>
#include <stdio.h>

#include "K8090.h"

#pragma comment( lib, "K8090D" )

int main()
{
	HK8090 hDevice = OpenDevice("COM6");
	if (hDevice == NULL) {
		printf("Failed to connect");
		return 1;
	}

	SendCommand(hDevice, CMD_TOGGLE_RELAY, 0xFF, PARAM_NONE, PARAM_NONE);

	CloseDevice(hDevice);

	return 0;
}

The problem you had is that the K8090 functions (SendCommand, OpenDevice, CloseDevice) are not known to your compiler. This is where K8090.h and K8090D.lib come in.

I don’t know anything about Cygwin, so I hope it can use the lib file and compile the code. If not, we can try to get it to work step by step by analysing the errors.

Thanks, thats brilliant. Your code compiles and executes.

I will shorly write up a simple step by step tutorial with what I have learnt on how to include pre-written C libraries using Netbeans IDE and cygwin/minGW compiler set.

Zac

Wonderful, glad it worked

Hoi,

Do you have also a solution for the Velleman k8090 in Matlab?
I can’t find any examples.

I would be very helped if you have.

I’m doing some development for a laserpointer driven keyboard (imaging) for people with a handicap (written in Matlab) and the possibility to switch equipment with the k8090 via the laserpointer solution.

Many thanks in advance.

Groet,
Leendert
Holland

Me too :slight_smile: Please let me know if you find examples for using the K8090 with Matlab.

Regards,
Lionel

Hoi,

Thanks Lionel for supporting this issue.

An example for a transformation from Visual Basic (VB) to Matlab for such an external board would be usefull, because Velleman delivers two software examples in VB with the board.

Please!

Groet,
Leendert

Hey Leendert and everybody,

any progress?
Did you find out how to send commands to the K8090?

I tried

delete(instrfindall);
s8 = serial('COM8','BaudRate', 19200);
fopen(s8); 
% s8.status % shows that it works til here
% how do i transmit commands to K8090??
% tried fprintf and fwrite but didnt succeed
fclose(s8);
delete(s8);
clear s8;

I also tried to load the dlls I found somewhere on this forum

addpath('E:\Programme\Velleman\K8090\Kits.NET');
loadlibrary('K8090.dll','k8090.h');                                                
libisloaded K8090            % works, lib is actually loaded
libfunctionsview K8090     % works too, shows the functions
% but i'm not able to make calllib work, neither with 
% calllib('K8090','OpenDevice',8)
% nor anything else like
% calllib('K8090','SendCommand', 11,2,0,0,-12)

I finally loaded the Velleman.Kits.dll
asminfo = NET.addAssembly(‘E:\Programme\Velleman\K8090\Kits.NET\Velleman.Kits.dll’)
and could change the Port to COM8 using
K8090.Port = ‘COM8’
But unfortunately that’s it…

Any ideas how I could send my commands to the cards?

Lionel

Hi Lionel and the rest,

Unfortunately I still can’t send commands to the K8090 8-channel relay card either.

calllib comes back with “no method found” or other errors.

Do we use the correct combination of the Velleman .dll file and the from internet taken .h file?

I suggest that Velleman themselves help us customers to get this nice card working!

Please, all forum-users, let’s stick together and ask Velleman to give us a working Matlab environment.

Groet,
Leendert

Matlab doesn’t seem to support the dll /header files… i tried different combinations of .dll and headers, nothing worked.

The Velleman.Kits.dll is only for .NET-stuff, but many .NET-“functions” aren’t supported by matlab…

I needed a solution, so I simply used

!E:\Programme\Velleman\K8090\Tools\K8090C.exe -p COM8 -c 1 -f ON && exit & (&& exit & is not necessary, but keeps your command window clean :slight_smile: )
and so on. Not the solution I imagined at the begining, but the functions ON, OFF and TOGGLE are sufficient for my purpose.

This might help in some cases, but is far away from an optimal solution…

Greetings,
Lionel

Hoi Lionel,

Can you explain a bit more about your work-around?

Is it from Windows prompt, from Matlab, parameter use etc.

Many thanks in advance.

Groet,
Leendert

It’s a tool contained in the installation package.

We have no experience whatsoever with Matlab… the K8090D.dll is a standard Windows DLL with six exported functions:

Function Name	Address	Relative Address	Ordinal	Filename	Type
OpenDevice	0x100011d0	0x000011d0	1 (0x1)	K8090D.dll	Exported Function
CloseDevice	0x100012f0	0x000012f0	2 (0x2)	K8090D.dll	Exported Function
SendCommand	0x10001410	0x00001410	5 (0x5)	K8090D.dll	Exported Function
RegisterListener	0x10001470	0x00001470	3 (0x3)	K8090D.dll	Exported Function
UnregisterListener	0x10001480	0x00001480	4 (0x4)	K8090D.dll	Exported Function
CalculateChecksum	0x10001490	0x00001490	6 (0x6)	K8090D.dll	Exported Function

The Velleman.Kits.dll is a .NET assembly file.

Hi Matlab friends,

Velleman responded that in Matlab the .NET .dll can be used.

With : asmInfo = NET.addAssembly(‘c:\windows\system32\Velleman.Kits.dll’);

that .dll can be load into the Matlab environment (I think).

But then?

Velleman has 2 samples in Visual Basic in the download package.

But what is the command structure within the Matlab program? Please!

Groet,
Leendert

Hi all,

It works!

With thanks to the MatWorks.

% Velleman K8090 8-Channel relay board Matlab program via .NET .dll 
% Date : 15-12-2010
% Author : Leendert Vermeulen Holland
% Mail : leendert.vermeulen@hccnet.nl
% Version : 0.1
% 
%
%
% Make .NET assembly visible to MATLAB and show its Classes

Info = NET.addAssembly('d:\Prive\Ontwikkeling\Matlab\vm8090\MathWorks\Velleman.Kits.dll');

%Info.Classes
%methodsview('Velleman.Kits.K8090Board');
%methods Velleman.Kits.K8090Board;

%% Define a new object check (or modify) ots port ans show applicable methods
objK8090 = Velleman.Kits.K8090Board;

objK8090.Port='COM20'; % In my case. Via Velleman .exe demo program to find out

serialPort = serial('COM20'); 
set(serialPort, 'BaudRate', 19200,'DataBits', 8, 'Parity', 'None', 'Stopbits', 1, 'FlowControl', 'None');

%% Connecting to the K8090 board
%isvalid(objK8090);%

Connect(objK8090);
%% Setting 8 relays
actief=input('Welke relais wilt u inschakelen? (0 - 255) :');

aan = uint8(actief);
uit = uint8(255-aan);
%%
%ToggleRelay(objK8090, bytewoord); %Werkt 14-12-2010
%%

SwitchRelayOff(objK8090, uit);
SwitchRelayOn(objK8090, aan);

%%

Disconnect(objK8090);
delete(serialPort);
clear serialPort;

It’s only version 0.1, but now we can connect the rest is up to you!

Groet,
Leendert

Hi all,

many thanks for your advice. Fine work in Mat-lab!!!
So, I have trying to find some error when I realized the same but in Lab-view 11.
I tried via .Net (Velleman.Kits.dll) and via C++ (k8090d.dll) but I have not any idea how to
make it?

If you have some similar problem please, send me some message or
give me pls some solution.

Many thanks
Regards
Martin :frowning:

Matlab can use .NET assemblies o_O very nice