Search code examples
c++libusb

Correct format for writing data to Datalogic PM9500 Scanner in c++ libusb


I'm currently working with the **PM9500 scanner (4-key model)**from Datalogic and trying to send a message to the screen after a certain event triggers. I've been looking all over for a correct way to format the messages, but everything I have tried so far has been a failure. According to the Datalogic documentation, a message sent to the handheld device should look like this:

[Scanner_Addr] [Scanner_Addr_delimiter] DC2 MESSAGE CR

Apparently there is also a way to send it to ALL scanners connected to the same base with a 00 00 00 00 2A AA multicast message, but adding that as a prefix also doesn't seem to work.

(The scanner is set to using USB-COM mode) Since I don't have delimiter activated on the device, that's not necessary to send. I figured out that DC2 = 0x12 and CR = 0x0D, but am stumped on how to ACTUALLY lay out the format of the message.

This is how I have it formatted right now:

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);

then I send it using libusb with a bulk transfer :

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

The write to the base station is successful and it writes the whole message, but it won't show up on the screen no matter what formatting I try to do. Does anyone have any experience with coding for these scanners?

EDIT : Whole code example in 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: 
};

in 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;
}

Solution

  • I fixed it! The correct formatting of the message needs to be :

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

    the \x1b[2J is for clearing the whole display and moving the cursor back to the correct position.