javawindows-7parallel-port

Java how to write to a Parallel port (Windows 7) - code not working


I have to (write) output a data "0x01" to a parallel port. I'm using the following code to write to the port but it does not write, the program just runs and nothing happens.

import java.io.*;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.ParallelPort;
import javax.comm.PortInUseException;

public class ParallelIO {

    private static OutputStream outputStream;
    private static InputStream inputStream;
    private static ParallelPort parallelPort;
    private static CommPortIdentifier port;
    static byte dat = 0x02;
    public static final String PARALLEL_PORT = "LPT1";

    public ParallelIO() {
        try {
            //detec the port
            System.out.println("Port : " + PARALLEL_PORT + " is detected");
            // get the parallel port connected to the output
            port = CommPortIdentifier.getPortIdentifier(PARALLEL_PORT);
            //port identified
            System.out.println("Port identified : " + port);
            // open the parallel port --
            //port(App name, timeout);
            parallelPort = (ParallelPort) port.open("0x0378", 50);
            //port opened
            System.out.println("Port opened : " + parallelPort);
            outputStream = parallelPort.getOutputStream();
            //get output
            System.out.println("Out put taken : " + outputStream);
            outputStream.write(dat);
            //data written
            System.out.println("Data Written : " + dat);
            outputStream.flush();
            outputStream.close();

        } catch (NoSuchPortException nspe) {
            System.out.println("\nPrinter Port LPT1 not found :     NoSuchPortException.\nException:\n" + nspe + "\n");
        } catch (PortInUseException piue) {
            System.out.println("\nPrinter Port LPT1 is in use : " +     "PortInUseException.\nException:\n" + piue + "\n");
        } catch (IOException ioe) {
            System.out.println("\nPrinter Port LPT1 failed to write : " + "IOException.\nException:\n" + ioe + "\n");
        } catch (Exception e) {
            System.out.println("\nFailed to open Printer Port LPT1 with exception : " + e +   "\n");
        } finally {
            if (port != null && port.isCurrentlyOwned()) {
                parallelPort.close();
            }
            System.out.println("Closed all resources.\n");
        }
    }

    public static void main(String[] args) {
        ParallelIO parr = new ParallelIO();
    }
}

Solution

  • Do you have comm.jar, win32.dll and javax.comm.properties ?

    you must place these file in to this pass

    1. Program Files\Java\jdk1.7.0_21\lib
    2. Program Files\Java\jdk1.7.0_21\bin
    3. Program Files\Java\jdk1.7.0_21\jre\bin
    4. Program Files\Java\jdk1.7.0_21\jre\lib
    5. Program Files\Java\jdk1.7.0_21\jre\lib\ext