1

在上面的程序中,我尝试使用 Java(即 USB4JAVA 包)Usb 编程向 Atmel Sama5d3Xplained Board 写入和读取数据。
有人请帮我设置波特率...这样我就可以尝试与 Sama5d3Xplained 板通信以读取和写入一些数据。
提前致谢

    public class exampleusb 
{
    private static final byte[] d2=new byte[] {0x12,0x43, 0x4E, 0x58,0x4E, 0x00, 0x01, 0x10,0x10,0x17, 0x01, 0x10, 0x17, 0x43,0x4E,0x4E, 0x01, 0x10,0x17, 0x43,0x4E,0x4E};
    private static final short VENDOR_ID = 0x03eb;
    private static final short PRODUCT_ID = 0x6119;
    private static final byte INTERFACE = 1;
    private static final byte interfaceNum = 1;
    private static final byte IN_ENDPOINT = 0x82;
    private static final byte OUT_ENDPOINT = 0x01;
    private static final int TIMEOUT = 5000;
    public static void write(DeviceHandle handle, byte[] data)
    {
        ByteBuffer b1=BufferUtils.allocateByteBuffer(data.length);
        b1.put(data);
        IntBuffer transferred = BufferUtils.allocateIntBuffer();
        int result = LibUsb.bulkTransfer(handle, OUT_ENDPOINT, b1,transferred, TIMEOUT);
        if(result!=LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to send data", result);
        }else
        {
            System.out.println(transferred.get() + " bytes sent to device");
        }
    }
    public static ByteBuffer read(DeviceHandle handle, int size)
    {
        ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(ByteOrder.LITTLE_ENDIAN);
        IntBuffer transferred = BufferUtils.allocateIntBuffer();
        int result = LibUsb.bulkTransfer(handle, IN_ENDPOINT, buffer,transferred, TIMEOUT);
        if (result != LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to read data", result);
        }
        System.out.println(transferred.get() + " bytes read from device");
        return buffer;
    }
    public static void main(String[] args) throws Exception
    {
        int result=LibUsb.init(null);
        if(result !=LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to initialize libusb", result);
        }else
        {
            System.out.println("libusb initialised");
        }
        DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, VENDOR_ID, PRODUCT_ID);
        if(handle==null)
        {
            System.out.println("test device not found....");
            System.exit(1);
        }else
        {
            System.out.println("device found");
        }
        result =LibUsb.detachKernelDriver(handle, interfaceNum);
        if(result !=LibUsb.SUCCESS)
        {
            throw new LibUsbException("Unable to claim interface", result);
        }
        write(handle, d2);
        ByteBuffer header = read(handle, 20);
    }
}
4

0 回答 0