C# 如何使用多项式 x16 + x12 + x5 + 1 计算 CRC 16

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15247244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:38:56  来源:igfitidea点击:

How to calculate CRC 16 with polynomial x16 + x12 + x5 + 1

c#.netserial-portcrccrc16

提问by fenix2222

I am trying to interface with some system and in their specs they require to calculate CRC 16 for serial communication. Here is an extract from documentation

我正在尝试与某些系统连接,并且在他们的规范中,他们需要计算用于串行通信的 CRC 16。这是文档的摘录

"16 bit CCITT CRC of the message utilizing the standard polynomial, X16 +X12 +X5 +1. Seed values are always 0 (zero)"

“使用标准多项式 X16 +X12 +X5 +1 的消息的 16 位 CCITT CRC。种子值始终为 0(零)”

First of all I only found 2-3 samples of C# code of how to do it and none of the seem to give me the correct value. I tried this one http://www.sanity-free.com/133/crc_16_ccitt_in_csharp.html, but I am not sure what to set for initial value. I tried zeros and still doesn't work.

首先,我只找到了 2-3 个 C# 代码示例,它们似乎都没有给我正确的值。我尝试了这个http://www.sanity-free.com/133/crc_16_ccitt_in_csharp.html,但我不确定要为初始值设置什么。我尝试了零,但仍然不起作用。

Data I am testing it with is:

我正在测试它的数据是:

0x00 0x09 0x10 0x01 0x01 0x7C 0xF4 0xB8 0x00, 

the CRC value I get is

我得到的CRC值是

0xF2 0x24, 

however their system says it should be

但是他们的系统说应该是

0xC0 0x2F

My understanding is that polynomial x16 + x12 + x5 + 1 = 0x11021, however even when I use this one in the code it still gives me wrong answer. What am I doing wrong?

我的理解是多项式 x16 + x12 + x5 + 1 = 0x11021,但是即使我在代码中使用这个多项式,它仍然给我错误的答案。我究竟做错了什么?

采纳答案by fenix2222

I figured it out. I had to use CRC16-CCITT Kermit inmplementation. I think their documentation needs to be updated as it uses a different polynomial.

我想到了。我不得不使用 CRC16-CCITT Kermit 实现。我认为他们的文档需要更新,因为它使用了不同的多项式。

http://www.sanity-free.com/147/standard_crc16_and_crc16_kermit_implementation_in_csharp.html

http://www.sanity-free.com/147/standard_crc16_and_crc16_kermit_implementation_in_csharp.html