java—这是解析串行数据的有效甚至正确的方法吗?

3b6akqbq  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(352)

在前言中,我是java新手(所以请接受我的建议,lol),但以下是我要做的:
我有一个gps冰球,我正在通过usb读取。gps冰球使用一些用户界面,但我正在尝试捕获数据,以便用于我自己的应用程序。我正在从串行缓冲区读取数据包,并试图将其拆分为有效负载,并将字段分解为哈希Map(不确定这是否是正确的应用程序)。
我想补充一点,在这种情况下,我关心效率的原因是,由于gps圆盘不断获取sat数据(大约每200/300ms),因此数据处理速度将相当快,因此,如果可能,我会尽量避免任何瓶颈。
这是我抛出的代码,我知道在代码中缓冲区没有读取任何内容,只是为了删除冗余代码,只要假设注解中写入的数据是我们在sys.out中所说的。

/* 

Assume this is serial data received from GPS serial packet:
B5 62 01 02 1C AA AA AA AA 01 02 03 04 0A 0B 0C 0D B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4 E1 E2 E3 E4 01 02;
     |  |   /  |                                                                                      /  |  
     |  \ len   \                                                                                    /  CRC
    /    |       \                                                                                  /
   /     |        \                                 PAYLOAD                                        /
Header   |         \                                                                              /
    class & id      \                                                                            /

* /

HashMap<String, String> payloadDict = new HashMap<String, String> (); // HashMap to write to

Bytes[] buffer; // serial buffer
String byteToHex = DataTypeConverter.parseHexBinary(buffer); // converts byte data to string in Hex

// Since the first few fields in the packet are static (header, class & id, and len),
// I strip the HEX string to only contain the payload
int payloadStartIndex = byteToHex.indexOf("C") + 1;
String payload = byteToHex.substring(payloadStartIndex, payloadStartIndex + 29);

/*

Assume this is the payload string after we parse:

AA AA AA AA 01 02 03 04 0A 0B 0C 0D B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4 E1 E2 E3 E4

**added seperation every 2 hex values for readability,

  but assume it's just one giant string

* /

// write to HashMap
payloadDict = NAV_POSLLH(payload);

/*

so to visualize what I would imagine being saved to the HashMap:

iTOW:   AA AA AA AA 
lon:    01 02 03 04 
lat:    0A 0B 0C 0D 
height: B1 B2 B3 B4 
hMSL:   C1 C2 C3 C4 
hAcc:   D1 D2 D3 D4 
vAcc:   E1 E2 E3 E4

* /

public Map<String, String> NAV_POSLLH(String dataArr) {
    // Temp HashMap
    HashMap<String, String> dict = new HashMap<String, String> ();

    /*
        this is the payload format.
        payloadStructure are the names of the all the fields I want to parse
        and hexOffset are the beginning of each field.      
    */

    String[] payloadStructure = new String[6] {"iTOW", "lon", "lat", "height", "hMSL", "hAcc", "vAcc"};
    int[] hexOffset = new int[6] {0, 7, 11, 15, 20, 24};

    // loop through each field 
    for(int i = 0; i < byteOffset.length; i++) {
        String parse = "";

        // I am not sure if this is the best way to handle out of index errors
        // in java, but this is what I thought would work
        if(byteOffset[i] == 24) {
            parse = dataArr.substring(hexOffset[i]);
            map.put(payloadStructure[i], parse);
            return dict;
        }

        // parse the substring from the beginning of each hex offset
        // to the next index -1
        parse = dataArr.substring(byteOffset[i], hexOffset[i+1] -1);
        map.put(payloadStructure[i], parse);

        // finally return HashMap
        return dict;
    }
}

我不确定这些评论是否能很好地解释数据包的结构,或者它是否令人困惑(因为格式很简陋),所以我附上了我制作的数据包布局的截图。

所以我想问的是:基于我所要完成的,这是一种有效的甚至是正确的实现方式吗?我不确定我是否有什么明显的遗漏。
我现在无法编译或测试任何代码,所以我只是试图将输出可视化,哈哈。非常感谢任何人的意见!

5jdjgkvh

5jdjgkvh1#

我会做一个 Packet 类并使用 ByteBuffer . 该类有很好的解析方法,包括各种原语大小的批量get方法。你可以考虑使用 ByteBuffer 对于该类的有效负载成员

相关问题