2016-04-05 61 views
2

目前我正尝试通过Java与并行端口进行通信,但事实证明这很麻烦。我目前正在使用EEG进行大脑研究,我想发送简单的“事件标记”到脑电图系统,这必须通过并行端口进行。我已经使用了javax.comm和RXTX,但由于某些原因,我无法将输出写入端口。测试码如下:并行端口通信Rxtx或javax.comm

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"); 
    } 
} 

我得到的toBytes()函数从Converting char[] to byte[]。我也直接尝试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系统(BioSemi ActiveTwo和ActiView软件)来测量大脑活动,可悲的是,我没有可能改变这一点。并行端口-USB转换器也无法选择。 (奇怪的是,如此技术先进的东西使用这种过时的硬件)。

非常感谢!

回答