devxlogo

Can Java Communicate with Local Serial Port?

Can Java Communicate with Local Serial Port?

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

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

Answer:
Java applications can talk to the local serial porton a system as long as the operating system provides a file systeminterface for accessing the serial port. If the serial portmaps onto a file name, Java programs can treat it like any otherfile on the system by simply creating FileInputStreamobjects that read from the serial port and FileOutputStreamobjects that write to the serial port. Note that this appliesto Java applications only — for Java applets an attempt to accessthe local disk or file system is considered a security violationand is not permitted.

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

               java SerialEcho /dev/ttyaimport 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 notreally files that a program can open directly (even with C or C++). The PCparadigm is closer to the notion of ports and address ranges — both ofwhich areinaccessible by Java programs. To implement this on systems without a filesystem interface to the serial port, you’d have to resort to nativemethods written in C.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist