Javafx 如果有数据,流如何返回零字节?

Javafx 如果有数据,流如何返回零字节?,javafx,netbeans,fxml,rxtx,Javafx,Netbeans,Fxml,Rxtx,我正在做一个RXTX项目。我的it设置如下: public void doConnect(ActionEvent event) { String selectedPort = (String)connectTabController.portList.getValue(); System.out.println("Connecting to: " + selectedPort); selectedPortIdentifier = (Comm

我正在做一个RXTX项目。我的it设置如下:

public void doConnect(ActionEvent event)
    {
        String selectedPort = (String)connectTabController.portList.getValue();
        System.out.println("Connecting to: " + selectedPort);
        selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);
        CommPort commPort = null;
        try
        {
            commPort = selectedPortIdentifier.open("AT QC ReponseTime", TIMEOUT);
            serialPort = (SerialPort)commPort;
            setConnected(true);
            if (isConnected)
                    {
                        if (initIOStream() == true)
                                {
                                    initListener();
                                    System.out.println("Initializing listener");
                                    connectTabController.gui_changeStatusLabel("Device Connected!");
                                }
                    }
        }
        catch (PortInUseException e)
        {
            System.out.println("Port In use! " + e.toString());
        }

        catch (Exception e)
        {
                    System.out.println("Failed to open! " + e.toString());
        }
    }
    public boolean initIOStream()
    {
        //return value for whether opening the streams is successful or not
        boolean successful = false;

        try {
            //
            input = serialPort.getInputStream();
            output = serialPort.getOutputStream();
            writeData(RESETTPOD);
            System.out.println("Writing Reset command");

            successful = true;
            System.out.println("IO Stream opened successfully!");
            return successful;

        }
        catch (IOException e) {
            System.out.println("I/O Streams failed to open. (" + e.toString() + ")");
            return successful;
        }
    }


    public void initListener()
    {
        try
        {
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        }
        catch (TooManyListenersException e)
        {
            System.out.println("Too many listeners. (" + e.toString() + ")");

        }
    }
这就是建立连接的方式,它有一个侦听器,应该在数据可用时发出通知,这会触发以下事件:

@Override
    public void serialEvent(SerialPortEvent evt) {

        BufferedReader reader = null;

        if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE)
        {
            try
            {
                reader = new BufferedReader(new InputStreamReader(input));

                if (reader.ready())
                {
                fullLine = reader.readLine();
                System.out.println(fullLine + "\n");
                }

            }
            catch (Exception e)
            {
                System.out.println("@SerialEvent Failed to read data. (" + e.toString() + ")");
            }
        }
}
然而,我一直得到“底层输入流返回零字节”


这是没有意义的,因为如果没有什么可读的,那么听众就不应该首先被触发。我试着运行这个应用程序,每1/3秒就会收到一条错误消息,这与发送数据的设备的突发输出相对应。(这在PuttY等程序中运行良好)

如果要使用BufferedReader,请查看javax.comm、CommPort和getInputStream的参考API文档。然后尝试对阈值和接收超时使用不同的设置

例如)。 serialPort.enableReceiveThreshold(3); serialPort.enableReceiveTimeout(1)


您不是在重复使用同一输入流创建新的缓冲读卡器吗?嗯,是的,我想现在我想它确实会覆盖并在每次出现通知时创建新的缓冲读卡器。但是,如果我在initIOStrema方法中创建缓冲读取器(就在我将值分配给输入/输出之后),我仍然会得到相同的问题。如果将底层流的引用包装到另一个流/读取器中,则基本上总是错误的。每个缓冲读取器将尝试从流中填充其缓冲区。所以在某一点上,某个读者将无法读取数据。。。你为什么还要监听“可用数据”?为什么不让读者阅读;它会按需要堵住的哦!我不知道,我有一个输出,每次设备发送新数据时我都需要更新,所以从我在示例和其他项目中看到的内容来看,他们通常会添加一个带有可用数据的侦听器。还有什么其他方法可以在收到数据后立即输出数据?