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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.