【问题标题】:Java how to write to a Parallel port (Windows 7) - code not workingJava如何写入并行端口(Windows 7)-代码不起作用
【发布时间】:2012-09-24 07:08:51
【问题描述】:

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

【问题讨论】:

  • 什么都没有发生?甚至你的第一个 System.out.println("Port : " + PARALLEL_PORT + " is detected"); 都没有运行?
  • 你必须先设置java通信库的路径

标签: java windows-7 parallel-port


【解决方案1】:

你有 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

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多