如何从连续输入流中获取命令结果?

时间:2019-06-05 02:16:03

标签: java serial-port inputstream

对不起,我的英语不好。

我正在尝试从连接到串行端口(rs232)的温度模块读取数据。
模块定期发送温度数据。
我可以向模块发送命令,例如当前状态,模块编号等。

我想做的是通过在同一流中发送命令来获取连续的温度数据和模块状态。

我尝试了下面的代码,但它停止并返回JVM错误。

public class SerialReader implements Runnable {
    InputStream in;
    boolean onCommand = false;
    int stByte;
    byte[] command;
    char[] temp = new char[8];
    int commandCnt;
    int temperatureCnt;

    public SerialReader(InputStream in) {
        this.in = in;
    }

    // After Sending command, receive the result
    public String commandResult(int size, int stByte) {
        command = new byte[size];
        this.commandCnt = 0;
        this.stByte = stByte;
        onCommand = true;
        while (onCommand) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//          System.out.println("onCommand: " + onCommand);
        }
        String result = new String(command);
        return result;
    }

    public void run() {
        int n = 0;
        try {
            while (true) {
                n = in.read();
                if (n == -1) {
                    continue;
                }
                // When command has sent, change the state
                if (onCommand) {
                    if (stByte != 0) {
                        if (stByte == n) {
                            stByte = 0;
                        } else {
                            continue;
                        }
                    }
                    command[commandCnt++] = (byte) n;
                    if (commandCnt == command.length) {
                        onCommand = false;
                    }
                    continue;
                }
//              System.out.print((char) n);

                // end of temperature data per 1 seconds
                if (n == '(') {
                    System.out.println("1CH: " + (temp[0] == '0' ? "+" : "-") + temp[1] + temp[2] + "." + temp[3] + "℃, 2CH: " + (temp[0] == '0' ? "+" : "-") + temp[5] + temp[6] + "." + temp[7] + "℃");
//                  System.out.println(new String(temperatureBytes));
                } else if (n == ')') {
                    temperatureCnt = 0;
                } else {
                    if (temperatureCnt < temp.length) {
                        temp[temperatureCnt++] = (char) n;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

下面的错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180005b6b, pid=14892, tid=0x0000000000002e6c
#
# JRE version: Java(TM) SE Runtime Environment (8.0_201-b09) (build 1.8.0_201-b09)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [rxtxSerial.dll+0x5b6b]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\workspace\sensor\hs_err_pid14892.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

任何想法都会受到赞赏。

0 个答案:

没有答案