public static byte[] ConvertToByte(SendMessage message) { List <byte> list = new List <byte>(); list.AddRange(Head); list.AddRange(message.ClientCode); list.Add(message.CenterCode); list.AddRange(TimeToByte(message.SendTime)); list.AddRange(BCDUtil.ConvertFrom(message.Serial.ToString(), 2)); list.AddRange(BytesUtil.ToHexArray(message.FunctionCode)); int DataLength = (message.Body == null) ? 0 : message.Body.Length; string DataLengthStr = "8" + DataLength.ToString("X3"); list.AddRange(BytesUtil.ToHexArray(DataLengthStr)); list.Add(BodyStart); if (message.Body != null) { list.AddRange(message.Body); } byte[] CRC = CRCUtil.CRC16(list.ToArray()); list.Add(Tail); list.AddRange(CRC); return(list.ToArray()); }
public bool CheckCRC(byte[] crc, int endBeforeLength) { byte[] checkData = new byte[endBeforeLength]; Array.Copy(Data, 0, checkData, 0, endBeforeLength); string str = BytesUtil.ToHexString(CRCUtil.CRC16(checkData)); return(str.Equals(BytesUtil.ToHexString(crc))); }