我正在与三个班级合作。 第一类打开GPS并读取数据。 第二个解析数据。 第三个是GUI中的解析数据。
我的问题是如何将全局变量gpsData从NMEA传递给classC?
我正在使用我在网上找到的gps数据代码。
看起来NMEA类依赖于serialGps,而serialGps依赖于NMEA。
我需要传递inputLine,它将NMEA类中包含的全局变量“gpsData”设置为第三个类,它将是我的GUI。
这是serialGps类
package gpsData;
import com.fazecast.jSerialComm.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings({"unused", "WeakerAccess"})
public class SerialGps {
public interface StateListener {
void onGpsStateUpdated(NMEA.GpsState state);
}
private String portName;
private int baudRate;
private boolean isRunning = false;
private List<StateListener> stateListeners = new ArrayList<>();
public SerialGps(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
}
public SerialGps(String portName) {
this(portName, 4800);
}
public void addStateListener(StateListener stateListener) {
stateListeners.add(stateListener);
}
public void start() {
NMEA nmea = new NMEA();
SerialPort[] serialPorts = SerialPort.getCommPorts();
SerialPort gpsPort = null;
for (SerialPort serialPort : serialPorts) {
if (serialPort.getDescriptivePortName().toLowerCase().contains("serial")) {
gpsPort = serialPort;
}
}
if (gpsPort == null) {
System.out.println("failed to find gps serial port");
return;
}
System.out.println("using serial port: " + gpsPort.getDescriptivePortName());
gpsPort.setBaudRate(4800);
gpsPort.openPort();
InputStream inStream = gpsPort.getInputStream();
if (inStream == null) {
System.out.println("opening port " + gpsPort.getDescriptivePortName() + " failed");
return;
}
Thread thread = new Thread(() -> {
String inputLine = "";
isRunning = true;
while (isRunning) {
try {
if (inStream.available() > 0) {
char b = (char) inStream.read();
if (b == '\n') {
NMEA.GpsState gpsState = nmea.getUpdatedStatus(inputLine);
updateState(gpsState);
inputLine = "";
} else {
inputLine += b;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
public void stop() throws InterruptedException {
isRunning = false;
}
private void updateState(NMEA.GpsState gpsState) {
stateListeners.forEach(stateListener -> stateListener.onGpsStateUpdated(gpsState));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SerialGps app = new SerialGps("/dev/cu.usbserial", 4800);
app.start();
}
}
NMEA课程:
package gpsData;
import java.io.IOException;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class NMEA {
public static String gpsData = null;
interface SentenceParser {
boolean parse(String[] tokens, GpsState position);
}
private class GPGGA implements SentenceParser {
public boolean parse(String[] tokens, GpsState position) {
position.time = Float.parseFloat(tokens[1]);
position.lat = Latitude2Decimal(tokens[2], tokens[3]);
position.lon = Longitude2Decimal(tokens[4], tokens[5]);
position.quality = Integer.parseInt(tokens[6]);
position.altitude = Float.parseFloat(tokens[9]);
return true;
}
}
private class GPGGL implements SentenceParser {
public boolean parse(String[] tokens, GpsState position) {
position.lat = Latitude2Decimal(tokens[1], tokens[2]);
position.lon = Longitude2Decimal(tokens[3], tokens[4]);
position.time = Float.parseFloat(tokens[5]);
return true;
}
}
private class GPRMC implements SentenceParser {
public boolean parse(String[] tokens, GpsState position) {
position.time = Float.parseFloat(tokens[1]);
position.lat = Latitude2Decimal(tokens[3], tokens[4]);
position.lon = Longitude2Decimal(tokens[5], tokens[6]);
position.velocity = Float.parseFloat(tokens[7]);
position.dir = Float.parseFloat(tokens[8]);
return true;
}
}
private class GPVTG implements SentenceParser {
public boolean parse(String[] tokens, GpsState position) {
position.dir = Float.parseFloat(tokens[3]);
return true;
}
}
private class GPRMZ implements SentenceParser {
public boolean parse(String[] tokens, GpsState position) {
position.altitude = Float.parseFloat(tokens[1]);
return true;
}
}
private static float Latitude2Decimal(String lat, String NS) {
float result = (Float.parseFloat(lat.substring(2)) / 60.0f) + Float.parseFloat(lat.substring(0, 2));
if (NS.startsWith("S")) {
result *= -1.0f;
}
return result;
}
private static float Longitude2Decimal(String lon, String WE) {
float med = (Float.parseFloat(lon.substring(3)) / 60.0f) + Float.parseFloat(lon.substring(0, 3));
if (WE.startsWith("W")) {
med *= -1.0f;
}
return med;
}
@SuppressWarnings("WeakerAccess")
public class GpsState {
public float time = 0.0f;
public float lat = 0.0f;
public float lon = 0.0f;
public boolean hasFix = false;
public int quality = 0;
public float dir = 0.0f;
public float altitude = 0.0f;
public float velocity = 0.0f;
void updatefix() {
hasFix = quality > 0;
}
public String toString() {
return String.format("POSITION: lat: %f, lon: %f, time: %f, Q: %d, dir: %f, alt: %f, vel: %f", lat, lon, time, quality, dir, altitude, velocity);
}
}
private GpsState position = new GpsState();
private static final Map<String, SentenceParser> sentenceParsers = new HashMap<>();
NMEA() {
sentenceParsers.put("GPGGA", new GPGGA());
sentenceParsers.put("GPGGL", new GPGGL());
sentenceParsers.put("GPRMC", new GPRMC());
sentenceParsers.put("GPRMZ", new GPRMZ());
sentenceParsers.put("GPVTG", new GPVTG());
}
GpsState getUpdatedStatus(String inputLine) {
if (inputLine.startsWith("$")) {
//System.out.println(inputLine);
String nmea = inputLine.substring(1);
String[] tokens = nmea.split(",");
String type = tokens[0];
this.gpsData = inputLine;
//System.out.println(gpsData);
if (sentenceParsers.containsKey(type)) {
try {
sentenceParsers.get(type).parse(tokens, position);
} catch (Exception e) {}
}
position.updatefix();
//System.out.println(gpsData);
}
return position;
}
public static String passer() {
String gpsData2 = gpsData;
return gpsData2;
}
}
classC
package gpsData;
public class classC {
static String inputLinet = null;
public void print() {
inputLinet = NMEA.gpsData;
if (inputLinet.startsWith("$")) {
System.out.print(inputLinet);
}
}
}