| | |
| | | import java.net.SocketException; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import gecaoji.Device; |
| | | import zhuye.Coordinate; |
| | | |
| | | import publicway.Gpstoxuzuobiao; |
| | | |
| | | public class UDPServer { |
| | | private static final int PORT = 7000; // 默认UDP监听端口 |
| | | private static final int BUFFER_SIZE = 65507; // UDP最大包大小 |
| | | private static final int THREAD_POOL_SIZE = 100; // 线程池大小 |
| | | |
| | | private static final AtomicInteger RECEIVED_PACKET_COUNTER = new AtomicInteger(0); |
| | | |
| | | private static volatile Thread serverThread; |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | int sequence = incrementReceivedPacketCounter(); |
| | | System.out.println("收到了差分数据(" + sequence + "):" + message); |
| | | |
| | | // 使用Gpstoxuzuobiao处理并获取XY坐标 |
| | | double[] xy = Gpstoxuzuobiao.processGNGGAToXY(message); |
| | | if (xy != null) { |
| | | // 这里可以将XY坐标传递给其他方法使用 |
| | | // System.out.println("UDP GNGGA -> XY: " + xy[0] + ", " + xy[1]); |
| | | } |
| | | |
| | | Coordinate.parseGNGGAToCoordinateList(message); |
| | | int count = Coordinate.coordinates.size(); |
| | | System.out.println("savenum:" + count); |
| | | |
| | | Device.updateFromGNGGA(message, fields[15]); |
| | | } |
| | | |
| | | /**处理串口接收到的数据*/ |
| | | public static void processSerialData(String message) { |
| | | String[] fields = message.split(","); |
| | | // 检查字段数量是否完整 |
| | | if (fields.length < 15) { |
| | | System.err.println("Invalid serial GNGGA format, expected at least 15 fields but got " + fields.length); |
| | | return; |
| | | } |
| | | |
| | | // 检查包头是否正确 |
| | | if (!fields[0].equals("$GNGGA")) { |
| | | System.err.println("Invalid message header: " + fields[0]); |
| | | return; |
| | | } |
| | | int sequence = incrementReceivedPacketCounter(); |
| | | System.out.println("收到了串口数据(" + sequence + "):" + message); |
| | | |
| | | // 使用Gpstoxuzuobiao处理并获取XY坐标 |
| | | double[] xy = Gpstoxuzuobiao.processGNGGAToXY(message); |
| | | if (xy != null) { |
| | | // 这里可以将XY坐标传递给其他方法使用 |
| | | // System.out.println("Serial GNGGA -> XY: " + xy[0] + ", " + xy[1]); |
| | | } |
| | | |
| | | Coordinate.dellchuankougngga(message); |
| | | int count = Coordinate.coordinates.size(); |
| | | System.out.println("savenum:" + count); |
| | | |
| | | Device.updateFromSerialGNGGA(message); |
| | | } |
| | | |
| | | private static int incrementReceivedPacketCounter() { |
| | | return RECEIVED_PACKET_COUNTER.updateAndGet(current -> { |
| | | int next = current + 1; |
| | | if (next > 10000 || next <= 0) { |
| | | next = 1; |
| | | } |
| | | return next; |
| | | }); |
| | | } |
| | | |
| | | public static int getReceivedPacketCount() { |
| | | return RECEIVED_PACKET_COUNTER.get(); |
| | | } |
| | | |
| | | 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("----------------------------------------"); |
| | | } |
| | | } |
| | | } |