简体   繁体   English

如何为以下TCP / IP协议计算CRC校验和

[英]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: 请注意:

  • This is not a CRC. 这不是CRC。 CRC = cyclic redundancy check, the name of an entirely different kind of algorithm , that can be used to calculate checksums. CRC =循环冗余校验,一种完全不同的算法的名称,可用于计算校验和。 The person who made the manual/protocol is incompetent. 手册/协议的制定者不称职。
  • 8 bit addition-based checksum is an amateur solution. 基于8位加法的校验和是一个业余解决方案。 It mostly just sits there as waste of space, as the probability for missing even simple single bit faults is laughably poor, basically 50/50. 它大多只是浪费空间,因为即使是简单的单个位故障,丢失的可能性也很低,基本上是50/50。 This is what data protocols looked like in the 1960s when people didn't know better. 这就是人们不了解的数据协议在1960年代的样子。 If you want any form of data integrity, this won't do. 如果您需要任何形式的数据完整性,则不会这样做。
  • RS-232 in general is an amateur solution, barely any better than raw UART/TTL levels. 一般来说,RS-232是一种业余解决方案,几乎没有原始UART / TTL级别更好。 This 1960s technique lived on because PC:s supported it for quite a while. 1960年代的这项技术得以延续,是因为PC:s支持了一段时间。 They don't any longer, they use USB. 他们不再使用USB。 As for industrial applications, they use RS-422/RS-485 or better yet CAN. 对于工业应用,它们使用RS-422 / RS-485或更好的CAN。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM