Files
Modbus.Net/NA200H/ModBus.Net/CRC16.cs
2014-08-27 16:00:31 +08:00

93 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModBus.Net
{
public class Crc16
{
/// <summary>
/// CRC验证表
/// </summary>
public byte[] crc_table = new byte[512];
private static Crc16 _crc16 = null;
public static Crc16 GetInstance()
{
if (_crc16 == null)
{
_crc16 = new Crc16();
}
return _crc16;
}
#region CRC码
/// <summary>
/// 生成CRC码
/// </summary>
/// <param name="message">发送或返回的命令CRC码除外</param>
/// <param name="CRC">生成的CRC码</param>
public short GetCRC(byte[] message, ref byte[] Rcvbuf)
{
int IX,IY,CRC;
int Len = message.Length;
CRC=0xFFFF;
//set all 1
if (Len<=0)
CRC = 0;
else
{
Len--;
for (IX=0;IX<=Len;IX++)
{
CRC=CRC^(message[IX]);
for(IY=0;IY<=7;IY++)
{
if ((CRC&1)!=0 )
CRC=(CRC>>1)^0xA001;
else
CRC=CRC>>1;
//
}
}
}
Rcvbuf[1] = (byte)((CRC & 0xff00)>>8);//高位置
Rcvbuf[0] = (byte)(CRC & 0x00ff); //低位置
CRC= Rcvbuf[0]<<8;
CRC+= Rcvbuf[1];
return (short)CRC;
}
#endregion
#region CRC验证
/// <summary>
/// CRC校验
/// </summary>
/// <param name="src">ST开头&&结尾</param>
/// <returns>十六进制数</returns>
public bool CrcEfficacy(byte[] byteframe)
{
byte[] recvbuff = new byte[2];
byte[] byteArr = new byte[byteframe.Length - 2];
Array.Copy(byteframe, 0, byteArr, 0, byteArr.Length);
GetCRC(byteArr, ref recvbuff);
if (recvbuff[0] == byteframe[byteframe.Length - 2] && recvbuff[1] == byteframe[byteframe.Length - 1])
{
return true;
}
else
{
return false;
}
}
#endregion
}
}