识别字节流中的数据包(Identification of packets in a byte stream)

编程入门 行业动态 更新时间:2024-10-23 22:29:44
识别字节流中的数据包(Identification of packets in a byte stream)

与加速计传感器的通信有点问题。 传感器连续输出大约8000个读数/秒。 传感器插入带有adaper的usb端口,显示为com4。 我的问题是我似乎无法从字节流中挑选传感器读取数据包。 数据包大小为五个字节,格式如下:

High nibble Low nibble Byte 1 checksum, id for packet start X high Byte 2 X mid X low Byte 3 Y high Y mid Byte 4 Y low Z high Byte 5 Y mid Y low

X,y,z是加速度。

在传感器的文档中,它指出第一个字节中的高半字节是校验和(计算的Xhigh + Xlow + Yhigh + Ylow + Zhigh + Zlow),但也是数据包开始的标识。 我很擅长针对外部设备进行编程,并且无法真正掌握如何将校验和用作程序包启动的标识符(校验和不会一直变化吗?)。 这是识别数据包开始的常用方法吗? 有谁知道如何解决这个问题?

任何帮助将不胜感激。

I'm having a bit of a problem with the communication to an accelerometer sensor. The sensor puts out about 8000 readings/second continuously. The sensor is plugged in to a usb port with an adaper and shows up as com4. My problem is that I can't seem to pick out the sensor reading packets from the byte stream. The packets have the size of five bytes and have the following format:

High nibble Low nibble Byte 1 checksum, id for packet start X high Byte 2 X mid X low Byte 3 Y high Y mid Byte 4 Y low Z high Byte 5 Y mid Y low

X, y, z is the acceleration.

In the documentation for the sensor it states that the high nibble in the first byte is the checksum (calculated Xhigh+Xlow+Yhigh+Ylow+Zhigh+Zlow) but also the identification of the packet start. I'm pretty new to programming against external devices and can't really grasp how the checksum can be used as an identifier for the start of the package (wouldn't the checksum change all the time?). Is this a common way for identifying the start of a packet? Does anyone have any idea how to solve this problem?

Any help would be greatly appreciated.

最满意答案

...无法真正掌握校验和如何用作包的开始的标识符(校验和不会一直变化吗?)。

是的,校验和会发生变化,因为它来自数据。 但即使是固定值的数据包开始半字节(单独)也不足以(最初)识别(或验证)数据包。 由于这是二进制数据(而不是文本),因此数据可以采用与任何固定值数据包开始相同的值。 如果你对这个初始半字节进行了一次微不足道的扫描,那么该算法很容易将数据半字节错误识别为起始半字节。

这是识别数据包开始的常用方法吗?

不,但鉴于高数据速率,似乎是一种最小化数据包大小的方案。

有谁知道如何解决这个问题?

您可能必须首先扫描每个字节序列五次(即数据包帧的长度)。 计算此帧的校验和,并将其与第一个半字节进行比较。 匹配表示您(可能)具有帧对齐。 不匹配意味着您应该丢弃第一个字节,并测试下一个可能的第二个字节开始的数据包帧(即移位剩余的4个字节并附加新的第5个字节)。

一旦实现(或假设)帧对齐,您需要不断验证每个数据包的校验和,以确认数据完整性并确保帧对齐。 任何校验和错误都应该强制进行另一次正确的帧对齐(从当前数据包的第二个字节开始)。

... can't really grasp how the checksum can be used as an identifier for the start of the package (wouldn't the checksum change all the time?).

Yes, the checksum would change since it is derived from the data. But even a fixed-value start-of-packet nibble would (by itself) not be sufficient to (initially) identify (or verify) data packets. Since this is binary data (rather than text), the data can take on the same value as any fixed-value start-of-packet. If you had a trivial scan for this start-nibble, that algorithm could easily misidentify a data nibble as the start-nibble.

Is this a common way for identifying the start of a packet?

No, but given the high data rate, it seems to be a scheme to minimize the packet size.

Does anyone have any idea how to solve this problem?

You probably have to initially scan every sequence of bytes five at a time (i.e. the length of a packet frame). Calculate the checksum of this frame, and compare it to the first nibble. A match indicates that you (may) have frame alignment. A mismatch means that you should toss the first byte, and test the next possible packet frame that would start with what was the second byte (i.e. shift the 4 remaining bytes and append a new 5th byte).

Once frame alignment has been achieved (or assumed), you need to continually verify the checksum of every packet in order to confirm data integrity and ensure frame alignment. Any checksum error should force another hunt for correct frame alignment (starting at the 2nd byte of the current packet).

更多推荐

本文发布于:2023-08-05 16:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据包   字节   Identification   stream   byte

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!