2017-08-15 64 views
0

我目前正在使用Datalogic的** PM9500扫描仪(4键模型)**,并尝试在某些事件触发后向屏幕发送消息。我一直在寻找一种正确的方式来格式化消息,但到目前为止我尝试过的所有东西都是失败的。据Datalogic的文档,发送到手持设备的消息应该是这样的:在C++中将数据写入Datalogic PM9500扫描仪的正确格式libusb

[Scanner_Addr] [Scanner_Addr_delimiter] DC2消息CR

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

(扫描仪设置为使用USB-COM模式) 因为我没有在设备上激活分隔符,所以不需要发送。我想通了DC2 = 0x12CR = 0x0D,但我难以想象如何实际上布局消息的格式。

这是怎么我有它现在格式化:

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

到基站的写入成功和:

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它会写出整个消息,但无论我尝试使用何种格式,它都不会显示在屏幕上。有没有人有这些扫描仪的编码经验?

编辑:全代码示例 在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; 
} 
+0

尝试使用'\ f'而不是'0x12'和'\ r'来代替'0x0d'并省略所有空格。 –

+0

'string msg =“ \ fHI \ r”;'仍然没有输入到屏幕。我试图联系他们的技术人员也无济于事。 –

+0

什么是您的EXACT代码,包括正确的扫描仪地址?请点击您的问题下的“编辑”并在那里更新(不在评论区域)。 –

回答

1

我定了! 消息的正确格式化需要是:

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

\ X1B [2J是用于清除整个显示和移动光标返回到正确的位置。

+0

干得好,谢谢你与社区分享。 –