| | |
| | | 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监听端口 |
| | |
| | | ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE); |
| | | |
| | | try (DatagramSocket socket = new DatagramSocket(PORT)) { |
| | | System.out.println("UDPServer started on port " + PORT); |
| | | |
| | | while (!Thread.currentThread().isInterrupted()) { |
| | | byte[] buffer = new byte[BUFFER_SIZE]; |
| | | DatagramPacket packet = new DatagramPacket(buffer, buffer.length); |
| | |
| | | String[] fields = message.split(","); |
| | | // 检查字段数量是否完整 |
| | | if (fields.length != 21) { |
| | | System.err.println("Invalid message format, expected 21 fields but got " + fields.length); |
| | | System.err.println("Invalid message format, expected 21 fields but got " + fields.length + ". Message content: [" + message + "]"); |
| | | return; |
| | | } |
| | | |
| | | // 检查包头是否正确 |
| | | if (!fields[0].equals("$GNGGA")) { |
| | | if (!fields[0].equals("$GNGGA") && !fields[0].equals("$GPGGA") && !fields[0].equals("$GBGGA")) { |
| | | 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) { |
| | | message=message.replace("GPGGA", "GNGGA"); |
| | | String[] fields = message.split(","); |
| | | // 检查字段数量是否完整 |
| | | if (fields.length < 15) { |
| | |
| | | } |
| | | |
| | | // 检查包头是否正确 |
| | | if (!fields[0].equals("$GNGGA")) { |
| | | if (!fields[0].equals("$GNGGA")&&!fields[0].equals("$GPGGA")&&!fields[0].equals("$GBGGA")) { |
| | | 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); |
| | | } |