Skip to content

How can i read the scanner data. #146

Description

@dearming623

I have confirmed that VENDOR_ID and PRODUCT_ID are correct.

Found problem in hidDataReceived did not trigger. The reason is that the read function has not read data.

Please look at the problem. Thank you all.

The operating environment is in the window system.
hidapi is version 0.14.0
The device is a scanning gun for HIM

Here's my code

public class BarcodeScannerReader {

private static final int VENDOR_ID = 0x1eab;
private static final int PRODUCT_ID = 0x1a03;

public static void main(String[] args) throws InterruptedException {
    HidServices hidServices = HidManager.getHidServices();
    hidServices.addHidServicesListener(new HidServicesListener() {
        @Override
        public void hidDeviceAttached(HidServicesEvent event) {
            System.out.println("Device attached: " + event.getHidDevice());
        }

        @Override
        public void hidDeviceDetached(HidServicesEvent event) {
            System.out.println("Device detached: " + event.getHidDevice());
        }

        @Override
        public void hidFailure(HidServicesEvent event) {
            System.out.println("HID failure: " + event);
        }

        @Override
        public void hidDataReceived(HidServicesEvent event) {
            System.out.println("HID hidDataReceived: " + event);
        }
    });

    hidServices.start();
    HidDevice hidDevice = hidServices.getHidDevice(VENDOR_ID, PRODUCT_ID, null);

    if (hidDevice != null) {
        System.out.println("Found device: " + hidDevice);
        byte[] data = new byte[10];
        while (true) {
            int bytesRead = hidDevice.read(data, 1000);
            if (bytesRead > 0) {
                String barcode = new String(data, 0, bytesRead);
                System.out.println("Barcode scanned: " + barcode);
            }
        }
    } else {
        System.out.println("Device not found!");
    }

    hidServices.shutdown();
}

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions