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:
            default:
                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 cmcmanis@freegate.com");
                break;
            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]);
                        System.exit(1);
                    }
                    tape = new byte[fis.available()];
                    fis.read(tape);
                    fis.close();
                    fis = null;
                }
                break;
        }
        cp = CommPortIdentifier.getPortIdentifier("COM3");
        if (cp == null) {
            System.out.println("Can't get COM3 handle.");
            System.exit(1);
        }
        port = (SerialPort)cp.openPort("SendTape", 100);
        if (port == null) {
            System.out.println("Unable to open the com port.");
            System.exit(1);
        }
        port.setSerialPortParams(110, SerialPort.DATABITS_8,
                                          SerialPort.STOPBITS_2,
                                          SerialPort.PARITY_NONE);
        port.setFlowcontrolMode(SerialPort.FLOWCTRL_NONE);
        OutputStream os = port.getOutputStream();
        addr = 0; op_code = 0;
        state = LEADER;
        for (int i = 0; i < tape.length; i++) {
            state = newState(state, tape[i]);
            os.write(tape[i]);
            os.flush();
            Thread.sleep(100); // let the PDP-8 digest this byte
        }
        port.closePort();
    }
}

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 http://www.javaworld.com/jw-09-1998/indepth/SimpleWrite.java
  • A paper tape reader simulator for loading PDP-8 programs from a Java-capable computer http://www.javaworld.com/jw-09-1998/indepth/SendTape.java
  • 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" http://www.javaworld.com/jw-05-1998/jw-05-javadev.html
  • Early access pages on Java Developer Connection (registration required) http://developer.java.sun.com/developer/earlyAccess/index.htm
  • Early access versions of the Java Communications API (registration required) http://developer.java.sun.com/developer/earlyAccess/communications.html
  • Doug Jones' page describing the PDP-8's history and pointers to his FTP archive http://www.cs.uiowa.edu/~jones/pdp8/
  • Previous Java In Depth articles http://www.javaworld.com/topicalindex/jw-ti-indepth.html
1 2 Page 2
Page 2 of 2