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

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

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

      用Java读取串口

      Reading serial port in Java(用Java读取串口)
      • <legend id='zSfAI'><style id='zSfAI'><dir id='zSfAI'><q id='zSfAI'></q></dir></style></legend>
          <tbody id='zSfAI'></tbody>

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

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

                本文介绍了用Java读取串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Java 初学者.我正在通过串行端口从设备读取数据.我每分钟获取一次数据,但在数据正确出现之后,第一次读数将达到一半.

                I'm beginner in Java. I'm reading data from device through serial port. I'm getting data for every one minute, but first reading is coming half, after that data is coming correctly.

                我得到的输出是:

                6050.003120815340006050.003120815350006050.0

                6050.003120815340006050.003120815350006050.0

                正确的输出应该是这样的:

                Correct output should be like this:

                03120815340006050.003120815350006050.0

                03120815340006050.003120815350006050.0


                我的代码是:

                import java.io.*;
                import java.util.*; //import gnu.io.*;
                import javax.comm.*;
                
                public class SimpleRead implements Runnable, SerialPortEventListener {
                    static CommPortIdentifier portId;
                    static Enumeration portList;
                
                InputStream inputStream;
                SerialPort serialPort;
                Thread readThread;
                byte[] readBuffer;
                
                public static void main(String[] args) {
                    portList = CommPortIdentifier.getPortIdentifiers();
                    System.out.println("portList... " + portList);
                    while (portList.hasMoreElements()) {
                        portId = (CommPortIdentifier) portList.nextElement();
                        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                            System.out.println("port identified is Serial.. "
                                    + portId.getPortType());
                            if (portId.getName().equals("COM2")) {
                                System.out.println("port identified is COM2.. "
                                        + portId.getName());
                                // if (portId.getName().equals("/dev/term/a")) {
                                SimpleRead reader = new SimpleRead();
                            } else {
                                System.out.println("unable to open port");
                            }
                        }
                    }
                }
                
                public SimpleRead() {
                    try {
                        System.out.println("In SimpleRead() contructor");
                        serialPort = (SerialPort) portId.open("SimpleReadApp1111",500);
                        System.out.println(" Serial Port.. " + serialPort);
                    } catch (PortInUseException e) {
                        System.out.println("Port in use Exception");
                    }
                    try {
                        inputStream = serialPort.getInputStream();
                        System.out.println(" Input Stream... " + inputStream);
                    } catch (IOException e) {
                        System.out.println("IO Exception");
                    }
                    try {
                        serialPort.addEventListener(this);
                
                    } catch (TooManyListenersException e) {
                        System.out.println("Tooo many Listener exception");
                    }
                    serialPort.notifyOnDataAvailable(true);
                    try {
                
                        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                
                        // no handshaking or other flow control
                        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                
                        // timer on any read of the serial port
                        serialPort.enableReceiveTimeout(500);
                
                        System.out.println("................");
                
                    } catch (UnsupportedCommOperationException e) {
                        System.out.println("UnSupported comm operation");
                    }
                    readThread = new Thread(this);
                    readThread.start();
                }
                
                public void run() {
                    try {
                        System.out.println("In run() function ");
                        Thread.sleep(500);
                        // System.out.println();
                    } catch (InterruptedException e) {
                        System.out.println("Interrupted Exception in run() method");
                    }
                }
                
                public void serialEvent(SerialPortEvent event) {
                
                    // System.out.println("In Serial Event function().. " + event +
                    // event.getEventType());
                    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: break;
                     */
                    case SerialPortEvent.DATA_AVAILABLE:
                        readBuffer = new byte[8];
                
                        try {
                
                            while (inputStream.available()>0) {
                
                                int numBytes = inputStream.read(readBuffer);
                            //   System.out.println("Number of bytes read " + numBytes);
                            }
                
                            System.out.print(new String(readBuffer));
                
                        } catch (IOException e) {
                            System.out.println("IO Exception in SerialEvent()");
                        }
                        break;
                    }
                    // System.out.println();
                /*  String one = new String(readBuffer);
                    char two = one.charAt(0);
                    System.out.println("Character at three: " + two);*/
                }
                
                }
                

                推荐答案

                使用如下:

                while (inputStream.available()>0) {
                  int numBytes = inputStream.read(readBuffer);
                  System.out.print(new String(readBuffer));
                }
                

                您正在从 while 循环中打印结果.但是循环内的代码可能会运行不止一次,因此会丢失大量数据.

                You are printing the result out of the while loop. However the code inside the loop may run more than once, so chunk of data will be lost.

                这篇关于用Java读取串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 中的默认语言环境设置以使其保持一致?)

                    <tbody id='JoziN'></tbody>

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

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

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