Java 和 HID 通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23796353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java and HID Communication
提问by Will
I'm looking to write a Linux/Windows/Mac Java HID controller for a simple wireless HID interface device. I've tinkered around with the USB4Java LibUsb library to no avail, and I've been steered in the direction of the JavaHIDAPI.
我正在为一个简单的无线 HID 接口设备编写一个 Linux/Windows/Mac Java HID 控制器。我已经对 USB4Java LibUsb 库进行了修改,但无济于事,而且我一直在朝着 JavaHIDAPI 的方向发展。
Unfortunately for me, I really have no idea what I'm doing beyond a pretty decent higher level programming skill set (C#/VB.Net), and switching over to this is killing me.
对我来说不幸的是,除了相当不错的高级编程技能集(C#/VB.Net)之外,我真的不知道我在做什么,而切换到这个让我很伤心。
The directionsthat I found said that I would first need to compile the HIDAPI library found here. It said it would build something called hidapi-jni.dll (which it didn't).
的方向,我发现说,我首先需要编译HIDAPI库中找到这里。它说它会构建一个叫做 hidapi-jni.dll 的东西(它没有)。
Anyway, I think this is where I need to go since the HID does nothing but simply sends and receives signals to and from a wireless control (and responds to signals received).
无论如何,我认为这是我需要去的地方,因为 HID 什么也不做,只是简单地向无线控制发送和接收信号(并响应接收到的信号)。
Is there any step-by-step tutorial for using the JavaHIDAPI for this? Or is there a better library? (I noticed that this is a bit... dated).
是否有使用 JavaHIDAPI 的分步教程?或者有更好的图书馆吗?(我注意到这有点……过时了)。
I tried myself with the following:
我尝试了以下方法:
static{ System.loadLibrary("hidapi"); }
.
.
.
public static void main(string[] Args) throws . . . {
ClassPathLibraryLoader.loadNativeHIDLibrary();
HIDDevice dev = HIDManager.getInstance().openById(VEND_ID, PROD_ID, null);
.
.
.
}
The issue into which I am running is that (besides the fact that the HIDAPI doesnt seem to be building into HIDAPI-jni.dll) is that the HIDManager.openById(. . .) always returns null. Also, I moved the hidapi.dll that was built into the C:...\Java...\bin directory... or something, I'm sure it's right because there wasn't any unsatisfied link error. It's really frustrating because there doesn't seem to be any newbies guide to Java and HID anywhere.
我遇到的问题是(除了 HIDAPI 似乎没有构建到 HIDAPI-jni.dll 中)是 HIDManager.openById(. . .) 总是返回 null。另外,我移动了内置在 C:...\Java...\bin 目录中的 hidapi.dll... 或其他东西,我确定它是正确的,因为没有任何不满意的链接错误。这真的很令人沮丧,因为似乎任何地方都没有任何关于 Java 和 HID 的新手指南。
What am I doing wrong here?
我在这里做错了什么?
采纳答案by Will
So the issue, I discovered, was with native libraries. I was able to get the application to work by copying the .dll from the .jar file and referencing it, but more important, I'm going to rebuild the .java class file responsible for loading the library and add the
所以我发现问题出在本地库上。我能够通过从 .jar 文件复制 .dll 并引用它来使应用程序工作,但更重要的是,我将重建负责加载库的 .java 类文件并添加
System.loadLibrary();
call. When the JavaHidApi
ClassPathLibraryLoader.loadNativeHIDLibrary();
method is called it doesn't load the library upon successfully writing it out to the temp file which is mildly annoying. Doing this will eliminate the necessity for manually loading the library from a static location...
称呼。当该JavaHidApi
ClassPathLibraryLoader.loadNativeHIDLibrary();
方法被调用时,它不会在成功将库写入临时文件后加载库,这有点烦人。这样做将消除从静态位置手动加载库的必要性......
Thank you for pointing me in the correct direction.
谢谢你指出我正确的方向。
回答by Gary Rowe
I'm a bit late to answer this question, but I've written a library called hid4javathat might solve your problem.
我回答这个问题有点晚了,但我写了一个名为hid4java的库,可以解决您的问题。
It's based on Java Native Access (JNA) which is a lot simpler than faffing about with complex JNI incantations.
它基于 Java Native Access (JNA),这比使用复杂的 JNI 咒语要简单得多。
I had to write it because javahidapi is out of date and the underlying signal11/hidapicode has moved on considerably. The older versions of the hidapi library had problems on OS X with attach/detach events which have now been fixed so an upgrade is necessary.
我不得不写它,因为 javahidapi 已经过时了,并且底层的signal11/hidapi代码已经发生了很大的变化。旧版本的 hidapi 库在 OS X 上存在附加/分离事件的问题,这些问题现已修复,因此需要升级。
Also I found it impossible to claim a USB HID device through usb4java on OS X so I ended up writing this library to solve that problem.
此外,我发现无法在 OS X 上通过 usb4java 声明 USB HID 设备,因此我最终编写了这个库来解决该问题。
Hope it helps.
希望能帮助到你。