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

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

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

    1. inputstream.available() 始终为 0

      inputstream.available() is 0 always(inputstream.available() 始终为 0)

        <tbody id='HTyOL'></tbody>
    2. <tfoot id='HTyOL'></tfoot>

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

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

                本文介绍了inputstream.available() 始终为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我不知道我的代码发生了什么.我没有收到任何错误,也没有回应.我正在将数据写入串行端口并通过激活 port.notifyOnDataAvailable(true); 等待响应,但未触发此事件并且 inputstream.available() 始终返回 0.可能有什么问题?我在 linux 中使用 RXTX.

                I have no idea of what is happening to my code. i am getting no errors and no response as well. I am writing the data to the serialport and waiting for the response by activating port.notifyOnDataAvailable(true); but this event is not triggered and inputstream.available() returns 0 always. What might be wrong ? I am using RXTX in linux.

                编辑

                package testConn;  
                import forms_helper.global_variables;  
                import java.io.BufferedReader; 
                import java.io.IOException;  
                import java.io.InputStream;  
                import java.io.InputStreamReader;  
                import java.io.OutputStream;  
                import java.io.PrintStream;  
                import java.io.UnsupportedEncodingException;  
                import java.util.logging.Level;  
                import java.util.logging.Logger;  
                import javax.comm.*;  
                import java.util.*;  
                /** Check each port to see if it is open. **/   
                public class openPort implements SerialPortEventListener {
                
                    static Enumeration portList;
                    static CommPortIdentifier portId;
                    static String messageString;
                    public static SerialPort serialPort;
                    static OutputStream outputStream;
                    InputStream inputStream;
                    static boolean outputBufferEmptyFlag = false;
                    private BufferedReader is;
                    private PrintStream os;
                
                    public void open() {
                        Enumeration port_list = CommPortIdentifier.getPortIdentifiers();
                
                        while (port_list.hasMoreElements()) {
                            // Get the list of ports
                            CommPortIdentifier port_id = (CommPortIdentifier) port_list.nextElement();
                            if (port_id.getName().equals("/dev/ttyS1")) {
                
                                // Attempt to open it
                                try {
                                    SerialPort port = (SerialPort) port_id.open("PortListOpen", 20000);
                                    System.out.println("Opened successfully:"+port);
                                    try {
                                        int baudRate = 9600; //
                                        port.setSerialPortParams(
                                                baudRate,
                                                SerialPort.DATABITS_7,
                                                SerialPort.STOPBITS_1,
                                                SerialPort.PARITY_EVEN);
                                        port.setDTR(true);
                
                
                                        port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                
                                        System.out.println("properties are set");
                                    } catch (UnsupportedCommOperationException e) {
                                        System.out.println(e);
                                    }
                                    try {
                                        //input = new SerialReader(in);
                                        port.addEventListener(this);
                                        System.out.println("listeners attached" + this);
                                    } catch (TooManyListenersException e) {
                                        System.out.println("too many listeners");
                                    }
                                    port.notifyOnDataAvailable(true);
                
                                    //port.notifyOnOutputEmpty(true);
                                    //sendMessage(port,"@PL");
                                    //port.close ();
                                    try {
                                        is = new BufferedReader(new InputStreamReader(port.getInputStream()));
                                    } catch (IOException e) {
                                        System.err.println("Can't open input stream: write-only");
                                        is = null;
                                    }
                                    try {
                                        os = new PrintStream(port.getOutputStream(), true);
                                    } catch (IOException ex) {
                                        Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                
                                    try {
                                        inputStream = port.getInputStream();
                                        System.out.println("inputstream" + inputStream.available());
                                        outputStream = (OutputStream) port.getOutputStream();
                                        os = new PrintStream(port.getOutputStream(), true, "US-ASCII");
                
                
                                    } catch (IOException e) {
                                        System.out.println(e);
                                    }
                
                                    //set the created variables to global variables
                                    global_variables.port = port;
                                    global_variables.inputStream = inputStream;
                                    System.out.println(inputStream);
                                    System.out.println(outputStream);
                                    global_variables.outputStream = outputStream;
                                    global_variables.os = os;
                                } catch (PortInUseException pe) {
                                    System.out.println("Open failed");
                                    String owner_name = port_id.getCurrentOwner();
                                    if (owner_name == null) {
                                        System.out.println("Port Owned by unidentified app");
                                    } else // The owner name not returned correctly unless it is
                                    // a Java program.
                                    {
                                        System.out.println("  " + owner_name);
                                    }
                                }
                            }
                        }
                    }
                
                    public static void sendMessage(SerialPort port, String msg) {
                        if (port != null) {
                                System.out.println(msg);
                            try {
                                byte[] bytes = msg.getBytes("US-ASCII");
                                try {
                                    global_variables.outputStream.write(bytes);
                                    System.out.println(bytes.length);
                                    global_variables.outputStream.flush();
                                } catch (IOException ex) {
                                    Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            } catch (UnsupportedEncodingException ex) {
                                Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
                            }
                                System.out.println("Opened successfully:"+msg.getBytes());
                                //global_variables.outputStream.write(msg.getBytes());
                                //global_variables.outputStream.flush();
                                //global_variables.os.print(msg);
                                System.out.println(global_variables.outputStream);
                                try {
                                    Thread.sleep(2000);  // Be sure data is xferred before closing
                                    System.out.println("read called");
                                    //SimpleRead read = new SimpleRead();
                                    //int read = global_variables.inputStream.read();
                                    //System.out.println("read call ended"+read);
                                } catch (Exception e) {
                                }
                
                        }
                    }
                
                    public void serialEvent(SerialPortEvent event) {
                        System.out.println(event.getEventType());
                        String line;
                                try {
                                    line = is.readLine();
                                    if (line == null) {
                                        System.out.println("EOF on serial port.");
                                        System.exit(0);
                                    }
                                    os.println(line);
                                } catch (IOException ex) {
                                    System.err.println("IO Error " + ex);
                                }
                        switch (event.getEventType()) {
                            /*
                            case SerialPortEvent.BI:
                
                            case SerialPortEvent.OE:
                
                            case SerialPortEvent.FE:
                
                            case SerialPortEvent.PE:
                
                            case SerialPortEvent.CD:
                
                            case SerialPortEvent.CTS:
                
                            case SerialPortEvent.DSR:
                
                            case SerialPortEvent.RI:
                
                
                            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                            System.out.println("event.getEventType()");
                            break;
                             *
                             */
                
                            case SerialPortEvent.DATA_AVAILABLE:
                                System.out.println("inside event handler data available");
                                byte[] readBuffer = new byte[20];
                
                
                                try {
                                    while (inputStream.available() > 0) {
                                        int numBytes = inputStream.read(readBuffer);
                                    }
                                    System.out.print(new String(readBuffer));
                                    System.exit(1);
                                } catch (IOException e) {
                                    System.out.println(e);
                                }
                
                                break;
                        }
                    }
                } // PortListOpen
                

                我在 main 方法上打开端口,并在应用程序内的按钮单击事件上发送消息.

                I am opening the port on main method and sending the message on a button click event inside the application.

                推荐答案

                .available() 不能用于进程间通信(包括串口),因为它只检查是否有数据在当前进程中可用(在输入缓冲区中).

                .available() can not be used in inter-process communication (serial included), since it only checks if there is data available (in input buffers) in current process.

                在串口通信中,当你发送一个messaga,然后立即调用available(),你会得到0,因为串口还没有回复任何数据.

                In serial communication, when you send a messaga and then immediately call available() you will mostly get 0 as serial port did not yet reply with any data.

                解决方法是在单独的线程中使用阻塞read()(用interrupt()来结束):

                The solution is to use blocking read() in a separate thread (with interrupt() to end it):

                线程中断未结束对输入流的阻塞调用阅读

                这篇关于inputstream.available() 始终为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
              • <tfoot id='GewrQ'></tfoot>
                  <tbody id='GewrQ'></tbody>

                  <bdo id='GewrQ'></bdo><ul id='GewrQ'></ul>

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

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

                        <legend id='GewrQ'><style id='GewrQ'><dir id='GewrQ'><q id='GewrQ'></q></dir></style></legend>