reattach 1046 after dettach

Supporting 2.6 and up
Post Reply
virtuosonic
Fresh meat
Posts: 2
Joined: Mon Sep 07, 2020 1:43 pm
Contact:

reattach 1046 after dettach

Post by virtuosonic »

I'm trying to write code to reattach a 1046_0 automatically after dettachment but it looks like it can't detect the device change.

I wrote this code for isolate the problem, but I can't get the library to find the device


System info is:

Code: Select all

$uname -a
Linux rasberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11::49::46 BST 2019 armv7l GNU/Linux
Compile with

Code: Select all

g++ -std=c++11 1046_reconn.cpp -L/usr/lib -lphidget21
Code:

Code: Select all

/**************************************
Name:       1046_reconn.cpp
Desc:       demuestra como reconectar
            un phidgets 1046_0
Date:       05-Sep-2020
License:    MIT
*/

#include <iostream>
#include <thread>
#include <phidget21.h>

using namespace std;
using namespace std::chrono;

class ph1046ctl
{
public:
    ph1046ctl();
    ~ph1046ctl();
    void Init();
    void Attach1046();
private:
    //methods
    void phidgetErr(int result);
    //props
    const int timeout;
    CPhidgetBridgeHandle bridge;
};

ph1046ctl::ph1046ctl() :
    timeout(5000)
{
    clog << "ph1046\n";
}

ph1046ctl::~ph1046ctl()
{
    clog << "~ph1046\n";
    phidgetErr(CPhidget_close((CPhidgetHandle)bridge));
    phidgetErr(CPhidget_delete((CPhidgetHandle)bridge));
}

void ph1046ctl::Init()
{
    clog << "Init\n";
    Attach1046();
}

int AttachHandler(CPhidgetHandle, void*);
int DetachHandler(CPhidgetHandle, void* pctl);
int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr);


void ph1046ctl::Attach1046()
{
    clog << "Creating bridge object\n";
	phidgetErr(CPhidgetBridge_create(&bridge));
	phidgetErr(CPhidget_open((CPhidgetHandle)bridge, -1));
	//1046 handlers
	clog << "Setting Handlers\n";
	phidgetErr(CPhidget_set_OnAttach_Handler(
        (CPhidgetHandle)bridge,
        AttachHandler,nullptr));
	phidgetErr(CPhidget_set_OnDetach_Handler(
        (CPhidgetHandle)bridge,
        DetachHandler, this));
	phidgetErr(CPhidget_set_OnError_Handler(
        (CPhidgetHandle)bridge,
        ErrorHandler, NULL));
	clog << "Waiting attachment\n";
	phidgetErr(CPhidget_waitForAttachment((CPhidgetHandle)bridge, timeout));
}

int AttachHandler(CPhidgetHandle, void*)
{
    clog << "Attach handler\n";
    return 0;
}

int DetachHandler(CPhidgetHandle bridge, void* pctl)
{
	clog << ("Detach handler\n");
    CPhidget_close((CPhidgetHandle)bridge);
    CPhidget_delete((CPhidgetHandle)bridge);
    clog << "Pause\n";
    this_thread::sleep_for(seconds(2));
    auto ctl = (ph1046ctl*)pctl;
    ctl->Attach1046();
	return 0;
}


int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr)
{
    clog << "Error handler: " << errorStr << endl;
	return 0;
}

void ph1046ctl::phidgetErr(int result)
{
    if (!result)
		return;
	const char *err;
	CPhidget_getErrorDescription(result, &err);
	cerr << err <<endl;
	throw runtime_error(err);
}

int main()
{
    clog << "Phidgets 1046 handlers test\n";
    ph1046ctl ctl;
    ctl.Init();
    cout << "Press q key to exit\n";
    while(cin.get() != 'q')
        asm("nop");
    return 0;
}
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: reattach 1046 after dettach

Post by Patrick »

Hi,

You shouldn't be closing/deleting/creating/opening when you get a detach event - just open you device once, and handle attach and detach events as they come in.

-Patrick
virtuosonic
Fresh meat
Posts: 2
Joined: Mon Sep 07, 2020 1:43 pm
Contact:

Re: reattach 1046 after dettach

Post by virtuosonic »

Hi thanks, looks like that was the problem, here is the example modified

Code: Select all

/**************************************
Name:       1046_reconn.cpp
Desc:       demuestra como reconectar
            un phidgets 1046_0
Date:       05-Sep-2020
License:    MIT
*/

#include <iostream>
#include <thread>
#include <phidget21.h>

using namespace std;
using namespace std::chrono;

class ph1046ctl
{
public:
    ph1046ctl();
    ~ph1046ctl();
    void Init();
    void Attach1046();
private:
    //methods
    void phidgetErr(int result);
    //props
    const int timeout;
    CPhidgetBridgeHandle bridge;
};

ph1046ctl::ph1046ctl() :
    timeout(5000)
{
    clog << "ph1046\n";
}

ph1046ctl::~ph1046ctl()
{
    clog << "~ph1046\n";
    phidgetErr(CPhidget_close((CPhidgetHandle)bridge));
    phidgetErr(CPhidget_delete((CPhidgetHandle)bridge));
}

void ph1046ctl::Init()
{
    clog << "Init\n";
    Attach1046();
}

int AttachHandler(CPhidgetHandle, void*);
int DetachHandler(CPhidgetHandle, void* pctl);
int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr);


void ph1046ctl::Attach1046()
{
    clog << "Creating bridge object\n";
	phidgetErr(CPhidgetBridge_create(&bridge));
	phidgetErr(CPhidget_open((CPhidgetHandle)bridge, -1));
	//1046 handlers
	clog << "Setting Handlers\n";
	phidgetErr(CPhidget_set_OnAttach_Handler(
        (CPhidgetHandle)bridge,
        AttachHandler,this));
	phidgetErr(CPhidget_set_OnDetach_Handler(
        (CPhidgetHandle)bridge,
        DetachHandler, this));
	phidgetErr(CPhidget_set_OnError_Handler(
        (CPhidgetHandle)bridge,
        ErrorHandler, NULL));
	clog << "Waiting attachment\n";
	phidgetErr(CPhidget_waitForAttachment((CPhidgetHandle)bridge, timeout));
}

int AttachHandler(CPhidgetHandle, void* pctl)
{
	auto ctl = (ph1046ctl*)pctl;
    clog << "Attach handler\n";
    clog << "ctl==" << ctl <<endl;
    return 0;
}

int DetachHandler(CPhidgetHandle bridge, void* pctl)
{
	clog << ("Detach handler\n");
    return 0;
}


int ErrorHandler(CPhidgetHandle, void*, int, const char* errorStr)
{
    clog << "Error handler: " << errorStr << endl;
	return 0;
}

void ph1046ctl::phidgetErr(int result)
{
    if (!result)
		return;
	const char *err;
	CPhidget_getErrorDescription(result, &err);
	cerr << err <<endl;
	throw runtime_error(err);
}

int main()
{
    clog << "Phidgets 1046 handlers test\n";
    ph1046ctl ctl;
    ctl.Init();
    cout << "Press q key to exit\n";
    while(cin.get() != 'q')
        asm("nop");
    return 0;
}
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests