gpt4 book ai didi

c++ - 在 C++ libusb 中将数据写入 Datalogic PM9500 扫描仪的正确格式

转载 作者:行者123 更新时间:2023-11-28 05:01:59 27 4
gpt4 key购买 nike

我目前正在使用 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;
}

最佳答案

我修好了!消息的正确格式需要是:

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

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

关于c++ - 在 C++ libusb 中将数据写入 Datalogic PM9500 扫描仪的正确格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45694951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com