如何在java中访问USB端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2121508/
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
How to access USB ports in java
提问by Petezah
I'm trying to write a java application that accesses the usb ports to read from and write to a device connected through usb. The problem I face is that I don't know what exactly to use in java to do such a thing. I searched online and found something called JUSB
but all the posts seem fairly old.
我正在尝试编写一个 Java 应用程序,该应用程序访问 USB 端口以读取和写入通过 USB 连接的设备。我面临的问题是我不知道在 java 中究竟使用什么来做这样的事情。我在网上搜索并找到了一个叫做JUSB
但所有帖子看起来都很旧的东西。
Currently I'm using the RXTX
libraries but I sometimes run into some sync error. When I use C# to do the equivalent it requires far less code and I don't face any of the same sync error.
目前我正在使用这些RXTX
库,但有时会遇到一些同步错误。当我使用 C# 执行等效操作时,它需要的代码要少得多,而且我不会遇到任何相同的同步错误。
My question is, is there anything built into the latest version of the JRE I can use to access the usb ports(that is just as easy as the equivalent C# code)?
我的问题是,在最新版本的 JRE 中是否内置了任何内容,我可以使用它来访问 USB 端口(这与等效的 C# 代码一样简单)?
采纳答案by Dave Sims
There is nothing equivalent to C#'s USB support in Java. Both jUSB and Java-USB are severely out-of-date and likely unusable for any serious application development.
Java 中没有与 C# 的 USB 支持等效的东西。jUSB 和 Java-USB 都严重过时,可能无法用于任何严肃的应用程序开发。
If you want to implement a cross-platform USB application, really your best bet is to write an abstract JNIinterface that talks to Linux, Mac and Windows native libraries that you'll have to write yourself. I'd look at LibUSBto handle Mac and Linux. Windows, as you've seen, is pretty straightforward. I just came off a year-long project that did just this, and unfortunately this is the only serious cross-platform solution. If you don't have to implement on Windows and your needs are limited, you may get by with one of the older Java libs (jUSB or Java-USB). Anything that needs to deploy on Win32/Win64 will need a native component.
如果您想实现一个跨平台的 USB 应用程序,最好的办法就是编写一个抽象的JNI接口,该接口与您必须自己编写的 Linux、Mac 和 Windows 本机库进行对话。我会考虑使用LibUSB来处理 Mac 和 Linux。正如您所见,Windows 非常简单。我刚刚完成了一个为期一年的项目,它就是这样做的,不幸的是,这是唯一严肃的跨平台解决方案。如果您不必在 Windows 上实现并且您的需求有限,您可以使用较旧的 Java 库之一(jUSB 或 Java-USB)。任何需要在 Win32/Win64 上部署的东西都需要一个本机组件。
回答by Taylor Leese
The Java Communications API. This should provide similar functionality to the C# System.IO.Portsnamespace.
将Java通信API。这应该提供与 C# System.IO.Ports命名空间类似的功能。
回答by wombling - Chris Paine
You might want to have a look at usb4java - http://usb4java.org/index.htmlit appears to support Windows, Linux and Mac OS's and appears to be reasonably current at the time of posting. Is unfortunately under the LGPL so may not be suitable for commercial development.
您可能想看看 usb4java - http://usb4java.org/index.html它似乎支持 Windows、Linux 和 Mac OS,并且在发布时似乎是最新的。不幸的是在 LGPL 下所以可能不适合商业开发。
回答by jboisvert
Been using usb4java for a year on cross platfom (Linux and Windows) and it works great.
在跨平台(Linux 和 Windows)上使用 usb4java 一年了,效果很好。
See:
看:
They are very active and have a very good javax USB front.
他们非常活跃并且有一个非常好的 javax USB 前端。
回答by Linny
If you're on a mac, USB's can be accessed with the /Volumes/ directory.
如果您使用的是 mac,则可以使用 /Volumes/ 目录访问 USB。
Example:
例子:
You want to write to a file in "Drive", so the filepath would be:
您想写入“驱动器”中的文件,因此文件路径为:
/Volumes/Drive/file.whatever
/Volumes/Drive/file.whatever
Not sure how it's accomplished on other platforms, but this is the simplest way I've found on a mac
不确定它是如何在其他平台上完成的,但这是我在 Mac 上找到的最简单的方法