-1

Example data from serial port using System.out.print()

$GPGGA,134658.00,5106.9792,N,11402.3003,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60

This message ends with a carriage return followed by a newline. When I use the code below, I get this output.

$
G
P
G
G
A
,
.
.
.
   (Carriage return)
   (New Line)
$
G
P
G
S
A

My goal is to get a string that I can then split by ",". I want to reset/refresh that string on a carriage return/new line sequence.

    static class SerialPortReader implements SerialPortEventListener {

        public void serialEvent(SerialPortEvent event) {

            try {
                byte[] tester = serialPort.readBytes(1);

                //System.out.print(getdata);
                InputStream gpsStream = new ByteArrayInputStream(tester);

                BufferedReader in = new BufferedReader(new InputStreamReader(gpsStream, StandardCharsets.UTF_8));
                String output = in.readLine();
System.out.println(in);
            } catch (SerialPortException | IOException ex) {                  
            }    
        }
    }

Lets Assume this is the console output. I want to parse the most recent line. In this case the $GPGSA string.

$GPGGA,134658.00,5106.9792,N,11402.3003,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60
$GPGSA,134658.00,W,2,09,1.0,1048.47,M,-16.27,M,08,AAAA*60

I guess my question is how do I get the input stream into a format that I can parse?

New edit: When I use this code, it prints exactly how I need it.

How can I replicate the System.out.print(); function?

String wtf = serialPort.readString(event.getEventValue());
                System.out.print(wtf);

Console output from above string:

 $GPGSA,M,3,30,11,28,07,01,17,08,13,,,,,2.3,1.2,2.0*3D
$GPGSV,3,1,12,30,74,226,33,11,64,090,31,28,55,317,32,07,55,169,27*75
$GPGSV,3,2,12,01,51,118,22,17,31,239,25,08,28,050,34,13,18,300,22*75
$GPGSV,3,3,12,48,22,240,,19,08,233,,15,01,324,,22,00,118,*7A
4

1 回答 1

0

对于每个 SerialPortEvent,您似乎只读取一个字节(=一个字母)

serialPort.readBytes(1);

然后你将这一个字母存储在 ByteArrayInputStream

要读取 ByteArrayInputStream,您使用的是 BufferedReader(当没有任何东西可以缓冲时,因为一切都已经在您的 ByteArrayInputStream 中)来读取唯一可用的字母

现在我唯一需要知道的才能进一步回答是:什么是类

串行端口

?

编辑:

经过一番谷歌搜索,假设您正在使用 jssc

如果你换行

serialPort.readBytes(1);

serialPort.readBytes(serialPort.getInputBufferBytesCount());

这个会比较好吗 ?

如果是,那么您将需要一个 while 循环来围绕您的 readline 函数:

String output = null;
while((output=in.readLine()) != null){
    System.out.println(output);
}
于 2018-01-12T23:59:38.383 回答