如何从 Java 中的数据流中查找时间戳和十进制值读数

How to find time stamp and decimal value reading from data stream in Java

因此,如果我们假设我们有以下数据从使用串行端口的微控制器流式传输到 Java:

0x21 0x32 0xCC 0x00 0x3A 0x98 0x02 0x12 0x03 0x21 0x33 0x01 0xC2

我一开始以为第一个字节是Magic Number,第二个字节是Key。那么这不会使数字 0xCC 0x00 0x3A 0x98 0x02 0x12 0x03 0x21 0x33 0x01 0xC2 成为我们从微控制器读取的值,我们只是将所有数字组合在一起以获得以下十进制读数:204058... and on?

对于时间戳,我们不只是将第二个字节 0x32 转换为十进制 50 unites of time 吗?

引用评论:

on the documentation it mentioned that the payload was a key-value pair and 0x32 was for "timestamp, 4-byte integer, milliseconds since reset" and 0x33 "potentiometer reading, 2-byte integer A/D counts" so does that mean the next time I hit 0x32 which is the second number and since its a 4-byte integer we concatonate 0xCC and 0x00 0x3A and 0x98?

引用问题中的示例数据:

0x21 0x32 0xCC 0x00 0x3A 0x98 0x02 0x12 0x03 0x21 0x33 0x01 0xC2

因此,key/value 对,其中键是单个字节,每个键具有不同长度的值。

0x32 was for "timestamp, 4-byte integer, milliseconds since reset"

0x21 0x32 0xCC 0x00 0x3A 0x98 0x02 0x12 0x03 0x21 0x33 0x01 0xC2
     ^^^^ ^^^^^^^^^^^^^^^^^^^
     key    4-byte integer

这 4 个字节可以是 big-endian 或 little-endian,但 big-endian 对于设备来说更常见,因此:

0xCC003A98 = 3422567064 ms = 39 days 14 hrs 42 mins 47.064 secs

0x33 "potentiometer reading, 2-byte integer A/D counts"

0x21 0x32 0xCC 0x00 0x3A 0x98 0x02 0x12 0x03 0x21 0x33 0x01 0xC2
                                                  ^^^^ ^^^^^^^^^
                                                  key  2-byte integer

0x01C2 = 450