Java如何写入并行端口(Windows 7) - 代码无法正常工作

时间:2012-09-24 07:08:52

标签: java windows-7 parallel-port

![在这里输入图像描述] [1]我必须(写)输出一个数据“0x01”到一个并行端口,我使用下面的代码写入端口但它没有写入,程序只是没有任何反应,请为我提供一个写入并行端口的正确解决方案。

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();
}

}

1 个答案:

答案 0 :(得分:0)

你有comm.jar,win32.dll和javax.comm.properties吗?

您必须将这些文件放入此通行证

  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