Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Use native methods to expand the Java environment

Learn how to interact with libraries and applications written in other languages

  • Print
  • Feedback

Page 6 of 7

As I'm sure many of you are already aware, connecting devices to the Java environment can be quite a challenge. At present, the only way to interface to some devices, such as your favorite MIDI board or that cool video feed from your camera, is to use the support provided with the vendor's libraries/DLLs. (DLL stands for Dynamic Link Library and is the method for loading additional code at runtime in the Win32 world.) But computer peripherals are not the only devices to consider. What about cash registers, scanners, or the thousands of devices in a factory? Java can only interface to a device if it is IP addressable or appears as a file in your file system.

Note: The Resources section includes a listing for Central Data's portio, a driver that allows you to use Ethernet to talk to serial ports. The portio package is not you standard IP addressable serial port; rather it appears as a file in your file system. Although many industry analysts and top players believe that eventually all devices will become network addressable, this approach is currently cost prohibitive.

The most commonly used device on the PC last year was the serial port. I was surprised it wasn't the network, but you don't plug in/out your network as often as you plug in/out devices from your serial port. Because one of the most precious pieces of real estate on a PC is a serial port, it gets a lot of use. For example, a typical day in the life of my serial port goes like this: I plug in my modem, I unplug my modem; I plug in my Smart Card Reader, I unplug my Smart Card Reader; I plug in my ISDN console monitor, I unplug my ISDN console monitor; I plug in my HUB console monitor, I unplug my HUB console monitor; and so on. That's a lot of devices, and I cannot use any of them from a Java application. It is possible to design a Java application that allows access to serial ports because many target systems have this support built into the OS. Eventually Java will also provide this type of support. However, if you can't wait, you can use native methods to interface directly to the serial support routines in the underlying OS.

As you well know, you cannot open files from an applet, but you can open files from a Java application. To access a serial port, the obvious answer is to open the serial port from an application. Unfortunately, the obvious is not always the easiest. You'd think you could simply open the port and proceed with business (opening /dev/ttya for Unix or COMx: for Windows 95/NT), but it's not that easy.

Unix users get the big break (no surprise here). When I tested this approach on Unix I was able to open the serial port as a file, just as the original designers of Unix intended. On Windows 95, however, it was a whole different ballgame. It appears that the I/O subsystem requires a different interface to open a serial port. But that's not all. Even if you could access the correct interface, you would come face to face with an even larger problem: Serial devices often require you to adjust parity and baud rate settings. In case you are unfamiliar with communications terminology, baud rate is the rate at which transitions are transmitted. For example, a 28.8K modem is a modem that can send 28,000 bits per second (BPS). Parity has to do with counting of the bits about to be sent and sending a bit that makes the total number of ones or zeros even or odd, hence even and odd parity. You'll also encounter stop bits, which provide the ability to send some preamble bits to allow receivers to synchronize. Users often change the settings of the serial port when they are moving devices around. What this means for you is that once you open the port you may need to set or change the the baud rate and/or the parity. Quality serial drivers will allow you to access every parameter.

  • Print
  • Feedback

Resources