[英]How do i calculate the CRC checksum for the following TCP/IP protocol
Here is the following spec. 这是以下规格。
I use python3 to talk to a server with TCP/IP using a simple protocol as you see in the image. 如图所示,我使用python3使用简单协议与具有TCP / IP的服务器通信。
I wonder how do i calculate the CRC checksum here ?. 我想知道如何在这里计算CRC校验和吗? Its need to be 1 byte as you see in the Command package spec.
如您在Command软件包规格中所见,它需要为1个字节。
I presume in Python 3, if you have the packet in bytes, ie say packet = bytes(0xF0, 0xF0, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xF0)
, the checksum would simply be the least-significant byte of 1's complement of the sum of these bytes, ie (0 - sum(packet)) & 0xFF
. 我假设在Python 3中,如果您有以字节为单位的数据包,即说
packet = bytes(0xF0, 0xF0, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xF0, 0xF0)
,则校验和就是这些字节之和的1的补码的最低有效字节,即(0 - sum(packet)) & 0xFF
。
Thus, if you have a payload payload
, this would be the code to make it into a full packet: 因此,如果您有一个有效
payload
有效payload
,这将是将其变成完整数据包的代码:
packet = b'\xF0\xF0' + payload + b'\xF0\xF0'
packet += bytes([(0 - sum(packet)) & 0xFF])
Assuming this thing is the target here. 假设这件事是这里的目标。 What they probably mean is that you should sum all bytes together but store the result in a 8 bit variable (
uint8_t
in C). 它们可能意味着您应该将所有字节加在一起,然后将结果存储在8位变量(C中的
uint8_t
)中。 Meaning that this variable will overflow several times and the least significant byte of the data is what you end up with as checksum. 这意味着该变量将溢出几次,并且数据的最低有效字节就是您最终得到的校验和。
Similar techniques were often used in the 1970s, but then they often inverted the resulting LS byte into one's complement or two's complement. 1970年代经常使用类似的技术,但是随后,他们常常将结果LS字节反转为一个或两个的补码。 (For example this is how the Motorola S-record and Intel Hex linker formats got their checksums.)
(例如,这就是Motorola S-record和Intel Hex链接器格式获取校验和的方式。)
Please note: 请注意:
Therefore this product is not "suitable to any industrial automation devices". 因此,此产品“不适用于任何工业自动化设备”。 It shouldn't be used in an industrial environment.
它不应该在工业环境中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.