字节流中数据包的标识 [英] Identification of packets in a byte stream

查看:261
本文介绍了字节流中数据包的标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与加速度传感器的通讯有点问题.传感器连续输出约8000个读数/秒.传感器已插入带有适配器的USB端口,并显示为com4.我的问题是我似乎无法从传感器读取字节流中的数据包.数据包的大小为5个字节,并具有以下格式:

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是加速度.

X, y, z is the acceleration.

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

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?

任何帮助将不胜感激.

推荐答案

...无法真正掌握如何将校验和用作包开头的标识符(校验和是否一直都在变化吗?).

... 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?

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

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).

这篇关于字节流中数据包的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆