Java如何写入并行端口(Windows 7) - 代码不起作用



我必须(写入)将数据"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();
    }
}

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

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

  1. 程序文件\Java\jdk1.7.0_21\lib
  2. 程序文件\Java\jdk1.7.0_21\bin
  3. 程序文件\Java\jdk1.7.0_21\jre\bin
  4. 程序文件\Java\jdk1.7.0_21\jre\lib
  5. 程序文件\Java\jdk1.7.0_21\jre\lib\ext

相关内容

最新更新