• <bdo id='XHEDp'></bdo><ul id='XHEDp'></ul>
        <i id='XHEDp'><tr id='XHEDp'><dt id='XHEDp'><q id='XHEDp'><span id='XHEDp'><b id='XHEDp'><form id='XHEDp'><ins id='XHEDp'></ins><ul id='XHEDp'></ul><sub id='XHEDp'></sub></form><legend id='XHEDp'></legend><bdo id='XHEDp'><pre id='XHEDp'><center id='XHEDp'></center></pre></bdo></b><th id='XHEDp'></th></span></q></dt></tr></i><div id='XHEDp'><tfoot id='XHEDp'></tfoot><dl id='XHEDp'><fieldset id='XHEDp'></fieldset></dl></div>
      1. <tfoot id='XHEDp'></tfoot><legend id='XHEDp'><style id='XHEDp'><dir id='XHEDp'><q id='XHEDp'></q></dir></style></legend>
      2. <small id='XHEDp'></small><noframes id='XHEDp'>

      3. Java 串口关闭块

        时间:2024-08-24

              <small id='ffF0G'></small><noframes id='ffF0G'>

                <bdo id='ffF0G'></bdo><ul id='ffF0G'></ul>
                <legend id='ffF0G'><style id='ffF0G'><dir id='ffF0G'><q id='ffF0G'></q></dir></style></legend>

                • <i id='ffF0G'><tr id='ffF0G'><dt id='ffF0G'><q id='ffF0G'><span id='ffF0G'><b id='ffF0G'><form id='ffF0G'><ins id='ffF0G'></ins><ul id='ffF0G'></ul><sub id='ffF0G'></sub></form><legend id='ffF0G'></legend><bdo id='ffF0G'><pre id='ffF0G'><center id='ffF0G'></center></pre></bdo></b><th id='ffF0G'></th></span></q></dt></tr></i><div id='ffF0G'><tfoot id='ffF0G'></tfoot><dl id='ffF0G'><fieldset id='ffF0G'></fieldset></dl></div>
                    <tbody id='ffF0G'></tbody>
                  <tfoot id='ffF0G'></tfoot>
                  本文介绍了Java 串口关闭块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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();
                  

                  如果设备不可用,我必须先清除流,然后才能发送新命令,一旦设备再次启动.但是,如果设备关闭,输入/输出流上的这种清除方法会阻塞.同时关闭流,或关闭 SerialDevice 块.

                  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.

                  有没有办法在不阻塞的情况下关闭/清除这些流或 SerialDevice?

                  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
                  

                  推荐答案

                  我看到这个帖子有一段时间了,我在使用 gnu.io 包(也称为 RXTX)时关闭串行端口时遇到了同样的问题.

                  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 有两个问题:

                  RXTX has two problems in my opinion if not more:

                  1. 根据您的 IDE,您需要为 Mac 放置:RXTXcomm.jarlibrxtxSerial.jnilib,对于 PC:RXTXcomm.jar, rxtxSerial.dll 在您的 IDE 或 Java 代码中项目的根目录,它因 IDE 而异.此处的文档不包括如何做到这一点,并且在 NetBeans、IntelliJ 等不同的 IDE 中,即使我让它在 Eclipse 和 IntelliJ 上都可以工作,但还不能在 NetBeans 上工作.它还有其他痛苦的问题.
                  2. 根据您的操作系统,即使您启动并运行此软件包,例如在 Windows 8.1 中,关闭端口也会出现问题.唯一的解决方案是重新启动您的 IDE/控制台并重新连接.每次开发项目时,您都可能会疯狂地重新启动 IDE.
                  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.

                  我花了很多时间寻找解决方案,但对于 Windows 8.1 及更高版本可能没有正确关闭端口的解决方案(不知道其他环境关闭端口问题),因为包是旧的并且支持是有限的.

                  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.

                  因此,我建议使用更轻松的包 JSSC.

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

                  这是一个简单的使用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.

                  • 查看所有 JSSC 代码示例
                  • 可以在这里下载包.
                  • JSSC 主页
                  • See all examples of JSSC codes
                  • The package can be downloaded here.
                  • JSSC home page

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

                  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.

                  我之前回答过另一个人类似问题 在 Stackoverflow 中.

                  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 串口关闭块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于串行通信的带有 rxtx 组件的 Java 小程序 下一篇:如何将字符串转换为十六进制和十六进制转换为字符串?

                  相关文章

                  <small id='h7DsG'></small><noframes id='h7DsG'>

                    <i id='h7DsG'><tr id='h7DsG'><dt id='h7DsG'><q id='h7DsG'><span id='h7DsG'><b id='h7DsG'><form id='h7DsG'><ins id='h7DsG'></ins><ul id='h7DsG'></ul><sub id='h7DsG'></sub></form><legend id='h7DsG'></legend><bdo id='h7DsG'><pre id='h7DsG'><center id='h7DsG'></center></pre></bdo></b><th id='h7DsG'></th></span></q></dt></tr></i><div id='h7DsG'><tfoot id='h7DsG'></tfoot><dl id='h7DsG'><fieldset id='h7DsG'></fieldset></dl></div>
                      <bdo id='h7DsG'></bdo><ul id='h7DsG'></ul>

                    1. <tfoot id='h7DsG'></tfoot>
                    2. <legend id='h7DsG'><style id='h7DsG'><dir id='h7DsG'><q id='h7DsG'></q></dir></style></legend>