3

我正在尝试HID Device (NTAG NFC Tag)使用 Usb4Java Api 将原始数据发送到一个。任何帮助将不胜感激!我的代码连接到接口和

LibUsb.interruptTransfer(handle, outEndpoint, buffer, transferred, timeout) == LibUsb.SUCCESS. 

我认为这意味着中断传输已成功?但是,数据没有存储在芯片上……我需要联系供应商吗?Api 工作正常吗?任何帮助都是极好的!

private static final byte[] MESSAGE = {0x01, 0x21, 0x21, 0x21, 0x21, 0x21,
    0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21};
/**NFC Device Info**/
private static final short vendorID = 0x1fc9;
private static final short productID = 0x0088;
private static final byte Interface = 0;
private static final byte outEndpoint = (byte)0x01;
private static final int timeout = 618;

public void transfer() {
    System.out.println("Sync Transfer started...");
    // Initialize the libusb context
    int result = LibUsb.init(null);
    if (result != LibUsb.SUCCESS)
    {
        System.err.println("Context cannot be initialized.");
        throw new LibUsbException(result);
    } else {
        System.out.println("Context Initialized");
    }

    // Open test device (NXP)
    DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, vendorID,
        productID);
    if (handle == null)
    {
        System.err.println("Test device not found.");
        System.exit(1);
    } else {
        System.out.println("Device Found");
    }

    // Claim the interface
    result = LibUsb.claimInterface(handle, Interface);
    if (result != LibUsb.SUCCESS)
    {
        System.err.println("Unable to claim interface");
        throw new LibUsbException(result);
    } else {
        System.out.println("Interface Claimed");
    }

    // Send Message
    write(handle, message);

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

    // Close the device
    LibUsb.close(handle);

    // Deinitialize the libusb context
    LibUsb.exit(null);
}

public void write(DeviceHandle handle, byte[] data)
{
    ByteBuffer buffer = BufferUtils.allocateByteBuffer(64);
    buffer.put(data);
    IntBuffer transferred = BufferUtils.allocateIntBuffer();
    buffer.rewind();
    int result = LibUsb.interruptTransfer(handle, outEndpoint, buffer, transferred, timeout);
    if (result != LibUsb.SUCCESS)
    {
        System.err.println("Unable to send data!");
        throw new LibUsbException(result);
    } else {
        System.out.println("Data Written");
    }
}

Output:
Sync Transfer started...
Context Initialized
Device Found
Interface Claimed
Data Written


Lastly, my message format comes from this C program that writes to the same chip (and WORKS) over 12c using a 17 byte message [adress, byte1, ... byte16].

int NTAGI2C_writeMemory(int dev_addr,unsigned char mem_addr,unsigned char mem_data[])
{
    struct i2c_rdwr_ioctl_data i2c_data;
    struct i2c_msg msg[2];
    unsigned char buffer[17];

    buffer[0] = mem_addr;
    memcpy(buffer+1, mem_data,16);

    i2c_data.msgs = msg;
    i2c_data.nmsgs = 1;

    i2c_data.msgs[0].addr = dev_addr;
    i2c_data.msgs[0].flags = 0;
    i2c_data.msgs[0].len = 17;
    i2c_data.msgs[0].buf = (__u8*)buffer;

    int ret = ioctl(fd,I2C_RDWR,&i2c_data);
    if(ret<0)
    {   
        perror("write data fail\n");
        return 0;
    }
    usleep(6);
    return 1;
}
4

0 回答 0