tags:

views:

603

answers:

4

Hi I'm trying to write a java application that accesses the usb ports to some read and writes 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 a found something called jusb but all the post 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 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? Sorry if the question isn't too clear, I'm fairly noob with coding usb IO.

+1  A: 

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

Taylor Leese
System.IO.Ports is for Serial communications unless the OP is implying a serial-to-usb interface?!
tommieb75
Agreed -- System.IO.Ports has no USB interfaces, is only for Com Ports.
Dave Sims
If you're USB device is exposed on a COM port then you can use System.IO.Ports.
Taylor Leese
Taylor: Have you tried it? Does it actually work?
tommieb75
Yes, I have an application that already does this that I use every day. The USB device is an encoder for magnetic stripe cards.
Taylor Leese
So that's a USB to serial cable? unless the USB driver is using serial emulation...interesting...never heard of that angle before...I have used a magnetic card reader which is a serial interface, using a usb cable... but it does not work (the limitation on the driver itself plus the cable was cheap)
tommieb75
It is using a USB-RS232 serial COM port interface, but there is no actual USB to serial cable connected to the devices USB cable.
Taylor Leese
+1  A: 

See Java-USB.

Jim Ferrans
Java-USB is Linux-only. If that's all you need, it may work out, although it doesn't appear to have been updated in a while.
Dave Sims
+1  A: 

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.

Dave Sims
A: 

see this sample whith jni: http://javausbapi.blogspot.com/

jean88