[英]Java split not working consistently
I have the following code below the output. 我在输出下方有以下代码。
My issue is that the split function is not working correctly/consistently. 我的问题是split函数无法正确/一致地工作。 I want to split on each "$".
我想分割每个“ $”。 Reason being is that I want to parse the GGA and RMC data.
原因是我想解析GGA和RMC数据。 Before anyone spends to much time, is this the right way to do this?
在花很多时间之前,这是正确的方法吗? My steps: Read GPS data Store sentence type (GGA, RMC) in variables that only store the most recent data Parse those variables and pass to program and then database?
我的步骤:读取GPS数据在仅存储最新数据的变量中存储句子类型(GGA,RMC),解析这些变量并传递给程序,然后传递给数据库?
[ $GPGSV,,,,,,,,,*43 [$ GPGSV 、、、、、、、 ** 43
$GPRMC,055106.000,A,,N,,W,0.00,61.40,,,,A*4D] $ GPRMC,055106.000,A 11 N ,, W,0.00,61.40 ,,,, A * 4D]
[ $GPVTG,,,,,,T,,M,,,,K,A*3E [$ GPVTG ,,,,,,,,,,,,,, K,A * 3E
$GPGGA,055107.000,,N,,W,,,,M,-33.3,M,,0000*6T $ GPGGA,055107.000,...,N ,, w ^ ,,,,男,-33.3,男,, 0000 * 6T
$GPGLL,,N,,W,055107.000,A,A*44] $ GPGLL 11 N ,, W,055107.000,A,A * 44]
[ [
$GPRMC,055107.000,A,,N,,W,0.00,,,,,A*4F $ GPRMC,055107.000,A 11 N ,, W,0.00 ,,,,, A * 4F
$GPVTG,,,,,,T,,M,0.00,,0.0,,A*3E] $ GPVTG ,,,,,,Ť...,M,0.00,0.0,,A * 3E]
[ [
$GPGGA,055108.000,3,N,,W,1,09,0.9,,,M,,0000*62] $ 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) {
}
}
}
}
The split function takes a regular expression, not a string, You are using a special character in a regular expresion ($) then you need to scape that character split函数采用正则表达式,而不是字符串,您在正则表达式($)中使用特殊字符,然后需要对该字符进行换码
String s= "$........$...$....";
String[] data2= s.split("\\$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.