Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
USB HID设备与usb4java之间的通信_Java_Usb_Hid_Usb4java - Fatal编程技术网

USB HID设备与usb4java之间的通信

USB HID设备与usb4java之间的通信,java,usb,hid,usb4java,Java,Usb,Hid,Usb4java,我正在尝试与一个用HID接口编程的嵌入式系统通信。 我得到这个错误: 线程“main”org.usb4java.libusbeexception中出现异常:USB错误0:无法检查内核驱动程序活动:成功 位于com.fabioang.usbcustomhid.USB_CustomHID.main(USB_CustomHID.java:157) 使用以下代码 import java.util.*; import java.nio.ByteBuffer; import org.usb4java.Dev

我正在尝试与一个用HID接口编程的嵌入式系统通信。 我得到这个错误:

线程“main”org.usb4java.libusbeexception中出现异常:USB错误0:无法检查内核驱动程序活动:成功 位于com.fabioang.usbcustomhid.USB_CustomHID.main(USB_CustomHID.java:157)

使用以下代码

import java.util.*;
import java.nio.ByteBuffer;
import org.usb4java.Device;
import org.usb4java.DeviceDescriptor;
import org.usb4java.DeviceHandle;
import org.usb4java.DeviceList;
import org.usb4java.LibUsb;
import org.usb4java.LibUsbException;

/**
 * @author Fabio
 *
 */
public class USB_CustomHID
{
    /** The vendor ID of the custom HID. */
    private static final short VENDOR_ID = 0x0483;

    /** The product ID of the custom HID. */
    private static final short PRODUCT_ID = 0x5750;

    /** The USB communication timeout. */
    private static final int TIMEOUT = 1000;

    /**
     * Searches for the custom HID and returns it. If there are
     * multiple ones attached then this simple demo only returns
     * the first one.
     * 
     * @return The custom HID USB device or null if not found.
     */
    public static Device findCustomHID()
    {
        // Read the USB device list
        DeviceList list = new DeviceList();
        int result = LibUsb.getDeviceList(null, list);
        if (result < 0)
        {
            throw new RuntimeException(
                "Unable to get device list. Result=" + result);
        }

        try
        {
            // Iterate over all devices and scan for the custom HID
            for (Device device: list)
            {
                DeviceDescriptor descriptor = new DeviceDescriptor();
                result = LibUsb.getDeviceDescriptor(device, descriptor);
                if (result < 0)
                {
                    throw new RuntimeException(
                        "Unable to read device descriptor. Result=" + result);
                }
                if (descriptor.idVendor() == VENDOR_ID
                    && descriptor.idProduct() == PRODUCT_ID) return device;
            }
        }
        finally
        {
            // Ensure the allocated device list is freed
            LibUsb.freeDeviceList(list, false); 
        }

        // No custom HID found
        return null;
    }

    /**
     * Sends a message to the custom HID.
     * 
     * @param handle
     *            The USB device handle.
     * @param message
     *            The message to send.
     */
    public static void sendMessage(DeviceHandle handle, byte[] message)
    {
        ByteBuffer buffer = ByteBuffer.allocateDirect(message.length);
        buffer.put(message);
        buffer.rewind();
        int transfered = LibUsb.controlTransfer(handle,
            (byte) (LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_INTERFACE),
            (byte) 0x09, (short) 2, (short) 1, buffer, TIMEOUT);
        if (transfered < 0)
            throw new LibUsbException("Control transfer failed", transfered);
        if (transfered != message.length)
            throw new RuntimeException("Not all data was sent to device");
    }

    /**
     * Sends a command to the missile launcher.
     * 
     * @param handle
     *            The USB device handle.
     * @param command
     *            The command to send.
     */
    public static void ledControl(DeviceHandle handle, int ledNumber, int ledValue)
    {
        byte[] message = new byte[64];
        message[1] = (byte) (ledNumber);
        message[2] = (byte) (ledValue);
        sendMessage(handle, message);
    }


    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println("USB custom HID controller starting...");

        // Initialize the libusb context
        int result = LibUsb.init(null);
        if (result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to initialize libusb", result);
        }

        System.out.println("libusb initialized");

        // Search for the custom HID USB device and stop when not found
        Device device = findCustomHID();
        if (device == null)
        {
            System.err.println("Custom HID not found.");
            System.exit(1);
        }

        System.out.println("Device found");

        // Open the device
        DeviceHandle handle = new DeviceHandle();
        result = LibUsb.open(device, handle);
        if (result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to open USB device", result);
        }
        try
        {
            System.out.println("Opening device");

            // Check if kernel driver is attached to the interface
            int attached = LibUsb.kernelDriverActive(handle, 1);
            if (attached < 0)
            {
                throw new LibUsbException(
                    "Unable to check kernel driver active", result);
            }

            // Detach kernel driver from interface 0 and 1. This can fail if
            // kernel is not attached to the device or operating system
            // doesn't support this operation. These cases are ignored here.
            result = LibUsb.detachKernelDriver(handle, 1);
            if (result != LibUsb.SUCCESS &&
                result != LibUsb.ERROR_NOT_SUPPORTED &&
                result != LibUsb.ERROR_NOT_FOUND)
            {
                throw new LibUsbException("Unable to detach kernel driver",
                    result);
            }

            // Claim interface
            result = LibUsb.claimInterface(handle, 1);
            if (result != LibUsb.SUCCESS)
            {
                throw new LibUsbException("Unable to claim interface", result);
            }

            // Read commands and execute them
            System.out.println("System ready");
            ledControl(handle, 1, 1);

            // Release the interface
            result = LibUsb.releaseInterface(handle, 1);
            if (result != LibUsb.SUCCESS)
            {
                throw new LibUsbException("Unable to release interface", 
                    result);
            }

            // Re-attach kernel driver if needed
            if (attached == 1)
            {
                LibUsb.attachKernelDriver(handle, 1);
                if (result != LibUsb.SUCCESS)
                {
                    throw new LibUsbException(
                        "Unable to re-attach kernel driver", result);
                }
            }

            System.out.println("Exiting");
        }
        finally
        {
            LibUsb.close(handle);
        }

        // Deinitialize the libusb context
        LibUsb.exit(null);
    }
}
import java.util.*;
导入java.nio.ByteBuffer;
导入org.usb4java.Device;
导入org.usb4java.DeviceDescriptor;
导入org.usb4java.DeviceHandle;
导入org.usb4java.DeviceList;
导入org.usb4java.LibUsb;
导入org.usb4java.LibUsbException;
/**
*@作者法比奥
*
*/
公共类USB_自定义HID
{
/**自定义HID的供应商ID*/
私有静态最终短供应商_ID=0x0483;
/**自定义HID的产品ID*/
私有静态最终短产品_ID=0x5750;
/**USB通信超时*/
私有静态最终int超时=1000;
/**
*搜索自定义HID并返回它。如果有
*多个连接,然后这个简单的演示只返回
*第一个。
* 
*@返回自定义HID USB设备,如果未找到,则返回null。
*/
公共静态设备findCustomHID()
{
//阅读USB设备列表
设备列表=新设备列表();
int result=LibUsb.getDeviceList(空,列表);
如果(结果<0)
{
抛出新的运行时异常(
“无法获取设备列表。结果=”+结果);
}
尝试
{
//迭代所有设备并扫描自定义HID
用于(设备:列表)
{
DeviceDescriptor描述符=新的DeviceDescriptor();
结果=LibUsb.getDeviceDescriptor(设备,描述符);
如果(结果<0)
{
抛出新的运行时异常(
“无法读取设备描述符。结果=”+结果);
}
if(descriptor.idVendor()==VENDOR\u ID
&&descriptor.idProduct()==产品ID)返回设备;
}
}
最后
{
//确保释放已分配的设备列表
LibUsb.freeDeviceList(list,false);
}
//找不到自定义隐藏
返回null;
}
/**
*向自定义HID发送消息。
* 
*@param句柄
*USB设备句柄。
*@param消息
*要发送的消息。
*/
公共静态无效发送消息(DeviceHandle句柄,字节[]消息)
{
ByteBuffer buffer=ByteBuffer.allocateDirect(message.length);
buffer.put(消息);
buffer.rewind();
int transferd=LibUsb.controlTransfer(句柄,
(字节)(LibUsb.REQUEST_TYPE_CLASS | LibUsb.RECIPIENT_接口),
(字节)0x09,(短)2,(短)1,缓冲区,超时);
如果(传输<0)
抛出新的libusbeexception(“控制传输失败”,已传输);
if(已传输!=消息长度)
抛出新的RuntimeException(“并非所有数据都已发送到设备”);
}
/**
*向导弹发射器发送命令。
* 
*@param句柄
*USB设备句柄。
*@param命令
*要发送的命令。
*/
公共静态无效ledControl(DeviceHandle句柄、int-ledNumber、int-ledValue)
{
字节[]消息=新字节[64];
消息[1]=(字节)(ledNumber);
消息[2]=(字节)(ledValue);
sendMessage(句柄、消息);
}
/**
*@param args
*/
公共静态void main(字符串[]args)
{
System.out.println(“USB自定义HID控制器启动…”);
//初始化libusb上下文
int result=LibUsb.init(null);
如果(结果!=LibUsb.SUCCESS)
{
抛出新的libusbeexception(“无法初始化libusb”,结果);
}
System.out.println(“libusb初始化”);
//搜索自定义HID USB设备,未找到时停止
设备设备=findCustomHID();
如果(设备==null)
{
System.err.println(“未找到自定义HID”);
系统出口(1);
}
System.out.println(“找到设备”);
//打开设备
DeviceHandle=新的DeviceHandle();
结果=LibUsb.open(设备、手柄);
如果(结果!=LibUsb.SUCCESS)
{
抛出新的libusbeexception(“无法打开USB设备”,结果);
}
尝试
{
System.out.println(“打开装置”);
//检查内核驱动程序是否连接到接口
int attached=LibUsb.kernelDriverActive(句柄,1);
如果(附加<0)
{
抛出新libusbeexception(
“无法检查内核驱动程序活动”,结果);
}
//从接口0和1分离内核驱动程序。如果
//内核未连接到设备或操作系统
//不支持此操作。此处忽略这些情况。
结果=LibUsb.detachKernelDriver(句柄,1);
如果(结果)=LibUsb.SUCCESS&&
结果!=LibUsb.ERROR\u不受支持&&
结果!=LibUsb。错误(未找到)
{
抛出新的LibUsbException(“无法分离内核驱动程序”,
结果);
}
//索赔接口
结果=LibUsb.claimInterface(句柄,1);
如果(结果!=LibUsb.SUCCESS)
{
抛出新的libusbeexception(“无法声明接口”,结果);
}
//读取命令并执行它们
System.out.println(“系统就绪”);
LED控制(手柄,1,1);
//释放接口
结果=LibUsb.releas
// Check if kernel driver must be detached
boolean detach = LibUsb.hasCapability(LibUsb.CAP_SUPPORTS_DETACH_KERNEL_DRIVER) 
   && LibUsb.kernelDriverActive(handle, interfaceNumber);
// Check if kernel driver must be detached
boolean detach = LibUsb.hasCapability(LibUsb.CAP_SUPPORTS_DETACH_KERNEL_DRIVER) 
   && LibUsb.kernelDriverActive(handle, interfaceNumber) == 1;
iface.claim(new UsbInterfacePolicy()
{            
    @Override
    // Force unloading of Kernel driver
    public boolean forceClaim(UsbInterface usbInterface)
    {
        return true;
    }
});