2016-04-05 12 views
2

現在、私はJava経由でパラレルポートと通信しようとしていますが、これは面倒です。私は現在、脳波を使って脳の研究をしています。単純な「イベントマーカー」を脳波システムに送りたいのですが、これはパラレルポートを経由しなければなりません。私は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"); 
    } 
} 

私は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を使うべきであることを読みました。しかし、私は脳活動を測定する脳波システム(BioSemi ActiveTwo with ActiViewソフトウェア)に取り組んでおり、悲しいかな、これを変更する可能性はありません。パラレルポート-USBコンバータもオプションではありません。 (奇妙なことだが、技術的に高度なものは古いハードウェアを使用している)。

ありがとうございました!

答えて

関連する問題