【发布时间】:2016-07-25 13:52:00
【问题描述】:
目前我正在尝试通过 Java 与并行端口通信,但事实证明这很麻烦。我目前正在使用 EEG 进行大脑研究,我想将简单的“事件标记”发送到 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 转换器也不是选项。 (奇怪的是,技术如此先进的东西使用了这种过时的硬件)。
非常感谢!
【问题讨论】:
标签: java byte bytearray outputstream parallel-port