| | |
| | | } |
| | | } |
| | | |
| | | public static void processIncomingMessage(String message) { |
| | | String[] fields = message.split(","); |
| | | // 检查字段数量是否完整 |
| | | if (fields.length != 21) { |
| | | System.err.println("Invalid message format, expected 21 fields but got " + fields.length); |
| | | return; |
| | | } |
| | | |
| | | // 检查包头是否正确 |
| | | if (!fields[0].equals("$GNGGA")) { |
| | | System.err.println("Invalid message header: " + fields[0]); |
| | | return; |
| | | } |
| | | System.out.println("收到了差分数据:" + message); |
| | | Coordinate.parseGNGGAToCoordinateList(message); |
| | | int count = Coordinate.coordinates.size(); |
| | | System.out.println("savenum:" + count); |
| | | |
| | | Device.updateFromGNGGA(message, fields[15]); |
| | | } |
| | | |
| | | private static class PacketProcessor implements Runnable { |
| | | private final DatagramPacket packet; |
| | |
| | | this.packet = packet; |
| | | } |
| | | |
| | | @Override |
| | | public void run() { |
| | | String receivedData = new String(packet.getData(), 0, packet.getLength()); |
| | | // 处理可能的连包情况 |
| | | String[] messages = receivedData.split("\\$"); |
| | | |
| | | for (String message : messages) { |
| | | if (message.isEmpty()) continue; |
| | | if (message.isEmpty()) { |
| | | continue; |
| | | } |
| | | // 重新添加$符号以便统一处理 |
| | | String fullMessage = "$" + message; |
| | | processMessage(fullMessage); |
| | | processIncomingMessage(fullMessage); |
| | | } |
| | | } |
| | | |
| | | private void processMessage(String message) { |
| | | String[] fields = message.split(","); |
| | | // 检查字段数量是否完整 |
| | | if (fields.length != 21) { |
| | | System.err.println("Invalid message format, expected 21 fields but got " + fields.length); |
| | | return; |
| | | } |
| | | |
| | | // 检查包头是否正确 |
| | | if (!fields[0].equals("$GNGGA")) { |
| | | System.err.println("Invalid message header: " + fields[0]); |
| | | return; |
| | | } |
| | | System.out.println("收到了差分数据:"+message); |
| | | Coordinate.parseGNGGAToCoordinateList(message); |
| | | int count= Coordinate.coordinates.size(); |
| | | System.out.println("savenum:"+count); |
| | | |
| | | Device.updateFromGNGGA(message, fields[15]); |
| | | // 打印解析后的数据 |
| | | //System.out.println("UTC时间: " + fields[1].toUpperCase()); |
| | | //System.out.println("纬度: " + fields[2].toUpperCase()); |
| | | //System.out.println("纬度半球: " + fields[3].toUpperCase()); |
| | | //System.out.println("经度: " + fields[4].toUpperCase()); |
| | | //System.out.println("经度半球: " + fields[5].toUpperCase()); |
| | | //System.out.println("定位质量指示: " + fields[6].toUpperCase()); |
| | | //System.out.println("卫星数量: " + fields[7].toUpperCase()); |
| | | //System.out.println("水平精度因子: " + fields[8].toUpperCase()); |
| | | //System.out.println("海拔高度: " + fields[9].toUpperCase()); |
| | | //System.out.println("海拔高度单位: " + fields[10].toUpperCase()); |
| | | //System.out.println("大地水准面高度: " + fields[11].toUpperCase()); |
| | | //System.out.println("大地水准面高度单位: " + fields[12].toUpperCase()); |
| | | //System.out.println("差分时间: " + fields[13].toUpperCase()); |
| | | //System.out.println("校验和: " + fields[14].toUpperCase()); |
| | | //System.out.println("设备编号: " + fields[15].toUpperCase()); |
| | | //System.out.println("设备电量: " + fields[16].toUpperCase()); |
| | | //System.out.println("卫星信号强度: " + fields[17].toUpperCase()); |
| | | //System.out.println("保留位1: " + fields[18].toUpperCase()); |
| | | //System.out.println("保留位2: " + fields[19].toUpperCase()); |
| | | //System.out.println("保留位3: " + fields[20].toUpperCase()); |
| | | //System.out.println("----------------------------------------"); |
| | | } |
| | | } |
| | | } |