Java serialport关闭块 [英] Java serialport close blocks

查看:420
本文介绍了Java serialport关闭块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与一个单位通信以通过串行连接控制卫星天线。



打开与串行设备的连接:

  import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;

portIdentifier = CommPortIdentifier.getPortIdentifier(device);
serialPort =(SerialPort)portIdentifier.open(name,
serialPort.setSerialPortParams(baudrate,databits,stopbits,parity);
serialPort.setFlowControlMode(flowMode);

bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();

如果设备不可用,我必须在设备重新启动之前清除流才能发送新命令。但输入/输出流上的这种清晰方法会阻止设备关闭流,或关闭SerialDevice块。



有没有办法关闭/清除这些流或SerialDevice而不阻塞?

  bufferedRead.close(); //阻塞直到设备再次启动
outputStream.close(); //阻塞直到设备再次启动
serialPort.close(); //阻止设备再次启动


解决方案

我已经看过这篇文章了,我在使用gnu.io包时也遇到了同样的问题,也就是所谓的RXTX。



这不是最终答案,而是对我找到的替代解决方案的建议。



RXTX在我看来有两个问题,如果不是更多:


  1. 根据您的IDE,您需要为Mac放置: RXTXcomm.jar librxtxSerial .jnilib 和PC: RXTXcomm.jar rxtxSerial.dll 的根目录IDE或Java代码中的项目,因IDE而异。这里的文档没有介绍如何做到这一点,并且在NetBeans,IntelliJ等不同的IDE中,即使我已经在Eclipse和IntelliJ上工作,但还没有在NetBeans上工作。它还有其他一些痛苦的问题。

  2. 根据您的操作系统,即使您启动并运行此软件包,例如在Windows 8.1中,关闭端口也会出现问题。唯一的解决方案是重新启动IDE /控制台并重新连接。每次开发项目时都会疯狂地重新启动IDE。

我花了很多时间寻找解决方案也许对于Windows 8.1及更高版本没有正确关闭端口的解决方案(不知道其他环境关闭端口问题),因为包已经过时且支持有限。



<因此,我建议转到一个名为JSSC的更令人头疼的软件包。



这是一个使用JSSC从串口读取的简单数据:

  public class Main {

public static void main(String [] args){
SerialPort serialPort = new SerialPort ( COM1);
try {
serialPort.openPort(); //打开串口
serialPort.setParams(9600,8,1,0); //设置参数。
byte [] buffer = serialPort.readBytes(10); //从串口读取10个字节
serialPort.closePort(); //关闭串口
}
catch( SerialPortException ex){
System.out.println(ex);
}
}
}

而且,它关闭了没有问题的端口。





注意:这是一个公开的答案,如果任何人对此有相关经验,请通过编辑答案做出贡献。
我见过人们在互联网上提出这个问题并且一般都遇到与RXTX几乎相同的问题,并且没有为RXTX找到可行的解决方案。



我已经回答了另一个类似于之前问题的人Stackoverflow。



我想分享我所拥有的知识,它可能对那些面临同样问题的人有用。它可以让你的一天减少痛苦。


I communicate with a unit to control a satellite antenna via a serial connection.

Opening a connection with the serial device:

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

portIdentifier = CommPortIdentifier.getPortIdentifier(device);
serialPort = (SerialPort) portIdentifier.open(name, 
serialPort.setSerialPortParams(baudrate, databits, stopbits, parity);
serialPort.setFlowControlMode(flowMode);

bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
outputStream = serialPort.getOutputStream();

If the unit is becomes unavailable, I have to clear the stream before being able to send new commands, once the device is up again. But this clear method on the input/output stream blocks if the device is down. Also closing the streams, or closing the SerialDevice blocks.

Is there a way to close/clear these streams or SerialDevice without blocking?

bufferedRead.close(); // blocks until device is up again
outputStream.close(); // blocks until device is up again
serialPort.close(); // blocks until device is up again

解决方案

I have seen this post for while, and I have had the same issue closing serialport when using gnu.io package, which is also called RXTX.

This is not a final answer, but a suggestion to alternative solution I found.

RXTX has two problems in my opinion if not more:

  1. Depending on your IDE, you need to place for Mac: RXTXcomm.jar and librxtxSerial.jnilib and for PC: RXTXcomm.jar, rxtxSerial.dll on the root of the project in your IDE or Java code, it varies from IDE to IDE. The documentation here does not cover how to do it, and in different IDE like NetBeans, IntelliJ even if I got it to work on both Eclipse and IntelliJ, but not NetBeans yet. It still have other painful issues.
  2. Depending on your OS, even if you get this package up and run, in Windows 8.1 as example, it has problem closing the port. And the only solution is to restart your IDE/console and reconnect. You can get crazy restarting your IDE each time while developing a project.

I have spend a lot of hours finding a solution and there is no solution for closing the port correctly perhaps for Windows 8.1 and later (don't know about other environment closing port problem), as the package is old and the support is limited.

Therefore I suggest going over to a more headache-less package called JSSC.

Here is a simple reading data from serial port using JSSC:

public class Main {

    public static void main(String[] args) {
        SerialPort serialPort = new SerialPort("COM1");
        try {
            serialPort.openPort();//Open serial port
            serialPort.setParams(9600, 8, 1, 0);//Set params.
            byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
            serialPort.closePort();//Close serial port
        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }
    }
}

And ya, it closes the port with out problem.

Note: This is an open answer, if any one have relevant experience regarding this please contribute by editing the answer. I have seen people asking this question around on the internet and having almost same problem with RXTX in general, and have not found a workable solution for RXTX.

I have answered another guy with similar previously question in Stackoverflow.

I wanted to share the knowledge I have, it could be useful for those facing same issue. It could make your day less painful.

这篇关于Java serialport关闭块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆