并口通信 Rxtx 或 javax.comm

Parallel Port communication Rxtx or javax.comm

目前我正在尝试通过 Java 与并行端口进行通信,但事实证明这很麻烦。我目前正在使用 EEG 进行大脑研究,我想将简单的 "event markers" 发送到 EEG 系统,这必须通过并行端口进行。我同时使用了 javax.comm 和 RXTX 但由于某种原因我无法将输出写入端口。测试代码如下:

import gnu.io.*; // RXTX
// import javax.comm.*; // javax.comm

public class PrlCom {
private String msg= "1";

private OutputStream outputStream;
private InputStream inputStream;

private ParallelPort parallelPort; // can be both Rxtx or javax.comm 
private CommPortIdentifier port;

// CONSTANTS
public final String PARALLEL_PORT = "LPT1";
public final String[] PORT_TYPE = { "Serial Port", "Parallel Port" };

public static void main(String[] args) {
    new PrlCom();
}
public PrlCom(){
    openParPort();
}

public void openParPort() {
    try {
        // get the parallel port connected to the EEG-system (used to be printer)
        port = CommPortIdentifier.getPortIdentifier(PARALLEL_PORT);

        System.out.println("\nport.portType = " + port.getPortType());
        System.out.println("port type = " + PORT_TYPE[port.getPortType() - 1]);
        System.out.println("port.name = " + port.getName());

        // open the parallel port -- open(App name, timeout)
        parallelPort = (ParallelPort) port.open("CommTest", 50);
        outputStream = parallelPort.getOutputStream();
        inputStream = parallelPort.getInputStream();

        System.out.println("Write...");
        outputStream.write(toBytes(msg.toCharArray()));
        System.out.println("Flush...");
        outputStream.flush();
    } 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 exeception : " + e + "\n");
    } finally {
        if (port != null && port.isCurrentlyOwned()) {
            parallelPort.close();
        }

        System.out.println("Closed all resources.\n");
     }
}

我从 Converting char[] to byte[] 得到了 toBytes() 函数。我也直接试了spam.getBytes(),也没什么区别

运行将此代码与javax.comm包结合后,并口无法识别。如果我 运行 带有 RXTX(gnu.io) 的代码,我会得到一个 IOException。整个打印输出如下

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7

port.portType = 2
port type = Parallel Port
port.name = LPT1
Output stream opened
Write...

Printer Port LPT1 failed to write : IOException.
Exception:
java.io.IOException: The device is not connected.
    in writeByte

Closed all resources.

有了Rxtx,代码就可以和并口建立连接了。但是,它无法将字节写入输出流。有人可以告诉我如何解决这个问题吗?

我在许多其他主题中读到并行端口是多么过时以及我应该使用 USB。但是,我正在使用 EEG 系统(带有 ActiView 软件的 BioSemi ActiveTwo)来测量大脑 activity,遗憾的是,我无法改变这一点。并行端口-USB 转换器也不是选项。 (虽然奇怪,技术如此先进的东西使用如此过时的硬件)。

非常感谢!

我已经接受 Rxtx 和 javax.comm 不再工作了。相反,我通过 Python 找到了解决方法。答案见