并行端口通信Rxtx或javax.comm

时间:2016-04-05 14:36:43

标签: java byte bytearray outputstream parallel-port

目前我正在尝试通过Java与并行端口进行通信,但事实证明这很麻烦。我目前正在使用脑电图进行大脑研究,我想发送简单的事件标记"到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)测量大脑活动,遗憾的是,我没有办法改变这种情况。并行端口-USB转换器也是没有选择的。 (奇怪的是,技术先进的东西使用这种过时的硬件)。

非常感谢你!

1 个答案:

答案 0 :(得分:0)

我已经接受Rxtx和javax.comm不再工作了。相反,我通过Python找到了一个解决方法。有关答案,请参阅 Parallel Port Communication with jnpout32pkg / jnpout32reg