php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 377|回复: 0

crc16代码实现

[复制链接]

2572

主题

2579

帖子

9100

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6406
贡献
0
注册时间
2021-4-14
最后登录
2024-3-28
在线时间
653 小时
QQ
发表于 2022-5-8 09:56:22 | 显示全部楼层 |阅读模式
[mw_shl_code=c,true]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace crc16
{
    class Program
    {
        static void Main(string[] args)
        {
            ushort crcresult = 0;
            byte[] puchmsg = { 0x01,0x01,0x00,0x00, 0x00, 0x09 };
            crcresult = CRC16_MODBUS(puchmsg,6);//MODBUS协议CRC16校验结果
            string str_crc = string.Format("0x{0:X2}", crcresult);
            Console.WriteLine("MODBUS输出结果:低8位:{0},高8位:{1}", Convert.ToString(crcresult & 0xff, 16), Convert.ToString((crcresult & 0xff00) >> 8, 16));
            Console.WriteLine("MODBUS输出结果:{0}", str_crc);
            
            Console.Read();                                                                                                                                      
        }


        static ushort CRC16_MODBUS(byte[] puchMsg, int usDataLen)  
        {  
          ushort  wCRCin = 0xFFFF;
          ushort wCPoly = 0x8005;  
          byte wChar = 0;
          byte a = 0;
          for (int j = 0; j < usDataLen;j++ )
          {
              wChar = puchMsg[j];
              wChar = InvertUint8(wChar);
              wCRCin ^= (ushort)(wChar << 8);
              for (int i = 0; i < 8; i++)
              {
                  if ((wCRCin & 0x8000)==0x8000)
                      wCRCin = (ushort)((wCRCin << 1) ^ wCPoly);
                  else
                      wCRCin = (ushort)(wCRCin << 1);
              }
          }
          wCRCin = InvertUint16(wCRCin);  
          return (wCRCin);  
        }
        static byte InvertUint8(byte inputdata)
        {
            byte a0 = 0;
            for (int i = 0; i < 8; i++)
            {
                if ((inputdata & ((byte)Math.Pow(2, i))) == ((byte)Math.Pow(2, i)))
                {
                    a0 |= (byte)Math.Pow(2, 7 - i);
                }
                else
                { a0 |= 0x00; }
            }
            return a0;
        }
        static ushort InvertUint16(ushort inputdata)
        {
            ushort a0 = 0;
            for (int i = 0; i < 16; i++)
            {
                if ((inputdata & ((ushort)Math.Pow(2, i))) == ((ushort)Math.Pow(2, i)))
                {
                    a0 |= (ushort)Math.Pow(2, 15 - i);
                }
                else
                { a0 |= 0x00; }
            }
            return a0;
        }
    }
}
[/mw_shl_code]





上一篇:clientSocket:实现client的socket通信
下一篇:SQL 导出表结构到Excel 代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-3-29 03:33 , Processed in 0.173606 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表