如何设置BaudRate以便与USB CDC串行通信

时间:2018-06-22 07:34:40

标签: java libusb baud-rate usb4java

在上述程序中,我试图使用Java(即USB4JAVA软件包)Usb编程将数据写入Atmel Sama5d3Xplained板。
有人请帮我设置BaudRate ...,以便我可以尝试与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);
    }
}

0 个答案:

没有答案