我正在尝试使用usb4java (低级API)与一个连接USB的科学仪器进行通信,该仪器被设为x86和x86_64上的HID设备。有一个非常老的C++应用程序可以很好地工作,但出于各种原因,我试图用纯Java替换它。
我可以获得设备描述符并确定接口/端点,但是异步传输永远不会发生(由设备监视Studio检查),回调也不会被调用。usb4java不会抛出任何异常,也不知道是否可以在libusb级别访问任何日志(如果可能的话)。在处理硬件方面,我是一个完全的新手,所以它很可能是我所缺少的最基本的东西。
只有一个端点,我不知道C++代码是如何管理双向通信的。代码中没有明确提到传输类型,因为所有细节都隐藏在第三方库中。
我安装了libusbK驱动程序,并尝试了高级实现(Javax)。转机还是没能通过。
下面是端点描述符转储的输出,以及代码的缩写版本。
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 8
bInterval 100
extralen 0
extra:下面是代码:
import java.nio.*;
import java.util.*;
import org.usb4java.*;
/**
* This class is intended to test USB communication using usb4java
*
*/
public class Test {
private static final String vendorId = "VVV";
private static final String productId = "PPP";
private Context context;
private Device device;
private Handle handle;
static volatile boolean exit = false;
Object synchObj = new Object();
boolean readCompleted = false;
private static final int BUFFER_SIZE = 8;
private final byte[] idQuery = new byte[]{ 0x1d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
public Test(){
initialize();
if (device == null){
System.out.println("No target devices found");
System.exit(0);
}
boolean loop = true;
//claim interface 0
int result = LibUsb.claimInterface(handle, 0);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to claim interface", result);
//this code doesn't get executed because the transfer never completes correctly
final TransferCallback readCallback = new TransferCallback(){
public void processTransfer(Transfer transfer){
System.out.println(transfer.actualLength() + " bytes sent");
//read the response here
synchronized (synchObj){
readCompleted = true;
synchObj.notify();
}
}
};
//initial request writing the device identification query
write(handle, idQuery, readCallback);
//waiting for the write/response to complete
while (loop){
synchronized (synchObj){
while (!readCompleted){
try{
synchObj.wait();
}
catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
readCompleted = false;
loop = false;
System.out.println("Completed reading");
}
result = LibUsb.releaseInterface(handle, 0);
LibUsb.close(handle);
}
private void initialize(){
try{
context = new Context();
int result = LibUsb.init(context);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to initialize libusb.", result);
DeviceList deviceList = new DeviceList();
result = LibUsb.getDeviceList(context, deviceList);
if (result < 0) throw new LibUsbException("Unable to get device list.", result);
for (int i = 0; i < deviceList.getSize(); i++){
Device dev = deviceList.get(i);
DeviceHandle h = new DeviceHandle();
int res = LibUsb.open(dev, h);
if (res != LibUsb.SUCCESS) continue;
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(dev, descriptor);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to read device descriptor", result);
String dump = descriptor.dump(h);
if (dump.indexOf("PPP") > -1){
device = dev;
handle = h;
System.out.println("Found target device");
break;
}
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
public static void write(DeviceHandle handle, byte[] data, TransferCallback callback){
ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.length);
buffer.put(data);
Transfer transfer = LibUsb.allocTransfer();
LibUsb.fillInterruptTransfer(transfer, handle, (byte)0x81, buffer, callback, null, 1000);
System.out.println("Sending " + data.length + " bytes to device");
int result = LibUsb.submitTransfer(transfer);
if (result != LibUsb.SUCCESS) throw new LibUsbException("Unable to submit transfer", result);
}
public static void main(String[] args){
Test app = new Test();
}
}谢谢您的建议。
发布于 2016-09-21 08:48:56
首先:如果你不喜欢摆弄和干预,我建议你最好还是接受已经提供的软件。前方有大量的工作,因为这是一种科学装置,它有可能扰乱你的读数。
现在来解释为什么这不能工作:有趣的是这段bEndpointAddress 0x81 EP 1 IN。它告诉我们的是,有一个端点可以向主机发送数据。是的,您可以而不是写入此端点。这将等到设备有了要发送的数据,并简单地覆盖您提供的缓冲区中的数据。
为什么会这样:你在和HID打交道。更好地阅读它,因为您不会得到简单的ASCII缓冲区,而是特殊格式的字节数组,必须根据HID协议进行解释。
向此设备发送数据的唯一方法是在端点0x00上使用控制传输。
最简单的方法是使用c++软件和usb,并试图理解数据的来回。
https://stackoverflow.com/questions/39604836
复制相似问题