I have the following code below the output.
My issue is that the split function is not working correctly/consistently. I want to split on each "$". Reason being is that I want to parse the GGA and RMC data. 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?
[ $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) {
}
}
}
}
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
String s= "$........$...$....";
String[] data2= s.split("\\$");
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.