Opening up new ports to Java with javax.comm

Sometimes a simple set of APIs can be exceptionally powerful

1 2 Page 2
Page 2 of 2
    public static void main(String args[]) throws Exception {
        FileInputStream fis;
        byte tape[] = null;
        int state;
        CommPortIdentifier cp;
        SerialPort port;
        switch (args.length) {
            case 0:
                System.out.println("Chuck McManis' PDP-8 loader program. V1.0");
                System.out.println("Usage is 'java SendTape -b' sends the BinLoader to PDP-8 in RIM format.");
                System.out.println("         'java SendTape foo.bin' sends foo.bin to PDP-8 in BIN format.");
                System.out.println("Send bug reports to");
            case 1:
                if (args[0].compareTo("-b") == 0)
                    tape = binloader;
                else {
                    fis = new FileInputStream(args[0]);
                    if (fis == null) {
                        System.out.println("Couldn't open file "+args[0]);
                    tape = new byte[fis.available()];
                    fis = null;
        cp = CommPortIdentifier.getPortIdentifier("COM3");
        if (cp == null) {
            System.out.println("Can't get COM3 handle.");
        port = (SerialPort)cp.openPort("SendTape", 100);
        if (port == null) {
            System.out.println("Unable to open the com port.");
        port.setSerialPortParams(110, SerialPort.DATABITS_8,
        OutputStream os = port.getOutputStream();
        addr = 0; op_code = 0;
        state = LEADER;
        for (int i = 0; i < tape.length; i++) {
            state = newState(state, tape[i]);
            Thread.sleep(100); // let the PDP-8 digest this byte

Finally, as you can see in the code above, the contents of the file are sent to the PDP-8 one byte at a time with a 100 millisecond (ms) delay between characters.

After reading the documentation for javax.comm, it took me about three hours to code, test, and document the SendTape class. In my opinion, this was much easier than the alternative of using the Win32 API, and it is portable.

Wrapping up

You may have surmised that I'm pretty impressed with the Java Communications API from Sun. It is complex enough to use for difficult tasks without getting in your way when you want to do something simple. It provides hooks into the Java 1.1 event system (which I didn't even go into in this column) that allow for easy integration into JFC-based windowed applications.

Accessing the serial port is quite useful in another area as well -- embedded computer systems. One of my hobbies is building small autonomous robots, and all of my robots load software via the serial port using some protocol that is usually unique to the manufacturer of the computer board used in the robot. I am planning to build some useful interaction tools with my robots in Java now that I can communicate with them easily.

Yes, I did get my PDP-8 running FOCAL-69. FOCAL stands for FORmula CALculator and turns the PDP-8 into a programmable calculator of sorts, which in the 70s was quite useful indeed! The response from my daughter was the predictable, "That is so slow Dad!" -- but I am filled with the memory of having an entire computer at my command. Now that was a power trip.

Chuck McManis currently is a distinguished engineer at FreeGate Corp., a venture-funded startup that is exploring opportunities in the Internet marketplace. Before joining FreeGate, Chuck was a member of the Java Group. He joined the Java Group just after the formation of FirstPerson Inc. and was a member of the portable OS group (the group responsible for the OS portion of Java). Later, when FirstPerson was dissolved, he stayed with the group through the development of the alpha and beta versions of the Java platform. He created the first "all-Java" home page on the Internet when he did the programming for the Java version of the Sun home page in May 1995. He also developed a cryptographic library for Java and versions of the Java class loader that could screen classes based on digital signatures. Before joining FirstPerson, Chuck worked in the operating systems area of SunSoft, developing networking applications. There, he did the initial design of NIS+. Check out his home page.

Learn more about this topic

  • Example program from the javax.comm distribution
  • A paper tape reader simulator for loading PDP-8 programs from a Java-capable computer
  • Rinaldo Di Giorgio devoted his JavaWorld May issue Java Developer column to javax.comm in "Java gets serial support with the new javax.comm package"
  • Early access pages on Java Developer Connection (registration required)
  • Early access versions of the Java Communications API (registration required)
  • Doug Jones' page describing the PDP-8's history and pointers to his FTP archive
  • Previous Java In Depth articles

This story, "Opening up new ports to Java with javax.comm" was originally published by JavaWorld.


Copyright © 1998 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
How to choose a low-code development platform