C# CRC 实现
我正在尝试将串行端口设备集成到我的应用程序中,该应用程序需要对我发送给它的字节进行 CRC-CCTT 验证。 我对管理字节数据包有点陌生,需要帮助。
它使用以下公式进行 CRC 演算:
[CRC-CCITT P(X)= X16 + C12 + C8 + 1]
例如,对于数据包:0xFC 0x05 0x11,CRC 为 0x5627。 然后我将此数据包发送到设备: 0xFC 0x05 0x11 0x27 0x56
另外,数据包长度将从 5 到 255 不等(包括 CRC 检查字节)
我不知道如何实现这一点,所以欢迎任何想法/建议。
希望我说清楚了, 提前致谢。
编辑: 这是我需要做的规范:
I am trying to integrate a Serial-port device into my application, which needs CRC-CCTT validation for the bytes that I send to it.
I'm kinda new into managing byte packets, and need help for this.
It uses this formula for making the CRC calculus:
[CRC-CCITT P(X)= X16 + C12 + C8 + 1]
So for example for the packet: 0xFC 0x05 0x11, the CRC is 0x5627.
Then I send this packet to the device: 0xFC 0x05 0x11 0x27 0x56
Also, packet lenghts will vary from 5 to 255 (including CRC checks bytes)
I don't know how to implement this, so any idea/suggestions will be welcome.
Hope I made myself clear,
Thanks in Advance.
EDIT:
here is the specification of what I need to do:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
标准 crc-ccitt 是 x16 + x12 + x5 + 1 我写了一个 @ http://www.sanity-free .com/133/crc_16_ccitt_in_csharp.html 如果我有时间,我会看看是否无法修改它以与 x16 + x12 + x8 + 1 多边形一起运行。
编辑:
给你:
示例:
standard crc-ccitt is x16 + x12 + x5 + 1 I wrote the one @ http://www.sanity-free.com/133/crc_16_ccitt_in_csharp.html If I have time I'll see if I can't modify it to run with the x16 + x12 + x8 + 1 poly.
EDIT:
here you go:
sample:
您是否尝试过使用谷歌搜索来举个例子?其中有很多。
示例 1: http://tomkaminski.com/crc32-hashalgorithm-c-net < br>
示例 2: http://www.sanity-free.com/12/crc32_implementation_in_csharp.html< /a>
您还可以通过 在 .Net 中获得本机 MD5 支持System.Security.Cryptography.MD5CryptoServiceProvider。
编辑:
如果您正在寻找 8 位算法:http ://www.codeproject.com/KB/cs/csRedundancyChckAlgorithm.aspx
和 16 位:http://www.sanity-free.com/133/crc_16_ccitt_in_csharp。 html
Have you tried Googling for an example? There are many of them.
Example 1: http://tomkaminski.com/crc32-hashalgorithm-c-net
Example 2: http://www.sanity-free.com/12/crc32_implementation_in_csharp.html
You also have native MD5 support in .Net through System.Security.Cryptography.MD5CryptoServiceProvider.
EDIT:
If you are looking for an 8-bit algorithm: http://www.codeproject.com/KB/cs/csRedundancyChckAlgorithm.aspx
And 16-bit: http://www.sanity-free.com/133/crc_16_ccitt_in_csharp.html
哈哈,我遇到了完全相同的状态请求序列,我目前正在开发与 CashCode Bill Validator 一起使用的软件:)。这是对我有用的代码,它是 CRC16-CCITT,其反转多项式等于 0x8408(代码中的 BDPConstants.Polynomial)。这就是对我有用的代码:
在某处初始化表(例如表单构造函数):
然后在代码中使用它,就像这样,
您可以使用字节列表而不是数组,更方便地将字节数据打包在“数据包”中进行发送
LOL, I've encountered exactly the same STATUS REQUEST sequense, i'm currently developing software to use with CashCode Bill Validator:). Here's the code worked for me, it's CRC16-CCITT with reversed polynomial equals 0x8408 (BDPConstants.Polynomial in the code). That's the code worked for me:
Initialize the table somewhere (e.g. Form constructor):
then use it in your code just like that,
You can use List of bytes instead of array, more convinient to pack byte data in the 'packet' for sending
它有效并且不需要表:
it works and dose not need table:
您实际上使用的是 CRC-XMODEM LSB-reverse(带有 0x8408 系数)。此演算的 C# 代码为:
阅读更多内容(或下载项目):
http://www.cirvirlab.com/index.php/c-sharp-code-examples/141-c-sharp-crc-computation.html
You are actually using CRC-XMODEM LSB-reverse (with 0x8408 coefficient). C# code for this calculus is:
Read more (or download project):
http://www.cirvirlab.com/index.php/c-sharp-code-examples/141-c-sharp-crc-computation.html