在 C++ libusb 中将数据写入 Datalogic PM9500 扫描仪的正确格式
Correct format for writing data to Datalogic PM9500 Scanner in c++ libusb
我目前正在使用 Datalogic 的**PM9500 扫描器(4 键型号)**,并尝试在特定事件触发后向屏幕发送消息。我一直在寻找格式化消息的正确方法,但到目前为止我尝试的一切都失败了。根据 Datalogic 文档,发送到手持设备的消息应如下所示:
[Scanner_Addr] [Scanner_Addr_delimiter] DC2 MESSAGE CR
显然还有一种方法可以通过 00 00 00 00 2A AA
多播消息将其发送到连接到同一基地的所有扫描仪,但将其添加为前缀似乎也不起作用。
(扫描仪设置为使用USB-COM模式)
因为我没有在设备上激活定界符,所以没有必要发送。我想通了 DC2 = 0x12
和 CR = 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用于清除整个显示并将光标移回正确位置。
我目前正在使用 Datalogic 的**PM9500 扫描器(4 键型号)**,并尝试在特定事件触发后向屏幕发送消息。我一直在寻找格式化消息的正确方法,但到目前为止我尝试的一切都失败了。根据 Datalogic 文档,发送到手持设备的消息应如下所示:
[Scanner_Addr] [Scanner_Addr_delimiter] DC2 MESSAGE CR
显然还有一种方法可以通过 00 00 00 00 2A AA
多播消息将其发送到连接到同一基地的所有扫描仪,但将其添加为前缀似乎也不起作用。
(扫描仪设置为使用USB-COM模式)
因为我没有在设备上激活定界符,所以没有必要发送。我想通了 DC2 = 0x12
和 CR = 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用于清除整个显示并将光标移回正确位置。