PDA

View Full Version : USB HID Connection somehow impossible?



ThomasM
15th November 2015, 19:59
Hey there,

I'm currently working on my project in which I use an AVR microcontroller to measure some data and send it to my computer via USB. I used some code and circuit examples from obdev.at (vusb) in which I decided to use HID-Classes.

My computer sees the device within my system configurations. Because it's an HID device, all other websites and references ask me to use HIDAPI (http://www.signal11.us/oss/hidapi/). It's a special kind of libusb (I guess) which is optimized for USB-HID communication. So I use the hidapi.h for MAC OSX and Linux (the windows platform will be there in future).

Nevertheless pure C++ Code is working pretty well and I can open my device with HIDAPI and no problems occur.

In the following code everything works pretty well:


#include <QGuiApplication>
#include <QApplication>

#include <iostream>
#include "hidapi.h"
using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication::setOrganizationName("ThomasProducts");
QCoreApplication::setOrganizationDomain("");
QCoreApplication::setApplicationName("CLC");
QCoreApplication::setApplicationVersion("alpha 0.1");

struct hid_device_info *devs, *cur_dev;

devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
cout << "Device Found\n type: " << cur_dev->vendor_id << " " << cur_dev->product_id << endl;
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);

QApplication *app = new QApplication(argc, argv);
return app->exec();
}


But that leads only to "static" USB-Connections. So I have to plug in my device before I start my application. It should be dynamic so that I can plug my USB device at any time I would like to.

In future use there will be a GUI by Qt and Database and so on… To use QtWidget and QtMainWindow I'm supposed to start my application with QApplication. So here is an example for my real Project:


#include <QGuiApplication>
#include <QApplication>

#include <iostream>
#include "hidapi.h"
using namespace std;

int main(int argc, char *argv[])
{
QApplication *app = new QApplication(argc, argv);
QCoreApplication::setOrganizationName("ThomasProducts");
QCoreApplication::setOrganizationDomain("");
QCoreApplication::setApplicationName("CLC");
QCoreApplication::setApplicationVersion("alpha 0.1");

struct hid_device_info *devs, *cur_dev;

devs = hid_enumerate(0x0, 0x0);
cur_dev = devs;
while (cur_dev) {
cout << "Device Found\n type: " << cur_dev->vendor_id << " " << cur_dev->product_id << endl;
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);

return app->exec();
}


The second code gives me some major issues (see Backtrace.txt for more information). If I comment the QApplication *app line, and modify the return line everything works pretty well. Otherwise It will be like within the Backtrace.txt The only difference is the line in which QApplication is defined!

Of course there are some other QWidgets I use, but in a nutshell this is just what happens in any case I try to establish the usb communication by Qt with HIDAPI. The source Code of the nutshell-programm is within the attachment.

I'm stuck here! Is there any way to use QtApplication and HIDAPI in a simple program and manage the whole USB communication by a stand alone class within C++?

I'm using Qt 5.5.0 on a Mac OSX Yosemite. If you need more information please ask me! I would be glad if someone got an idea about my problem.

Greetings,
Thomas