在c ++ libusb

时间:2017-08-15 14:26:51

标签: c++ libusb

我目前正在使用来自Datalogic的** PM9500扫描仪(4键型号)**,并尝试在某个事件触发后向屏幕发送消息。我一直在寻找一种正确的格式化消息的方法,但到目前为止我所尝试的一切都是失败的。根据Datalogic文档,发送到手持设备的消息应如下所示:

  

[Scanner_Addr] [Scanner_Addr_delimiter] DC2 MESSAGE CR

显然还有一种方法可以使用00 00 00 00 2A AA多播消息将其发送到连接到同一基站的所有扫描仪,但添加它作为前缀似乎也不起作用。

(扫描仪设置为使用USB-COM模式) 由于我没有在设备上激活分隔符,因此无需发送分隔符。我发现了DC2 = 0x12CR = 0x0D,但我对如何实际列出邮件格式感到困惑。

这就是我现在格式化的方式:

string msg = "0000000C86B40x12HI0x0D"; // scanner address is specific to a scanner (12 char), this is a random scanner address for security  
int size = msg.size();
cout << "size : " << size << endl;
unsigned char *data = new unsigned char[size]; //data to write
strncpy((char*)data, msg.c_str(), size);

然后我使用 libusb 发送批量转移:

r = libusb_bulk_transfer(dev_handle, (2 | LIBUSB_ENDPOINT_OUT), data, size, &actual, 0);

对基站的写入成功并且它写入了整个消息,但无论我尝试做什么格式化,它都不会显示在屏幕上。有没有人有编码这些扫描仪的经验?

编辑:整个代码示例 在DeviceUSB.h中

#include <libusb-1.0/libusb.h>
#include <cassert>
#include <cstdio>

#define SCANNER_VENDOR_ID 0x05f9
#define SCANNER_PRODUCT_ID 0x2210

class DeviceUSB
{

    private: 
};
DeviceUSB.cpp中的

#include "DeviceUSB.h"
#include <iostream>
#include <cstring>

using namespace std;

int main() {

    libusb_device ** devs; //pointer to pointer of device, used to retrieve a list of devices
    libusb_device_handle * deviceHandle; //a device handle
    libusb_context * context = NULL; //a libusb session
    libusb_device_descriptor desc = {0};
    int r; //for return values
    ssize_t cnt; //holding number of devices in list
    r = libusb_init(&context); //initialize the library for the session we just declared
    if(r < 0) {
        cout << "Init Error " << r << endl; //there was an error
        return 1;
    }
    libusb_set_debug(context, LIBUSB_LOG_LEVEL_DEBUG); //set verbosity level to 3, as suggested in the documentation

    cnt = libusb_get_device_list(context, &devs); //get the list of devices
    if(cnt < 0) {
        cout << "Get Device Error" << endl; //there was an error
        return 1;
    }
    cout << cnt << " Devices in list." << endl;
    libusb_device * device;
    for (size_t i = 0; i < cnt - 1; i++)
    {
        device = devs[i];
        int recieve = libusb_get_device_descriptor(device, &desc);
        assert(recieve == 0);
        printf("Vendor:Device = %04x:%04x\n", desc.idVendor, desc.idProduct);

        if (desc.idVendor == SCANNER_VENDOR_ID)
        {
            break;
        }
        else
            continue;
    }

    // open the device
    int ret = libusb_open(device, &deviceHandle);
    assert(ret == 0);

    if(deviceHandle == NULL)
        cout << "Cannot open device" << endl;
    else
        cout << "Device Opened" << endl;
    libusb_free_device_list(devs, 1); //free the list, unref the devices in it

    string msg = "0000000C86B40x12HI0x0D";
    int size = msg.size();
    cout << "size : " << size << endl;
    unsigned char *data = new unsigned char[size]; //data to write
    strncpy((char*)data, msg.c_str(), size);

    int retBytes;
    if(libusb_kernel_driver_active(deviceHandle, 0) == 1) { //find out if kernel driver is attached
        cout << "Kernel Driver Active" << endl;
        if(libusb_detach_kernel_driver(deviceHandle, 0) == 0) //detach it
            cout << "Kernel Driver Detached!" << endl;
    }
    r = libusb_claim_interface(deviceHandle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
    if(r < 0) {
        cout << "Cannot Claim Interface" << endl;
        return 1;
    }

    cout << "Data->" << data << "<-" << endl; //just to see the data we want to write 
    r = libusb_bulk_transfer(deviceHandle, (2 | LIBUSB_ENDPOINT_OUT), data, size, &retBytes, 0); //my device's out endpoint was 2, found with trial- the device had 2 endpoints: 2 and 129

    if(r == 0 && retBytes == size) //we wrote the 4 bytes successfully
        cout << "Writing Successful!" << endl;
    else
        cout << "Write Error" << endl;

    r = libusb_release_interface(deviceHandle, 0); //release the claimed interface
    if(r!=0) {
        cout << "Cannot Release Interface" << endl;
        return 1;
    }

    libusb_close(deviceHandle); //close the device we opened
    libusb_exit(context); //needs to be called to end the

    delete[] data; //delete the allocated memory for data
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我修好了! 消息的正确格式必须是:

 string msg = "0000000C86B4\x12\x1b[2JHI\x0d";

\ x1b [2J 用于清除整个显示屏并将光标移回正确的位置。