首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >并行口通信Rxtx或javax.comm

并行口通信Rxtx或javax.comm
EN

Stack Overflow用户
提问于 2016-04-05 14:36:42
回答 1查看 1.5K关注 0票数 2

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

代码语言:javascript
复制
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[]获得了Converting char[] to byte[]()函数。我还直接尝试了spam.getBytes(),这并没有什么不同。

在使用javax.comm包运行此代码后,将无法识别并行端口。如果我使用RXTX(gnu.io)运行代码,就会得到一个IOException。然后,整个打印输出如下

代码语言:javascript
复制
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和ActiView软件)来测量大脑活动,不幸的是,我没有可能改变这一点。并口-USB转换器也是没有选择的.(但奇怪的是,如此先进的技术却使用了如此过时的硬件)。

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-08 12:47:52

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36429473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档