简体   繁体   中英

catching <CR><LF> in a bytes array

I have wrote an event listener that suppose to read any messages of a specific type from a receiver.

here is my event listener:

class SerialPortReader implements SerialPortEventListener {
    public void serialEvent(SerialPortEvent event) {
        if (event.isRXCHAR() && event.getEventValue() > 0) {         
            try {
                byte[] buffer = SP.readBytes(6);
                String type = new String(buffer);
                if (type.equals("$GPGGA")){
                    String line = "";
                    line = line + type;
                    buffer = SP.readBytes(66);
                    String values = new String(buffer);
                    line = line + values;
                    writer.write(line + "\n");
                }
            } 
            catch (SerialPortException ex) {System.out.println(ex);} 
            catch (IOException ex) {System.out.println(ex);}
        }
    }
}

right now after i check that the message is of the correct type i just read 80 bytes off data, which is roughly the size of the message. however, sometimes the message is not full and there for it is shorter then usual. this is the message structure: 在此处输入图片说明

as you can see, the message end s with <CR><FL> . i would like to modify my method so it would read each byte at a time and stop reading once it hits the end of the message. how can i catch the <CR><FL> inside the byte array?

any help would be appreciated. thank you.

It depends on the definition of "messages": are they fixed-length or delimited by a certain sequence? In your current code, you're reading a "header" that is 6 bytes long and a "message" that is 66 bytes long. However, it appears that you actually don't know the length a priori, and instead the message is newline-terminated. A couple of pointers:

  • You're reading bytes from the stream, then turning them into a String by using the String(byte[]) ctor. The documentation states that this uses the default charset for your platform, which may be UTF-8, Latin-1 or whatever regional default. If you are communicating with a device over a serial port, this is probably not what you want since the device is likely to have a single, specific charset for messages (maybe ASCII?). Investigate that point and use either this String ctor or, if you want to be notified when the input contains undecodable garbage, the CharsetDecoder class .
  • If the messages are text-based and newline-delimited, you should definitely use a BufferedReader to read from the stream. Assuming that your SP serial port is an InputStream, you could create a BufferedReader over it, and then call the readLine() method on the reader. The reader will keep requesting bytes from the stream until it sees a newline, then return the whole line to you as a String. Reader objects also encapsulate the Charset I was talking about before.

I have managed to solve my problem. pretty easy solution:

String tmp = SP.readString();
String[] msgs = tmp.split("\r\n");

after reading the string from the serial port, i just split it by the end of line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM