| | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | System.err.println("Invalid message header: " + fields[0]); |
| | | return; |
| | | } |
| | | System.out.println("收到了差分数据:" + message); |
| | | 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; |