Click here, if you are looking for usb4java high level api
I’m recently developing an application that has to communicate with a POS thermal printer and after a long research and some trials decided best way would be to send commands through usb port. Unfortunately I don’t have a POS printer right now but I managed to claim a USB device (GamePad) with the help of Usb4Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
public class UsbLowPos { private DeviceHandle handle; private Context context; private short vendor; private short product; private byte endpoint; public UsbLowPos() { context = new Context(); } public void claimDevice(short vendor, short produkt) { findDevice(context, vendor, produkt); } public void releaseUsb() { LibUsb.releaseInterface(handle, 0); LibUsb.close(handle); } public static void main(String[] args) { UsbLowPos upos = new UsbLowPos(); upos.claimDevice((short) (0x1504), (short) (0x001F)); char[] initEP = new char[]{0x1b, '@'}; char[] cutP = new char[]{0x1d, 'V', 1}; String ptxt = new String(initEP) + 'n'; upos.print(ptxt); upos.print("abc n"); upos.print("abc n"); upos.print("nnnnnnnnn"); upos.print(new String(cutP)); upos.releaseUsb(); } public void setEndpoint(byte endpoint) { this.endpoint = endpoint; } public byte getEndpoint() { return endpoint; } public 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); } 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 void getDeviceHandle(Device device) { 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 void claimDevice(DeviceHandle handle, int interfaceNumber) { int result = LibUsb.claimInterface(handle, interfaceNumber); if (result != LibUsb.SUCCESS) { throw new LibUsbException("Unable to claim interface", result); } } public boolean print(String text) { ByteBuffer buffer = ByteBuffer.allocateDirect(text.getBytes().length); buffer.put(text.getBytes()); IntBuffer transfered = IntBuffer.allocate(3); int result = LibUsb.bulkTransfer(handle, endpoint, buffer, transfered, 3000); if (result != LibUsb.SUCCESS) { System.out.println("EXCEPTION THROWN"); return false; } System.out.println(transfered.get() + " bytes sent"); return true; } } |