使用Java读取USB设备的最佳方法是什么?



我想写一个与机械臂相连的应用程序,机械臂通过USB连接到我的电脑上。我想要使用Java .

到目前为止,我还没有找到一个可靠的方法来实现这个连接。

我能得到一些关于使用什么和如何使用的建议吗?

下面的代码在win32-x86-64上为我工作:

import org.usb4java.*;

public class SO72998852 {
public static void main(String[] args) {
int status = LibUsb.init(null);
try {
if (status != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to initialize libusb.", status);
}
DeviceList list = new DeviceList();
try {
status = LibUsb.getDeviceList(null, list);
if (status < 0) {
throw new LibUsbException("Unable to get device list", status);
}
for (Device device : list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
status = LibUsb.getDeviceDescriptor(device, descriptor);
if (status != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to read device descriptor", status);
}
System.out.println(descriptor);
}
} finally {
LibUsb.freeDeviceList(list, true);
}
} finally {
LibUsb.exit(null);
}
}
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testing</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.usb4java/usb4java-javax -->
<dependency>
<groupId>org.usb4java</groupId>
<artifactId>usb4java-javax</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>

相关内容

最新更新