You are not logged in.

#1 2014-05-25 09:08:39

vegas
Member
Registered: 2014-05-25
Posts: 7

Programming the Velleman k8055 Interface Card in C++

Hi, I am trying to get this board working with C++. I installed the k8055-git package from the aur and the example program works fine with my board. Unfortunately this example is written in C so I still have no clue, how to work with this c++ library from the package.
The package containing a library written in C code, which is used in the example. Then there is another library which seems to translating between C and C++. (I am not quite familiar with classes and libraries, so please excuse me if I paste the wrong parts of the code.)

k8055++.h

#include "k8055.h"

class K8055 {
    public:
        K8055();
        ~K8055();

        int read( void );
        int write( void );
//[...]
        static char* Version( void );
        static int SearchDevices( void );

        int OpenDevice( int board_address );
        int CloseDevice();
//[...]
        int SetDigitalChannel( int channel );
//[...]
    private:
        struct k8055_dev dev;
};

k8055++.cpp

#include "k8055++.h"

K8055::K8055() {
}

K8055::~K8055() {
    k8055_close_device( &dev );
}
//[...]
int K8055::SearchDevices( void ) {
    return k8055_search_devices(0);
}

int K8055::OpenDevice( int board_address ) {
    return k8055_open_device( &dev, board_address );
}

int K8055::CloseDevice() {
    return k8055_close_device( &dev );

//[...]
int K8055::SetDigitalChannel( int channel ) {
    return k8055_set_digital_channel( &dev, channel );
}

//[...]

And my code:

#include <iostream>
#include "k8055++.h"

int main() {
	using std::cout;
	using std::endl;
	K8055 device;
	int dev_addr = SearchDevices();
	cout << dev_addr << endl;
	cout << "Open Device: " << device.OpenDevice(dev_addr) << endl;
	device.SetDigitalChannel(3);
}

produces:

clang++ -std=c++11 -stdlib=libc++ -lc++abi -Wall -pedantic -lk8055++ -lk8055 -o test02 test02.cpp && ./test02
0
Open Device: -1
zsh: segmentation fault (core dumped)  ./test02

I have no idea, what I am doing wrong. Please help. Thank you in advance!

Offline

#2 2014-05-26 09:24:03

zorro
Member
Registered: 2011-11-18
Posts: 47

Re: Programming the Velleman k8055 Interface Card in C++

Looking at http://forum.velleman.eu/viewtopic.php?f=3&t=4122

Post subject: Re: K8055 search devices

New multicard function and procedures
SearchDevices
Syntax
FUNCTION SearchDevices(): Longint;
Description
The function returns all connected devices on the computer. The returned value is a bit field.
Returned value
· Bin 0000, Dec 0 : No devices was found
· Bin 0001, Dec 1 : Card address 0 was found.
· Bin 0010, Dec 2 : Card address 1 was found.
· Bin 0100, Dec 4 : Card address 2 was found.
· Bin 1000, Dec 8 : Card address 3 was found.
Example : return value 9 = devices with address 0 and 3 are connected.

This is likely to be same for your code.

Offline

#3 2014-05-26 15:54:02

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,772

Re: Programming the Velleman k8055 Interface Card in C++

What are you trying to do?  Are you planning on writing a C application, or a C++ application?
C is directly callable from C++.  All k8055++.cpp does is take the C calling conventions and wraps them in a class to make them look for C++ish.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#4 2014-05-26 20:52:04

vegas
Member
Registered: 2014-05-25
Posts: 7

Re: Programming the Velleman k8055 Interface Card in C++

My intention is writing in C++. I found the way to connect with the board. Insteat using

	K8055 device;
	cout << "Open Device: " << device.OpenDevice(3) << endl;
//	[...]

I am know using

	K8055 * device = new K8055();
	cout << "Open Device: " << device->OpenDevice(3) << endl;
	// or
	cout << "Close Device: " << (*device).OpenDevice(3) << endl;
	// or
	cout << "Open Device: " << device[0].OpenDevice(3) << endl;

I have (actually) no clue, why it works when I using a pointer. Perhaps you can give me a hint?

Offline

#5 2014-06-01 12:24:11

derhamster
Member
Registered: 2012-07-08
Posts: 86

Re: Programming the Velleman k8055 Interface Card in C++

My guess is the segfault happens here in k8055_set_digital_channel.

The C++ wrapper does not initialize the k8055_dev struct, especially the dev_no member (see k8055_alloc). Since your K8055 C++ object lives on the stack, the dev_no member has some random value and OpenDevice fails (see here), leading to the segfault. However, if you allocate the K8055 object on the heap, everything is probably initialized to 0.

Imho, the C++ wrapper looks really bad. I suggest using the C api or writing a proper wrapper.

Offline

Board footer

Powered by FluxBB