我尝试从USB设备读取USB错误5:无法读取数据:找不到实体有人可以帮助我吗? [英] I try to read from a usb device I get USB error 5: Unable to read data: Entity not found can someone help me?

查看:159
本文介绍了我尝试从USB设备读取USB错误5:无法读取数据:找不到实体有人可以帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过usb4java api从USB设备读取内容,但出现错误: USB错误5:无法读取数据:找不到实体 有谁能够帮我? 必须注意,endpoint_in是我从LibUsb.ENDPOINT_IN获得的,并将其传递给读取功能.找到了该设备已被声明,但我无法继续从该设备读取数据.

I try to read from a usb device through usb4java api but I get an error: USB error 5: Unable to read data: Entity not found can anybody help me? It has to be noted that the endpoint_in i get it from LibUsb.ENDPOINT_IN and i pass it to the read funtion The device is found is claimed but I cannot move forward to read from the device.

My code is given below:




import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.swing.JOptionPane;

import org.usb4java.BufferUtils;
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 Test {
 private static byte endpoint;
   // private static final byte IN_ENDPOINT = 0;
private static Device device;
private static   Context context = new Context();
private static DeviceHandle handle;

    public static void main(String[] args) {

        // Create the libusb context
    LibUsb.init(null);
        findDevice(context, (short) (0x046D), (short) (0xC016));
        // 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) {
         endpoint = (byte)LibUsb.getDeviceAddress(device);
          handle = new DeviceHandle();
          JOptionPane.showMessageDialog(null, "endpoint="+endpoint);

        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);
           // read(handle,1000);
            JOptionPane.showMessageDialog(null, "interface="+interfaceNumber);
           read(handle, 2048);






        } 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.allocateDirect(8);
        buffer.put(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
        // 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");
    }

    public static ByteBuffer read(DeviceHandle handle, int size)
    {


         ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(ByteOrder.LITTLE_ENDIAN);
         IntBuffer transferred = BufferUtils.allocateIntBuffer();

       // IntBuffer transferred = BufferUtils.allocateIntBuffer();
        long TIMEOUT = 5000;


        JOptionPane.showMessageDialog(null, "endpoint_in="+ LibUsb.ENDPOINT_IN);
        JOptionPane.showMessageDialog(null, "endpoint_out="+ LibUsb.ENDPOINT_OUT);
        JOptionPane.showMessageDialog(null, "handle="+handle);

        int result = LibUsb.bulkTransfer(handle, (byte)-128, buffer,
            transferred, TIMEOUT);

        JOptionPane.showMessageDialog(null, result);

        if (result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to read data", result);
        }
        else{System.out.println(transferred.get() + " bytes read from device");

        }


        return buffer;
         }

}

设备是USB鼠标,这是问题吗?

The device is an usb mouse is that the problem?

In order to write i use this code (the write function that I call in my main function):

公共静态ByteBuffer写入(DeviceHandle句柄,整数大小) {ByteBuffer buffer = ByteBuffer.allocateDirect(8); buffer.put(new byte [] {'p'});

public static ByteBuffer write(DeviceHandle handle, int size) { ByteBuffer buffer = ByteBuffer.allocateDirect(8); buffer.put(new byte[] { 'p' });

    String text="p";

    //ByteBuffer buffer = ByteBuffer.wrap(text.getBytes());

    IntBuffer transferred = BufferUtils.allocateIntBuffer();
    long TIMEOUT = 5000;
    int result = LibUsb.bulkTransfer(handle, (byte) 0x01, buffer,
            transferred, TIMEOUT);


    if (result != LibUsb.SUCCESS)
    {
        throw new LibUsbException("Unable to write data", result);
    }
    else{System.out.println(transferred.get() + " bytes written to the device");

    }



    return buffer;


}

但是我收到USB错误5:无法写入数据:找不到实体

but I get USB error 5: Unable to write data: Entity not found

Why the entity is not found since I use endpoint 0x01...

推荐答案

  • 从以下URL下载libusbK.
  • 删除Zadig驱动程序.
  • 安装libusbK驱动程序.
  • URL: https://sourceforge.net/projects/libusbk/

    这篇关于我尝试从USB设备读取USB错误5:无法读取数据:找不到实体有人可以帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆