一段时间后接收串行数据时出错

时间:2013-12-17 15:56:16

标签: java multithreading serial-port

我的微控制器以“900 899 | 900 898 | 900 800 900 700 | 900 799 900 756 |”格式连续发送数据。我的Java代码正确地读取数据一段时间,但后来收到类似“900 899900 899”的内容。(注意在我的第一个900之前有一个空格导致错误)导致“java.lang.NumberFormatException:”at我将字符串转换为int的语句..请帮我解决这个bug ... 注意:检查这些读数“900 808 900 899 | 900 806 900 899 | 900 899900 899 |”它应该读取900 804,而不是读取先前收到的值,即900 899

import java.io.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {

static int X[];
static int Y[];
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
byte[] readBuffer = new byte[500];
byte[] noOfBalls = new byte[2];
byte[] temp = new byte[1];
static int arrayEnd = 0;

public SimpleRead(CommPortIdentifier portId) {

    try {
        serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } 
    catch (PortInUseException e) {System.out.println(e);}

    try {
        inputStream = serialPort.getInputStream();
    } 
    catch (IOException e) {System.out.println(e);}

    try {
        serialPort.addEventListener(this);
    }
    catch (TooManyListenersException e) {System.out.println(e);}

    serialPort.notifyOnDataAvailable(true);

    try {
        serialPort.setSerialPortParams(115200,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
    } 
    catch (UnsupportedCommOperationException e) {System.out.println(e);}

    readThread = new Thread(this);
    readThread.start();
}

public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {System.out.println(e);}
}

public void serialEvent(SerialPortEvent event) {
    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:
        arrayEnd = 0;
        try {
            while (inputStream.available() > 1 ) {
          /*    for(int i=0; i< 2; i++){
                    inputStream.read(temp);
                    noOfBalls[i] = temp[0];
                }
                String tmp = new String(noOfBalls);
                int iterations = Integer.parseInt(tmp);
                iterations = iterations * 2;
                for(int j =0; j< iterations; j++){*/
                inputStream.read(temp);
                if (temp[0] == '|') break;
                else
                    readBuffer[arrayEnd++] = temp[0];

                //}
           }
        } catch (IOException e) {System.out.println(e);}
        createXYArray();

       // serialPort.close();/****closing for now, later not needed*/
        break;
    }
}

private void createXYArray() {
    System.out.println(new String(readBuffer));
    X = new int[100];
    Y = new int[100];
    byte[] tmpXY = new byte[3];
    int i = 0;
    while(i < readBuffer.length){
        if(readBuffer[i]==0) break;
        int j=0;
        while(readBuffer[i]!=' ' && j!=3){
            //System.out.println(i);
            tmpXY[j] = readBuffer[i];
            //System.out.println("x "+tmpXY[j]);
            i++; j++;
        }
//  if(j!=3) break;
            createX(tmpXY);
        i++;
        j = 0;
        while(readBuffer[i]!=' ' && j!=3 ){
            tmpXY[j] = readBuffer[i];
        //  System.out.println("y "+tmpXY[j]);
            i++;
            j++;
        }
    /*  if(j==3){
            for (int k = 0; k < 3; k++) {
                three[k] = tmpXY[k];
            }
            createY(three);
        }
        else   */
            createY(tmpXY);
        i++;
    }
}

private static void createY(byte[] tmpY2) {

    String tmp = new String(tmpY2);
    Y[arrayEnd] = Integer.parseInt(tmp);
    //System.out.println(Y[arrayEnd]+" y");
    arrayEnd++;
}

private static void createX(byte[] tmpX2) {

    String tmp = new String(tmpX2);
    X[arrayEnd] = Integer.parseInt(tmp);
    //System.out.println(X[arrayEnd]+" x");

}
}

2 个答案:

答案 0 :(得分:0)

在您要解析的trim个对象上使用String方法。这将删除任何前导或尾随空格。

答案 1 :(得分:0)

好的伙计们,我弄清楚错误是什么。在语句“while(inputStream.available()&gt; 1)”中,我们必须用-1替换1。我真的不知道为什么,但我做了试验和错误,我发现它适用于while(inputStream.available()&gt; -1)。 如果有人知道原因,请在评论中添加..