检测usb

 

http://www.microchip.com/forums/tm.aspx?m=340892

 

 

#ifndef USB_H

#define USB_H

#include <QObject>
#include <QtGlobal>

#include <openusb.h>

class USB : public QObject
{
private:
const static int INTERFACE = 0;
const static int CONFIGURATION = 1;
const static int ENDPOINT_BULK_IN = 0x81;
const static int ENDPOINT_BULK_OUT = 0x01;
const static int TIMEOUT = 5000;

bool m_IsOpened;
openusb_handle_t m_LibHandle;
openusb_dev_handle_t m_UsbDevice;

public:
USB();

bool Open (quint16 VendorId, quint16 ProductId);
bool Write (const char *packet, quint8 size);
bool Read (const char *packet, quint8 size);
};

#endif // USB_H

 

 

cpp

USB::USB()
{
this->m_IsOpened = false;

qDebug ("Initializing OpenUSB");
if (openusb_init(0, &this->m_LibHandle) != OPENUSB_SUCCESS)
qCritical ("OpenUSB initialization error");
}

bool USB::Open(quint16 VendorId, quint16 ProductId)
{
openusb_busid_t *bus = NULL;
openusb_devid_t *devids = NULL;
openusb_dev_data_t *devdata;

quint32 devnum = 0;
quint32 busnum = 0;

quint32 i, j;
int ret;

qDebug ("Scanning for device with VendorId = %04x and ProductId = %04x", VendorId, ProductId);
openusb_get_busid_list(this->m_LibHandle, &bus, &busnum);

for (j=0; j<busnum; j++)
{
openusb_get_devids_by_bus(this->m_LibHandle, bus[j], &devids, &devnum);
for (i=0; i<devnum; i++)
{
openusb_get_device_data(this->m_LibHandle, devids[i], 0, &devdata);
if (devdata->dev_desc.bDeviceSubClass == 0 &&
devdata->dev_desc.bDeviceClass == 0 &&
devdata->dev_desc.idProduct == ProductId &&
devdata->dev_desc.idVendor == VendorId)
{
openusb_free_device_data (devdata);
break;
}
// TODO: check why go in corruption or double free !!!!
//openusb_free_device_data (devdata);
}
if (i >= devnum) openusb_free_devid_list (devids);
else break;
}

openusb_free_busid_list (bus);

if (j >= busnum)
{
qDebug ("Cannot find specified device");
return false;
}

if ((ret = openusb_open_device (this->m_LibHandle, devids[i], USB_INIT_DEFAULT, &m_UsbDevice)) != OPENUSB_SUCCESS)
{
qCritical ("Cannot open specified device. Error code %d", ret);
openusb_free_devid_list (devids);
return false;
}
qDebug ("Specified USB device found");
openusb_free_devid_list (devids);

if ((ret = openusb_set_configuration (this->m_UsbDevice, this->CONFIGURATION)) != OPENUSB_SUCCESS)
{
qCritical ("Cannot set configuration 1. Error code %d", ret);
return false;
}
qDebug ("Configuration 1 setted");

if ((ret = openusb_claim_interface (this->m_UsbDevice, this->INTERFACE, USB_INIT_DEFAULT)) != OPENUSB_SUCCESS)
{
qCritical ("Cannot claim interface. Error code %d", ret);
return false;
}
qDebug ("Interface claimed");
this->m_IsOpened = true;
return true;
}

bool USB::Write (const char *packet, quint8 size)
{
int ret;

if (!this->m_IsOpened) return false;

openusb_bulk_request_t bulk;
memset (&bulk, 0, sizeof (bulk));
bulk.payload = (uint8_t*)packet;
bulk.length = size;
bulk.timeout = this->TIMEOUT;

if ((ret = openusb_bulk_xfer (this->m_UsbDevice, this->INTERFACE, this->ENDPOINT_BULK_OUT, &bulk)) != OPENUSB_SUCCESS)
{
qCritical ("Bulk xfer write failed: %s", openusb_strerror (ret));
return false;
}
qDebug ("Bulk xfer write result:\n- status = %d\n- xfer_bytes = %d\n", bulk.result.status, bulk.result.transferred_bytes);
return true;
}

bool USB::Read (const char *packet, quint8 size)
{
int ret;

if (!this->m_IsOpened) return false;

openusb_bulk_request_t bulk;
memset (&bulk, 0, sizeof (bulk));
bulk.payload = (uint8_t*)packet;
bulk.length = size;
bulk.timeout = this->TIMEOUT;

if ((ret = openusb_bulk_xfer (this->m_UsbDevice, this->INTERFACE, this->ENDPOINT_BULK_IN, &bulk)) != OPENUSB_SUCCESS)
{
qCritical ("Bulk xfer read failed: %s", openusb_strerror (ret));
return false;
}
qDebug ("Bulk xfer read result:\n- status = %d\n- xfer_bytes = %d\n", bulk.result.status, bulk.result.transferred_bytes);
return true;
}

 

use

this->m_Usb = new USB();
if (this->m_Usb->Open(0x04d8, 0x0053))
{
char test[64];
//int i;
//for (i=0; i<64; i++) test[i] = i+1;
test[0] = 0x81;
this->m_Usb->Write(test, 1);
this->m_Usb->Read(test, 1);
}

posted on 2011-05-18 10:20  风乔  阅读(307)  评论(0编辑  收藏  举报

导航