Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 20, 1997

Can Java Communicate with Local Serial Port?

Question:
Is it possible to have Java talk to a local serial port on a machine, for example, COM1 or COM2 on a PC?

Is there a way to do this without native methods, and is this a security violation?

Answer:
Java applications can talk to the local serial port on a system as long as the operating system provides a file system interface for accessing the serial port. If the serial port maps onto a file name, Java programs can treat it like any other file on the system by simply creating FileInputStream objects that read from the serial port and FileOutputStream objects that write to the serial port. Note that this applies to Java applications only -- for Java applets an attempt to access the local disk or file system is considered a security violation and is not permitted.

On most unix systems the serial port is accessible via a file name like /dev/ttya or /dev/term/a and really does conform to the file system paradigm. For example, the following Java application reads lines from the serial port and echoes them back onto the serial port. It takes the file name of the serial port as a commandline parameter:

               java SerialEcho /dev/ttya


import java.io.*;

public class SerialEcho {
       //
       // Defining a main() routine allows the program to be
       // run as a standalone java application
       //
       public static void main(String argv[]) {
               if (argv.length != 1) {
                       System.out.println("Usage: SerialEcho
");
                       System.exit(1);
               }

               // get an input stream from the port

               DataInputStream input = null;
               try {
                       input = new DataInputStream(
                                       new FileInputStream(argv[0]));
               } catch (Exception e) {
                       System.out.println("Couldn't open " + argv[0]);
                       System.exit(1);
               }

               // get an output stream to the port

               PrintStream output = null;
               try {
                       output = new PrintStream(new FileOutputStream(argv[0]));
               } catch (Exception e) {
                       System.out.println("Couldn't create " + argv[1]);
                       System.exit(1);
               }

               // forever read a line from the port and echo it back

               while (true) {
                       String line = null;

                       try {
                               line = input.readLine();
                       } catch (Exception e) {
                               System.out.println("Error reading " + argv[0]);
                               System.exit(1);
                       }

                       if (line == null)
                               System.exit(1);
                       System.out.println(line);
                       //
                       // \12 and \15 are carriage-return and line-feed chars
                       //
                       output.print(line + '\12' + '\15');
               }
       }
}
Unfortunately, this doesn't work on Win95/NT because COM ports are not really files that a program can open directly (even with C or C++). The PC paradigm is closer to the notion of ports and address ranges -- both of which are inaccessible by Java programs. To implement this on systems without a file system interface to the serial port, you'd have to resort to native methods written in C.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date