Работа с usb java

How to access USB ports in java [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

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

Читайте также:  transform

6 Answers 6

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

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

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.

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.

Linking with LGPL libraries should not be an issue, if you are going to only use their APIs in your code (and you have a non LGPL license). GPL licensed libraries are copyleft licenses, forcing your code’s license also to be GPL license, regardless of what license you have used.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

USB library for Java based on libusb

License

usb4java/usb4java

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

USB library for Java based on libusb 1.0

Copyright (C) 2011 Klaus Reimer, k@ailis.de
Copyright (C) 2013 Luca Longinotti, l@longi.li

See LICENSE.md for licensing information.

This library can be used to access USB devices in Java. It is based on the native libusb 1.0 shared library and reflects this API as complete as possible. usb4java also implements the javax-usb (JSR80) API. So you can choose if you want to access the USB devices via the low-level libusb 1.0 API or via the high level javax-usb API.

The native libraries are included in pre-compiled form for Linux, Windows and Mac OS X in 32 and 64 bit versions. The libraries are automatically extracted and loaded so you don’t need to care about them.

For more detailed information please visit the usb4java website.

About

USB library for Java based on libusb

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

License

manuelbl/JavaDoesUSB

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Java Does USB: USB Library for Java

Java Does USB is a Java library for working with USB devices. It allows to query information about all conntected USB devices and to communicate with USB devices using custom / vendor specific protocols. (It is not intended for communication with standard types of USB devices such as mass storage devices, keyboards etc.)

The library uses the Foreign Function & Memory API to access native APIs of the underlying operating system. It only uses Java code and does not need JNI or any native third-party library. The Foreign Function & Memory API (aka as project Panama) is in preview and will be introduced in a future Java version. Currently, it can be used with Java 19 or Java 20 (with preview features enabled).

Note: The main branch and published versions ≥ 0.5.0 work with JDK 20 only. For JDK 19, use version 0.4.x.

  • Single API for all operating systems (similar to WebUSB API)
  • Enumeration of USB devices
  • Control, bulk and interrupt transfers (optionally with timeout)
  • Notifications about connected/disconnected devices
  • Descriptive information about interfaces, settings and endpoints
  • High-throughput input/output streams
  • Support for alternate interface settings, composite devices and interface association
  • Published on Maven Central
  • Changing configuration: The library selects the first configuration. Changing configurations is rarely used and not supported on Windows (limitation of WinUSB).
  • USB 3.0 streams: Not supported on Windows (limitation of WinUSB).
  • Providing information about USB buses, controllers and hubs

The library is available at Maven Central. To use it, just add it to your Maven or Gradle project.

If you are using Maven, add the below dependency to your pom.xml:

dependency> groupId>net.codecrete.usbgroupId> artifactId>java-does-usbartifactId> version>0.5.1version> dependency>

If you are using Gradle, add the below dependency to your build.gradle file:

compile group: 'net.codecrete.usb', name: 'java-does-usb', version: '0.5.1'
package net.codecrete.usb.sample; import net.codecrete.usb.USB; public class EnumerateDevices < public static void main(String[] args) < for (var device : USB.getAllDevices()) < System.out.println(device); > > >
  • Bulk Transfer demonstrates how to find a USB device, open it and communicate using bulk transfer.
  • Enumeration lists all connected USB devices and displays information about interfaces and endpoints.
  • Monitor lists the connected USB devices and then monitors for USB devices being connected and disconnnected.
  • Device Firmware Upload (DFU) for STM32 uploads firmware to STM32 microcontrollers supporting the built-in DFU mode.
  • Java 20, preview features enabled (available at https://www.azul.com/downloads/?package=jdk)
  • Windows (x86 64-bit), macOS (x86 64-bit, ARM 64-bit) or Linux 64 bit (x86 64-bit, ARM 64-bit)

For JDK 19, use the latest published version 0.4.x.

No special considerations apply. Using this library, a Java application can connect to any USB device and claim any interfaces that isn’t claimed by an operating system driver or another application.

libudev is used to discover and monitor USB devices. It is closely tied to systemd. So the library only runs on Linux distributions with systemd and the related libraries. The majority of Linux distributions suitable for desktop computing (as opposed to distributions optimized for containers) fulfill this requirement.

Similar to macOS, a Java application can connect to any USB device and claim any interfaces that isn’t claimed by an operating system driver or another application.

Most Linux distributions by default set up user accounts without the permission to access USB devices directly. The udev system daemon is responsible for assigning permissions to USB devices. It can be configured to assign specific permissions or ownership:

Create a file called /etc/udev/rules.d/80-javadoesusb-udev.rules with the below content:

SUBSYSTEM=="usb", ATTRS=="cafe", MODE="0666" 

This adds the rule to assign permission mode 0666 to all USB devices with vendor ID 0xCAFE . This unregistered vendor ID is used by the test devices.

The Windows driver model is more rigid than the ones of macOS or Linux. It’s not possible to open any USB device by default. Instead, only devices using the WinUSB driver can be opened. This even applies to devices with no installed driver.

USB devices can implement certain control requests to instruct Windows to automatically install the WinUSB driver (search for WCID or Microsoft OS Compatibility Descriptors). The driver can also be manually installed or replaced using a software called Zadig.

The test devices implement the required control requests. So the driver is installed automatically.

The library has not been tested on Windows for ARM64. It might or might not work.

The Foreign Function & Memory API has not been implemented for 32-bit operating systems / JDKs (and likely never will be).

Many bindings for the native APIs have been generated with jextract. See the jextract subdirectory for more information.

In order to run the unit tests, a special test device must be connected to the computer. See the loopback-stm32 directory.

Tests can be run from the command line:

If they are run from an IDE (such as IntelliJ IDEA), you must likely configure VM options to enable preview features and allow native access:

--enable-preview --enable-native-access=net.codecrete.usb 

Or (if modules are ignored):

--enable-preview --enable-native-access=ALL-UNNAMED 

Источник

Оцените статью