【问题标题】:RXTX how to reading from com portRXTX 如何从 com 端口读取数据
【发布时间】:2011-07-20 12:02:46
【问题描述】:

你好,我有这样的事情

package compot;

import java.util.Enumeration;
import gnu.io.*;


public class core {

    private static SerialPort p;

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Enumeration ports = CommPortIdentifier.getPortIdentifiers();
        System.out.println("start");
        while(ports.hasMoreElements())
        {
            CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
            System.out.print(port.getName() + " -> " + port.getCurrentOwner() + " -> ");
            switch(port.getPortType())
            {
                case CommPortIdentifier.PORT_PARALLEL:
                    System.out.println("parell");
                break;
                case CommPortIdentifier.PORT_SERIAL:
                    //System.out.println("serial");
                try {
                    p = (SerialPort) port.open("core", 1000);
                    int baudRate = 57600; // 57600bps
                    p.setSerialPortParams(
                            baudRate,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                } catch (PortInUseException e) {
                    System.out.println(e.getMessage());
                } catch (UnsupportedCommOperationException e) {
                    System.out.println(e.getMessage());
                }
                break;
            }
        }
        System.out.println("stop");
    }
}

但我不知道如何从端口读取??我已经阅读了this tutorial,但我不知道他们的“演示应用程序”是什么意思??

编辑

OutputStream outStream = p.getOutputStream();
                    InputStream inStream = p.getInputStream();

                    BufferedReader in = new BufferedReader( new InputStreamReader(inStream));
                    String inputLine;

                    while ((inputLine = in.readLine()) != null) 
                        System.out.println(inputLine);
                    in.close();

我已添加此代码,但我收到了

稳定库 ========================================== 原生库版本= RXTX-2.1-7 Java lib 版本 = RXTX-2.1-7 启动 /dev/ttyUSB3 -> null -> 底层输入流返回零字节停止

【问题讨论】:

  • 有后续吗?一点反应都没有? :p
  • 这可能是流控制的问题。您可以使用 port.setFlowControl() 方法在端口上设置它。但是,我无法告诉您应该使用哪种流量控制。也许开始阅读有关串行端口的一般内容:tldp.org/HOWTO/Serial-HOWTO-4.html 我建议按字节而不是按行阅读输入流。但这取决于您实际尝试与之通信的设备。
  • 我正在使用 TTL 到 RS232 转换器,但是我得到的是 ttyUSB0 而不是 ttyS0 ?但是当我插入pendrive时它看不到它

标签: java rxtx


【解决方案1】:

这是你的代码吗?你到底想在那里做什么? :p

为了从 SerialPort 读取,您需要声明此端口:

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system

然后在这个端口上打开一个连接:

SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);

下一步是设置此端口的参数(如果需要):

serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

这是我的配置 - 你的可能会有所不同:)

现在您可以在此端口上读取一些数据了! 要获取数据,您需要获取 serialPorts 输入流并从中读取:

InputStream inputStream = serialPort.getInputStream();
while (active) {
        try {
            byte[] buffer = new byte[22];
            while ((buffer[0] = (byte) inputStream.read()) != 'R') {
            }
            int i = 1;
            while (i < 22) {
                if (!active) {
                    break;
                }
                buffer[i++] = (byte) inputStream.read();
            }
            //do with the buffer whatever you want!
        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        }
}

我在这里实际做的是使用read() 方法从输入流中读取数据。这将阻塞直到数据可用,如果到达流的末尾,则返回 -1。在这个例子中,我等到我得到一个“R”字符,然后将接下来的 22 个字节读入缓冲区。这就是您读取数据的方式。

  1. 获取 serialPorts 输入流
  2. 使用 .read() 方法
  3. 所有内容都在一个循环中并在取消时退出循环(在我的情况下,active 可以通过另一种方法设置为 false,从而结束读取过程。

希望对你有帮助

【讨论】:

    【解决方案2】:

    尝试使用

    if (socketReader.ready()) {
    }
    

    以便套接字仅在缓冲流中有要读取的内容时响应 所以异常永远不会发生。

    【讨论】:

      【解决方案3】:

      在你的 try 块中有这样的东西:

      OutputStream outStream = p.getOutputStream();
      InputStream inStream = p.getInputStream();
      

      【讨论】:

      • 那么p = (SerialPort) port.open("core", 1000); 这是为了什么,第一部分“核心”需要什么??
      • 串行端口连接被命名为“核心”,当当前所有者没有在这段时间内释放连接时,1000 毫秒后超时.. 无论如何,您可能应该没有使用此端口的进程
      • 此名称将标识端口的所有者。只需使用您的应用程序名称。更多信息:link