简体   繁体   English

Java 使用 ByteBuffer 从 GPS 设备解码字节

[英]Java decoding bytes from a GPS device with ByteBuffer

I'm facing issues decoding bytes send from a GPS device to my java UDP listener program.我在解码从 GPS 设备发送到我的 java UDP 侦听程序的字节时遇到了问题。 I can extract some of the information but for other info I cannot understand how to do this.我可以提取一些信息,但对于其他信息,我无法理解如何执行此操作。 The format of the data from the device manual is as follows:设备手册中的数据格式如下:

Name       | Type          | Length
length:    | short         | 2
cmd:       | short         | 2
enabled:   | unsigned char | 1
alarm:     | unsigned char | 1
speed:     | char          | 1
direction: | short         | 2
longitude: | double        | 4
latitude:  | double        | 4
datetime:  | long          | 4
userid:    | char          | 11
iostate:   | char          | 1
oilstate:  | char          | 1

In my program I can decode length, command, alarm, speed, direction and device.在我的程序中,我可以解码长度、命令、警报、速度、方向和设备。 I am stuck on latitude, longitude and datetime and would require some assistance.我被困在纬度、经度和日期时间上,需要一些帮助。 The values that I get are completely far from what the proper ones should be.我得到的值与正确的值相去甚远。 This was working without any issues on python with the struct.unpack function.这在使用struct.unpack函数的 python 上没有任何问题。

The code in the function is as follows:函数中的代码如下:

private void SetupUDP() throws IOException {
        String message;
        // Setup UDP Stuff and listen on all interfaces
        DatagramSocket serverSocket = new DatagramSocket(9876, InetAddress.getByName("0.0.0.0"));
        byte[] receiveData = new byte[1024];
        while(true) {
            // Start receiving UDP packets from devices
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);

            ByteBuffer data = ByteBuffer.wrap(receivePacket.getData());
            data.order(ByteOrder.LITTLE_ENDIAN);
            if (receivePacket.getLength() == 15) {
                short len = data.getShort(0);
                short cmd = data.getShort(2);
                String device = "";
                for (int x=4; x < 14; x++) {
                    byte b = data.get(x);
                    device += (char) b;
                }
                System.out.println(len + " " + cmd + " -- " + device);
            }
            if (receivePacket.getLength() == 34) {
                short len = data.getShort(0);
                short cmd = data.getShort(2);
                byte benabled = data.get(4);
                int enabled = (int) benabled;
                byte balarm = data.get(5);
                // Alarm (OK)
                int alarm =  Math.abs((int) balarm);
                // Speed (OK)
                byte bspeed = data.get(6);
                int speed = (int) bspeed;
                // Direction (OK)
                short direction = data.getShort(7);
                // Coordinates
                double longitude = data.getDouble(9);
                double latitude = data.getDouble(13);
                // Device ID (OK)
                String device = "";
                for (int x=21; x < 31; x++) {
                    byte bdevice = data.get(x);
                    device += (char) bdevice;
                }

                // Datetime (testing)
                int ddatetime = data.get(17);
                Date datetime = new Date(ddatetime * 1000);
                //System.out.println("-->" + datetime + ", " + longitude);
                System.out.println(len + " " + cmd + " " + enabled + " " + alarm + " " + speed + " " + direction + " " + longitude + " " + latitude + " " + datetime + " " + device);
            }
        }
    }

You are reading the latitude and longitude as 8 bytes each, the spec you have says 4. double s in java, and getDouble in ByteBuffer , are 8 bytes, you want getFloat .您正在将纬度和经度读取为 8 个字节,您的规范说getDouble中的doubleByteBuffer getDouble是 8 个字节,您需要getFloat

This also corrupts the data after as you read too far.当你读得太远时,这也会破坏数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM