我在输出下方有以下代码。

我的问题是split函数无法正确/一致地工作。
我想分割每个“ $”。
原因是我想解析GGA和RMC数据。
在花很多时间之前,这是正确的方法吗?
我的步骤:
读取GPS数据
将句子类型(GGA,RMC)存储在仅存储最新数据的变量中
解析这些变量并传递给程序,然后传递给数据库?

[
$ GPGSV 、、、、、、、 ** 43

$ GPRMC,055106.000,A ,, N ,, W,0.00,61.40 ,,,, A * 4D]

[
$ GPVTG 、、、、、、、 T 、、 M 、、、 K,A * 3E

$ GPGGA,055107.000,,N ,, W ,,, M,-33.3,M ,, 0000 * 6T

$ GPGLL ,, N ,, W,055107.000,A,A * 44]

[

$ GPRMC,055107.000,A ,, N ,, W,0.00 ,,,,, A * 4F

$ GPVTG ,,,,,,, T ,, M,0.00,,0.0,,A * 3E]

[

$ GPGGA,055108.000,3,N ,, W,1,09,0.9 ,,, M ,, 0000 * 62]

import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener; import jssc.SerialPortException;
import java.util.Arrays;
//import java.awt.List;
//import java.util.Base64;
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.lang.*;
//import static java.util.Arrays.asList;
//import java.util.List;
//import java.util.stream.Collectors;
//import org.apache.commons.lang3.StringUtils;


public class test {
static List<String> datat = new ArrayList<String>();
static SerialPort serialPort;

public static void main(String[] args) {
    serialPort = new SerialPort("COM1");
    try {
        serialPort.openPort();//Open ports
        serialPort.setParams(4800, 8, 1, 0);//Set params
      int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
       serialPort.setEventsMask(mask);//Set mask
       serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener


    }
    catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

/*
 * In this class must implement the method serialEvent, through it we learn about
 * events that happened to our port. But we will not report on all events but only
 * those that we put in the mask. In this case the arrival of the data and change the
 * status lines CTS and DSR
 */

static class SerialPortReader implements SerialPortEventListener {

    public void serialEvent(SerialPortEvent event) {
    //   if(event.isRXCHAR()){//If data is available
      //     if(event.getEventValue() < 577){//Check bytes count in the input buffer
                //Read data, if 10 bytes available

                try {
                    String getdata =  serialPort.readString(event.getEventValue()+1);
                String[] parts= getdata.split("$");

                if(!datat.isEmpty()){
                    datat.set(datat.size() - 1, datat.get(datat.size() - 1) + parts[0]);
                }
                //data.set(data.size() - 1, data.get(data.size() - 1) + parts[0]);
                for (int i=1; i<parts.length; i++) {
                    datat.add(parts[i]);
               //     System.out.println(Arrays.toString(parts));
                }

               String[] data2 = datat.toArray(new String[0]);

               for(String s : data2)
               {

                   data2 = s.split("$");

                   List<String> data3 = Arrays.asList(data2);
                 //  int testing = data3.size();
                   System.out.println(data3);


               }




                }
                catch (SerialPortException ex) {
             }

       }
}


}

最佳答案

split函数采用正则表达式而不是字符串,您在正则表达式($)中使用特殊字符,然后需要对该字符进行换码

String s= "$........$...$....";
String[] data2= s.split("\\$");

07-24 09:20