如何从条形码扫描仪中正确读取数据?

时间:2015-06-09 05:40:56

标签: c++ libusb-1.0

我正致力于旨在保存数据库文章信息的项目。为此,我实现了一个对话框(使用wxWidgets c ++),允许输入有关文章的信息。当用户扫描给定文章的条形码时,我的应用程序应该读取条形码中的数据并将其显示在文本控件上。要读取数据,我使用 libusb 1.0 库,它提供了许多例程来处理USB设备。为了从条形码扫描器中轮询数据,我实现了一个处理定时器事件的函数,并在其中放置了一个代码来读取数据。不幸的是,到那时,我无法从条形码扫描仪读取数据。请在下面找到我的代码。

#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/assert.hpp>
#include <libusb.h>
#include <ioSerialPort\SerialPort.h>


using namespace std;
using namespace boost;
using namespace boost::assign; 



SerialPort::SerialPort()
: _session(NULL)
, _targetDevices()
{
    init();
}

void SerialPort::init()
{
    libusb_device **devs = NULL; 
    libusb_context *ctx = NULL; 
    libusb_device_handle *barCodeScannerHandle = NULL;
    uint8_t barCodeScannerEndPointIn;
    ssize_t cnt; 
    r = libusb_init(&_session); 

    libusb_set_debug(_session, 3); //set verbosity level to 3, as suggested in the documentation
    cnt = libusb_get_device_list(_session, &devs); //get the list of devices

    ///Find target device
    libusb_device* barCodeScanner = findDevice(devs, cnt, 0x5050, 0x24);

    if ( barCodeScanner != NULL )
    {
        barCodeScannerHandle = open(barCodeScanner);
        barCodeScannerEndPointIn = getDeviceInEndpoint(barCodeScanner);
    }
    _targetDevices += DeviceInfos(*barCodeScanner, *barCodeScannerHandle, barCodeScannerEndPointIn);

    libusb_free_device_list(devs, 1); //free the list, unref the devices in it
}

libusb_device* SerialPort::findDevice(libusb_device **devices,int deviceNum, uint16_t VID, uint16_t PID)
{
    assert( _session != NULL);
    for(int iDevice = 0; iDevice < deviceNum; iDevice++ )
    {
        libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(devices[iDevice], &desc);
        if( r >= 0 && desc.idVendor == VID && desc.iProduct == PID)
        {
            return devices[iDevice];
        }
    }
    return NULL;
}

libusb_device_handle* SerialPort::open( libusb_device* device) const
{
    assert( device != NULL );

    libusb_device_handle *handle = NULL;
    libusb_open(device, &handle);
    return handle;
}

uint8_t SerialPort::getDeviceInEndpoint(libusb_device* device)
{
    uint8_t inEndpoint = 0;
    libusb_device_descriptor desc;
    int r = libusb_get_device_descriptor(device, &desc);
    libusb_config_descriptor *config;
    libusb_get_config_descriptor(device, 0, &config);
    const libusb_interface *inter;
    const libusb_interface_descriptor *interdesc;
    inter = &config->interface[0];
    interdesc = &inter->altsetting[0];
    for(int iEndPoint = 0; iEndPoint < interdesc->bNumEndpoints; iEndPoint++)
    {
        if( interdesc->endpoint[iEndPoint].bEndpointAddress & LIBUSB_ENDPOINT_IN)
        {
            inEndpoint =  interdesc->endpoint[iEndPoint].bEndpointAddress;
        }

    }
    libusb_free_config_descriptor(config);
    return inEndpoint;
}

/// This is the method that I use when catching the timer event
string SerialPort::readDataFromDevice()
{
    int bytes_received;
    string data;


    int result = 0;

    DeviceInfos deviceInfos = _targetDevices.back();
    libusb_device_handle* handle = deviceInfos._handle;
    uint8_t inEndPoint = deviceInfos._endPoint;


    int DATA_MAX_SIZE =  libusb_get_max_packet_size(deviceInfos._device, inEndPoint);
    unsigned char data_in[8];
    int actualData;
    if( handle != NULL)
    {
        if(libusb_kernel_driver_active(handle, 0) == 1) 
        {
            int r = libusb_detach_kernel_driver(handle, 0); //detach it
        }

        if( libusb_claim_interface(handle, 0) == 0) 
        {
            bytes_received = libusb_bulk_transfer(handle, inEndPoint, data_in, DATA_MAX_SIZE, &actualData, 0);
        }

        if( bytes_received > -1)
        {
            data = reinterpret_cast<char*> (data_in);
        }
        int r = libusb_release_interface(handle, 0);
    }
    return data;
}



SerialPort::~SerialPort()
{
    libusb_exit(_session); //close the session
}

函数“libusb_bulk_transfer”始终返回-1。请帮帮我!

0 个答案:

没有答案