Java Usb communication Usb4Java ( Printer USB)

1,965 views
Skip to first unread message

DHARA MISTRY

unread,
Mar 2, 2015, 5:46:01 AM3/2/15
to usb4...@googlegroups.com
Hello

I’m recently developing an application that has to communicate with a POS thermal printer using low level API usb4java and using zadig tool
Here is the workflow in this post
1. get the device list
2. choose device (id and vendor)
3. claim the device
4. send data
I am getting error : "data must be direct buffer "

Code : 

import java.nio.ByteBuffer;
import org.usb4java.Context;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceHandle;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;
 
public class DetectUSB {
 
    public static void main(String[] args) {
 
        // Create the libusb context
        Context context = new Context();
        findDevice(context, (short) (0x1203), (short) (0x0128));
        // Deinitialize the libusb context
        LibUsb.exit(context);
        
    }
 
    public static void findDevice(Context context, short vendorId, short productId) {
 
        // Initialize the libusb context
        int result = LibUsb.init(context);
        if (result < 0) {
            throw new LibUsbException("Unable to initialize libusb", result);
        }
 
        // Read the USB device list
        DeviceList list = new DeviceList();
        result = LibUsb.getDeviceList(context, list);
        if (result < 0) {
            throw new LibUsbException("Unable to get device list", result);
        }
 
        try {
            // Iterate over all devices and list them
            for (Device device : list) {
 
                int address = LibUsb.getDeviceAddress(device);
                int busNumber = LibUsb.getBusNumber(device);
                DeviceDescriptor descriptor = new DeviceDescriptor();
                result = LibUsb.getDeviceDescriptor(device, descriptor);
 
                if (result < 0) {
                    throw new LibUsbException(
                            "Unable to read device descriptor", result);
                }
                System.out.format(
                        "Bus %03d, Device %03d: Vendor %04x, Product %04x%n",
                        busNumber, address, descriptor.idVendor(),
                        descriptor.idProduct());
 
                if (result != LibUsb.SUCCESS) {
                    throw new LibUsbException("Unable to read device descriptor", result);
                }
                if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId) {
 
                    System.out.println("Device Found");
                    getDeviceHandle(device);
                    //LibUsb.claimInterface(handle, 0);
                }
 
            }
        } finally {
            // Ensure the allocated device list is freed
            LibUsb.freeDeviceList(list, true);
        }
    }
 
    public static void getDeviceHandle(Device device) {
 
        DeviceHandle handle = new DeviceHandle();
 
        int result = LibUsb.open(device, handle);
 
        if (result != LibUsb.SUCCESS) {
            throw new LibUsbException("Unable to open USB device", result);
        }
 
        try {
            // Use device handle here
            claimDevice(handle, 0);
        } finally {
            LibUsb.close(handle);
        }
    }
 
    public static void claimDevice(DeviceHandle handle, int interfaceNumber) {
        int result = LibUsb.claimInterface(handle, interfaceNumber);
 
        if (result != LibUsb.SUCCESS) {
            throw new LibUsbException("Unable to claim interface", result);
        }
        try {
 
            System.out.println("Device Claimed");
            sendData(handle);
            
        } finally {
            result = LibUsb.releaseInterface(handle, interfaceNumber);
            if (result != LibUsb.SUCCESS) {
                throw new LibUsbException("Unable to release interface", result);
            }
        }
    }
 
    @SuppressWarnings("unused")
public static void sendData(DeviceHandle handle) {
 
    char[] initEP = new char[]{0x1b, '@'};
    char[] cutPaper = new char[]{0x1d, 'V', 1};
 
      String initStr = new String(initEP);
      String cutStr = new String(cutPaper);
        String text = "blabla \n\n\n";
 
       ByteBuffer buffer = ByteBuffer.wrap(initStr.getBytes());
        // SEND INIT BYTE
       int transfered = LibUsb.controlTransfer(handle,
             (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
             (byte) 0x01, (short) 2, (short) 1, buffer, 5000);
 
     if (transfered < 0) {
           throw new LibUsbException("Control transfer failed", transfered);
        }
 
        // SENDT TEXT
        buffer = ByteBuffer.wrap(text.getBytes());
        
       transfered = LibUsb.controlTransfer(handle,
                (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
                (byte) 0x01, (short) 2, (short) 1, buffer, 5000);
 
        if (transfered < 0) {
            throw new LibUsbException("Control transfer failed", transfered);
        }
        
      /*  // SENDT CUT BYTE
        buffer = ByteBuffer.wrap(text.getBytes());
        
        transfered = LibUsb.controlTransfer(handle,
                (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
                (byte) 0x09, (short) 2, (short) 1, buffer, 5000);
 
        if (transfered < 0) {
            throw new LibUsbException("Control transfer failed", transfered);
        } */
        System.out.println(transfered + " bytes sent");
    }
}


Error :
Bus 002, Device 001: Vendor 8086, Product 8c2d
Bus 001, Device 001: Vendor 8086, Product 8c26
Bus 002, Device 004: Vendor 0bda, Product 0129
Bus 002, Device 003: Vendor 1203, Product 0128
Device Found
Device Claimed
Exception in thread "main" java.lang.IllegalArgumentException: data must be a direct buffer
at org.usb4java.LibUsb.controlTransfer(Native Method)
at detectUSB.DetectUSB.sendData(DetectUSB.java:124)
at detectUSB.DetectUSB.claimDevice(DetectUSB.java:102)
at detectUSB.DetectUSB.getDeviceHandle(DetectUSB.java:87)
at detectUSB.DetectUSB.findDevice(DetectUSB.java:64)
at detectUSB.DetectUSB.main(DetectUSB.java:19)


Please any one help with this ?


Dennis Sheirer

unread,
Mar 3, 2015, 7:00:36 AM3/3/15
to usb4...@googlegroups.com
Instead of using a jvm managed byte buffer:

ByteBuffer buffer = ByteBuffer.wrap(initStr.getBytes());

You need to create a direct (native) byte buffer that gets allocated outside of the jvm managed memory space and is accessible by the low level libusb library.  Create the direct buffer and then overlay your initStr bytes and then make your call.

Read about it here:
http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html

Stel Al

unread,
Jun 3, 2016, 4:15:34 AM6/3/16
to usb4java
I have the same problem can you please help us???
Reply all
Reply to author
Forward
0 new messages