Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Java gets serial support with the new javax.comm package

Java gets serial support with the new javax.comm package

Tutorial Details:

Java gets serial support with the new javax.comm package
Java gets serial support with the new javax.comm package
By: By Shivaram H. Mysore and Rinaldo Di Giorgio
Sun's JavaSoft division provides support for RS-232 and parallel devices with standard extensions
Co-authored by Shivaram H. Mysore
he Java Communications (a.k.a. javax.comm) API is a proposed standard extension that enables authors of communications applications to write Java software that accesses communications ports in a platform-independent way. This API may be used to write terminal emulation software, fax software, smart-card reader software, and so on.
Developing good software usually means having some clearly defined interfaces. The high-level diagram of the API interface layers are shown in this figure.
In this article we will show you how to use javax.comm to communicate with a serial device based on RS-232. We'll also discuss what the javax.comm API provides and what it doesn't provide. We'll present a small example program that shows you how to communicate to the serial port using this API. At the end of the article we'll briefly detail how this javax.comm API will work with other device drivers, and we'll go over the requirements for performing a native port of this API to a specific OS.
Unlike classical drivers, which come with their own models of communication of asynchronous events, the javax.comm API provides an event-style interface based on the Java event model (java.awt.event package). Let's say we want to know if there is any new data sitting on the input buffer. We can find that out in two ways -- by polling or listening . With polling, the processor checks the buffer periodically to see if there is any new data in the buffer. With listening, the processor waits for an event to occur in the form of new data in the input buffer. As soon as new data arrives in the buffer, it sends a notification or event to the processor.
Among the various serial interfaces available, two of the most popular are the RS-232C and RS-422 standards, which define the electrical signal levels and the meaning of various signal lines. Low-speed serial interfaces typically clock data out as a square wave, with clock coordination provided by start and stop bits.
RS-232 stands for Recommend Standard 232 ; the C simply refers to the latest revision of the standard. The serial ports on most computers use a subset of the RS-232C standard. The full RS-232C standard specifies a 25-pin "D" connector, of which 22 pins are used. Most of these pins are not needed for normal PC communications, and indeed, most new PCs are equipped with male D-type connectors having only 9 pins. For more on RS-232, see the Resources section .
Note: For an understanding of what other drivers have done in the past, take a look at the Unix termio manual page or OpenBSD Unix, a variation of the BSD Unix driver source. This is available free on the Internet. Please see the Resources section for more information.
The javax.comm API: What is provided
The javax.comm API provides the following functionality to developers:
A complete API specification for serial and parallel communication ports. (In this article we consider serial ports only.) Without a common API in your development efforts, workload will increase because you'll have to provide support to serial devices.
Full control of all serial framing parameters (baud stop bits, parity, bits/frame) as well as manual or automatic control of the flow control lines. Normally, in RS-232, there are two signal lines and the rest are intended for control lines. Depending on the type of communication (synchronous or asynchronous), the number of control lines selected may vary. This API provides access to the underlying control signals.
A brief diversion here may help you understand something about parity and start and stop bits. Parity was added to RS-232 because communication lines can be noisy. Let's say we send ASCII 0 , which in hex equals 0x30 (or 00110000 in binary), but along the way someone passes by holding a magnet, causing one of the bits to change. As a result, instead of sending 8 bits as intended, an additional bit is added to the first string of bits sent, making the sum total of bits sent even or odd. voilą ! You've got parity.
Start and stop bits were added to the serial communication protocol to allow the receivers to synchronize on the characters being sent. One-bit parity does not allow error correction -- only detection. Solutions to this problem come from protocols that are layered on top of the serial APIs. Most serial communication these days uses block protocols with checksums (a mathematical function that can be generated on the receiver and compared to the transmitted checksum) that allow errors to be detected on larger groups of bits. When you are communicating with your ISP over PPP, packets may be 128 bytes per packet with a checksum. If they match, you are 99.999% sure the data is okay.
There are cases wherein this scheme doesn't work. For example, when sending critical commands to devices that are very far out in the solar system, forward correcting protocols can be used. Forward correcting protocols are needed because there may be no time for a retransmission, and space has a lot of electromagnetic noise.
Okay, back to the list of functionalities provided by the javax.comm API!
The basic I/O via a subclass of Java IO streams. For input and output, the javax.comm API uses streams; the concept of streams should be familiar to all Java programmers. It is important to reuse Java concepts when building new functionality or the APIs will become unwieldy.
Streams that can be extended to provide client flow control and threshold controls. For example, you may want an alert when there are 10 characters in the buffer or when there are only 10 locations left for characters. Flow control is important when the two devices connected via an interface cannot keep up with each other. Without flow control, you can have overruns or underruns . In the overrun condition, you received data before it was processed so it was lost; in the underrun, you were ready for data but it was not available. Usually these conditions occur at the USART (Universal Synchronous Asynchronous Receiver Transmitter), which is hardware that converts bytes to a serial wave form with timing to match the baud rate.
The javax.comm API uses the Java event model to provide notification of various signal line changes as well as buffer status. State changes refer to well-defined signals specified in the RS-232 standard. For example, carrier detect is used by a modem to signal that it has made a connection with another modem, or it has detected a carrier tone. Making the connection or detecting a carrier tone is an event. Event detection and notification of changes is implemented in this API.
What is not provided
The javax.comm API does not provide:
Line discipline type processing, dialer management, or modem management. Line discipline refers to additional processing of input or output characters. For example, one common post-processing option is the conversion of CR to CR LF. These terms have their origins in the early days of teletypes. CR (carriage return) means to simple-return the carriage to the left margin; in the Arabic world, this would be the right margin. LF (line feed) advances the printing area up by one. When bitmap screens and laser printers came along, these terms became less important.
Dialer management and modem management are additional applications that can be written using the javax.comm API. Dialer management typically provides an interface to the modem management's AT command interface. Almost all modems have an AT command interface. This interface is documented in modem manuals.
Perhaps a little example will make this concept clear. Suppose we have a modem on COM1 and we want to dial a phone number. A Java dialer management application will query for the phone number and interrogate the modem. These commands are carried by javax.comm, which does no interpretation. To dial the number 918003210288, for example, the dialer management probably sends an "AT," hoping to get back an "OK," followed by ATDT918003210288. One of the most important tasks of dialer management and modem management is to deal with errors and timeouts.
GUI for serial port management. Normally, serial ports have a dialog box that configures the serial ports, allowing users to set parameters such as baud rate, parity, and so on. The following diagram depicts the objects involved in reading and/or writing data to a serial port from Java.
Support for X, Y, and Z modem protocols. These protocols provide support error detection and correction.
The programming basics
Too often, programmers dive right into a project and code interactively with an API on the screen without giving any thought to the problem they are trying to solve. To avoid confusion and potential problems, gather the following information before you start a project. Remember, programming devices usually requires that you consult a manual.
Get the manual for the device and read the section on the RS-232 interface and RS-232 protocol. Most devices have a protocol that must be followed. This protocol will be carried by the javax.comm API and delivered to the device. The device will decode the protocol, and you will have to pay close attention to sending data back and forth. Not getting the initial set-up correct can mean your application won't start, so take the time to test things out with a simple application. In other words, create an application that can simply write data onto the serial port and then read data from the serial port using the javax.comm API.
Try to get some code samples from the manufacturer. Even if they are in another language, these examples can be quite useful.
Find and code the smallest example you can to verify that you can communicate with the device. In the


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Java gets serial support with the new javax.comm package

View Tutorial:
Java gets serial support with the new javax.comm package

Related Tutorials:

Displaying 1 - 50 of about 3818 Related Tutorials.

Package categories in Java
Package categories in Java Package categories in Java           ....  java.nio This package handles New I/O
 
New to Java?
New to Java - New to java tutorial New to Java...;   If you are new to Java technology and you want to learn... quickly and easily. We have tried to put support examples related to each Java
 
New to Java?
New to Java - New to java tutorial New to Java...;   If you are new to Java technology and you want to learn... quickly and easily. We have tried to put support examples related to each Java
 
New Features of JAVA SE 6.
New Features of java SE 6. New Features of JAVA SE 6.... Changes in I/O This is a new feature added in Java SE 6... deleted in JAVA SE 6 and JPDA has been added. Some new Methods are included like
 
Java package
Java package,Java packages Java package...; Introduction to Java Package Package is a mechanism for organizing..., usability and category.  An example of package is the JDK package of SUN Java
 
Java AWT Package Example
AWT Java AWT Package Example  ...; In this section you will learn about the AWT package of the Java. Many running... will learn about the different components available in the Java AWT package
 
Java util package Examples
;   The util package or java provides many utility interfaces and classes for easy manipulation of in-memory data. The java util package tutorials  at RoseIndia.net introduces you with the Java util package of JDK. All
 
Java AWT Package Example
AWT Java AWT Package Example  ...; In this section you will learn about the AWT package of the Java. Many running... will learn about the different components available in the Java AWT package
 
Java AWT Package Example
AWT Java AWT Package Example  ...; In this section you will learn about the AWT package of the Java. Many running... will learn about the different components available in the Java AWT package
 
New to programming...
Java Programing, New to Java Programming, New in Java Programming New...... Break the old rhythm. Explore the new horizons. 
 
Create Your Own Package
of the java class that includes its package name.  Now run the program... Create Your Own Package Create Your Own Package...;  The package to which the source file belongs is specified
 
java.applet package examples
Java Applets,Free Java Applet,Java Applets Tutorial,Download Java Applets,Java Applet Tutorials java.applet package examples... the different methods of an applet.    HTML for Java
 
java.applet package examples
Java Applets,Free Java Applet,Java Applets Tutorial,Download Java Applets,Java Applet Tutorials java.applet package examples... the different methods of an applet.    HTML for Java
 
java.applet package examples
Java Applets,Free Java Applet,Java Applets Tutorial,Download Java Applets,Java Applet Tutorials java.applet package examples... the different methods of an applet.    HTML for Java
 
How to import a package
How to import a package How to import a package... in package. Declaring the fully-qualified class name. For example, mypackage.HelloWorld helloworld = new mypackage.HelloWorld
 
Java program to get class name without package
Java Get Example Java program to get class name... package. For this purpose we have to fetch first the full class name with package and after then we will remove the package name with string manipulation functions
 
Java Util Package - Utility Package of Java
Java Util Package,Java Util Packages,Java Util Package Examples - Java Util Tutorials Java Util Package - Utility Package...;   Java Utility package is one of the most commonly used
 
New Features in JDBC 4.0
JDBC4.0,JDBC Features,New Features in JDBC API 6.0 New Features in JDBC 4.0         ...;     Introduction Java database connectivity (JDBC
 
Inserting a New Entry in a List
Inserting a New Entry in a List,XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials Inserting a New Entry in a List...; This Example shows you how to Insert a new Entry in a list  in a DOM document
 
Dialog and Console Input-Output
Java: Dialog and Console Input-Output... Java NotesDialog and Console Input-Output        ...;   This is similar to the previous program, but it also gets input from
 
How to Create New Excel Sheet Using JSP
a new  excel sheet using java .You can create any number of  new excel sheets... Create New Excel Sheet,How to Make a New Excel Sheet Using JSP,Creating Excel Sheet With JSP How to create new excel sheet using
 
Java Get classes In Package
Java Get classes In Package Java Get classes In Package          ... the classes from the package by providing the path of the jar file and the package
 
Create Subpackages (i.e. A Package inside another package)
Create Subpackages (i.e. A Package inside another package) Create Subpackages (i.e. A Package inside another package...; We can also put a package inside an another package. The packages that comes
 
Keyboard
Java Notes: Keyboard Java Notes: Keyboard Not normally used. You don't normally need to capture... in keyPressed(). Warning: This was written for Java 1.2. In more recent versions
 
New Page 1
and use application data without using java code. EL was introduced in JSTL 1.0... be a map key.. If the first value is a Java Bean, then second value must be a bean...; The Person Bean: package myPack; public class Person {      
 
Adding a New Column Name in Database Table
JDBC Example,JDBC Example MySQL,JDBC Examples in Java,JDBC Add Column,Add New Column,Add New Column in MySQL,Add New Column in MySQL,Adding a New Column Name in Database Table Adding a New Column Name
 
Introduction to java.sql package
Title Introduction to java.sql package...;  This package provides the APIs for accessing and processing... the java programming language. It includes a framework where we different drivers
 
JSP 2.0 - New Features
;    JSP 2.0  is released with new promises. JSP 2.0 is an upgrade to JSP 1.2 with several new and interesting features... with the objective of making the life of Developers easy. Here is the new features of JSP 2.0
 
ObjecteeringUML for Java Enterprise Edition
for Java is a new "all-in-one" product package, based on the Enterprise... ObjecteeringUML for Java Enterprise Edition ObjecteeringUML for Java Enterprise Edition    
 
Introduction to the JSP Java Server Pages
This section shows you how to import a java package or the class in your jsp... JSP Tutorial - Java Server Pages Tutorials JSP... with working source code. Introduction to JSP Java Server Pages
 
Example of Variable Support Tags of JSTL Core Tag Library
Example of Variable Support Tags of JSTL Core Tag Library Example of Variable Support Tags of JSTL Core Tag Library...; JSTL( Java Sever pages Standard Tag Library) provide simple tags of core
 
Session Tracking
the information. It always treats each request as a new request. In Http client makes a connection to the server, sends the request., gets the response... session ID and gives it back to the client along with the response. This ID gets
 
Haskell development support
Eclipse Plugin-Language Haskell development support... We extend the Eclipse IDE to support development in functional programming languages. The goal is to provide support for a wide range of tools (compilers
 
Java - Opening a url in new window from an applet
Open New URL in Java,Opening New Browser from Applet,How to Open Browser from Applet Java - Opening a url in new window from... a new window from an applet. You can use the code given in this program to open
 
Open Source ERP
; CRM Solution Compiere?s ERP software gets the right information... that keeps him afloat?paid support contracts?the response rate so far is worse than what spam gets. Of more than 600,000 downloads of Compiere, 50 customers have signed
 
Java class in JSP
a package inside which there is java class. We will import the package by using... declared in the java class. This package is used inside the page directive. Now make... Java class in JSP Java class in JSP
 
Old and New Vector Methods
Java: Old and New Vector Methods Java: Old and New Vector Methods When the new Collections API was introduced in Java 2 to provide uniform data structure classes, the Vector class
 
Subversive
; The Subversive project is a brand new Eclipse plug-in that provides Subversion support. From a user point of view, Subversive provides Subversion support similar to CVS support, which is already part of the standard Eclipse platform
 
Java as an Internet Language
and devices. But later new features were added to it and it was renamed as Java. Java... Environment (JRE). Support to Internet Protocols: Java has a rich variety of classes... package jipxhtml is developed in java to parse and create the html 4.0 documents
 
Replacing a Node with a New One
Replacing a Node with a New One, XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials Replacing a Node with a New One.... JAXP (Java API for XML Processing) is an interface which provides parsing
 
Java 5 Features
you with the new features on Java 5. Here you will learn about the exciting new... understand the features of Java 5.0. New Features in JDK 5 Here are the list... Java 5 Features,JDK 5.0 Latest Features,Java 5 Language Features
 
The development Zenwalk Linux 2.1 (Core) release
dependency support. Some improvements are included in the new version of Zenwalk...! The new version of Zenwalk core includes the following improvements: based on Linux kernel version 2.6.14.4 with Reiser4 support; SGI XFS support was added
 
Java Util Examples List
;   The util package or java provides many utility interfaces and classes for easy manipulation of in-memory data. The java util package tutorials  at RoseIndia.net introduces you with the Java util package of JDK. All
 
The CodeGenClipse project
platform (v3.1.2 and v3.2) and the Java (v1.5+) by adding IDE support for code generation tools. Initially, the end-user support is focused on support for the Jostraca...; * Open new/other/CodeGenClipse/Jostraca Java Template  * Fillout template
 
Working with java Collections class
to wok with Collections class of java.util package. Actually java collections... Working with java Collections class Working with java Collections class      
 
Core Java Interview Questions!
before the instance is created of class by using new operator and gets destroyed... Core Java Interview Questions! Core Java Interview... is a set of classes and interfaces that support operation on collections
 
Java Illegal State Exception
;     The java. lang package include several classes... Java Illegal State Exception Java Illegal State... a list of exception. while some other classes are checked. Java Illegal State
 
Java SE 6
examples.  These are the major new Features in JAVA SE 6... with the new exciting features of Java SE 6.      ...;    Java 6.0 New Features (Collection Framework
 
Java Features
. Hence Java was designed to support applications on network. This feature of Java... Java Features Java Features   ... independent) is one of the important key feature of java language that makes java
 
Java Get Host Name
Java Get Host Name Java Get Host Name...;  In this Example you will learn how to get host name in Java. Go through the below given code to understand how it works and gets the host name
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.