How to access USB ports in java [closed]

时光毁灭记忆、已成空白 提交于 2019-12-17 07:25:32

问题


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.

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.

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)?


回答1:


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.

If you want to implement a cross-platform USB application, really your best bet is to write an abstract JNI interface that talks to Linux, Mac and Windows native libraries that you'll have to write yourself. I'd look at LibUSB to 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.




回答2:


Been using usb4java for a year on cross platfom (Linux and Windows) and it works great.

See:

http://usb4java.org/

They are very active and have a very good javax USB front.




回答3:


You might want to have a look at usb4java - http://usb4java.org/index.html it 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.




回答4:


See the jUSB or usb4java libraries.




回答5:


The Java Communications API. This should provide similar functionality to the C# System.IO.Ports namespace.




回答6:


If you're on a mac, USB's can be accessed with the /Volumes/ directory.

Example:

You want to write to a file in "Drive", so the filepath would be:

/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



来源:https://stackoverflow.com/questions/2121508/how-to-access-usb-ports-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!