Usb4Java到HID设备
我正在尝试使用Usb4Java Api将原始数据发送到Usb4Java到HID设备,java,usb,nfc,hid,usb4java,Java,Usb,Nfc,Hid,Usb4java,我正在尝试使用Usb4Java Api将原始数据发送到HID设备(NTAG NFC标签)。任何帮助都将不胜感激!我的代码连接到接口和 LibUsb.interruptTransfer(handle, outEndpoint, buffer, transferred, timeout) == LibUsb.SUCCESS. 我认为这意味着中断传输已经成功了?但是,数据没有存储在芯片上。。。我需要联系供应商吗?Api工作正常吗?任何帮助都会很棒 private static final byte
HID设备(NTAG NFC标签)
。任何帮助都将不胜感激!我的代码连接到接口和
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;
}
private static final byte[]消息={0x01,0x21,0x21,0x21,0x21,
0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21,0x21};
/**NFC设备信息**/
私有静态最终短vendorID=0x1fc9;
私有静态最终短productID=0x0088;
私有静态最终字节接口=0;
私有静态最终字节outEndpoint=(字节)0x01;
私有静态最终整数超时=618;
公开转让{
System.out.println(“同步传输已启动…”);
//初始化libusb上下文
int result=LibUsb.init(null);
如果(结果!=LibUsb.SUCCESS)
{
System.err.println(“无法初始化上下文”);
抛出新的libusbeexception(结果);
}否则{
System.out.println(“上下文初始化”);
}
//开放式测试设备(NXP)
DeviceHandle handle=LibUsb.openDeviceWithVidPid(空,供应商ID,
productID);
if(handle==null)
{
System.err.println(“未找到测试设备”);
系统出口(1);
}否则{
System.out.println(“找到设备”);
}
//声明接口
结果=LibUsb.claimInterface(句柄、接口);
如果(结果!=LibUsb.SUCCESS)
{
System.err.println(“无法声明接口”);
抛出新的libusbeexception(结果);
}否则{
System.out.println(“接口声明”);
}
//发送消息
写(句柄、消息);
//释放接口
结果=LibUsb.releaseInterface(句柄,接口);
如果(结果!=LibUsb.SUCCESS)
{
抛出新的LibUsbException(“无法释放接口”,结果);
}
//关闭设备
LibUsb.close(手柄);
//取消初始化libusb上下文
LibUsb.exit(空);
}
公共无效写入(DeviceHandle句柄,字节[]数据)
{
ByteBuffer buffer=BufferUtils.allocateByteBuffer(64);
buffer.put(数据);
IntBuffer transfered=BufferUtils.allocateIntBuffer();
buffer.rewind();
int result=LibUsb.interruptTransfer(句柄、outEndpoint、缓冲区、已传输、超时);
如果(结果!=LibUsb.SUCCESS)
{
System.err.println(“无法发送数据!”);
抛出新的libusbeexception(结果);
}否则{
System.out.println(“写入的数据”);
}
}
输出:
同步传输已开始。。。
上下文初始化
找到设备
接口声明
写入的数据
最后,我的消息格式来自这个C程序,它使用17字节的消息[adress,byte1,…byte16]在12c以上写入同一个芯片(并工作)。
int NTAGI2C_writeMemory(int dev_addr,unsigned char mem_addr,unsigned char mem_data[]))
{
结构i2c_rdwr_ioctl_数据i2c_数据;
结构i2c_msg msg[2];
无符号字符缓冲区[17];
缓冲区[0]=内存地址;
memcpy(缓冲区+1,内存数据,16);
i2c_data.msgs=msg;
i2c_data.nmsgs=1;
i2c_data.msgs[0].addr=dev_addr;
i2c_data.msgs[0]。标志=0;
i2c_data.msgs[0].len=17;
i2c_data.msgs[0].buf=(uu u8*)缓冲区;
int ret=ioctl(fd、I2C_RDWR和I2C_数据);
如果(ret)