1 /// <summary> 2 /// 获取校验和 3 /// </summary> 4 /// <param name="Command"></param> 5 /// <returns></returns> 6 public static string ADD2(string Command) 7 { 8 byte[] data = Encoding.ASCII.GetBytes(Command); 9 int sum = 0; 10 for (int i = 0; i < data.Length; i++) 11 { 12 sum += data[i]; 13 } 14 sum = sum % 256; 15 16 sum = ~sum + 1; 17 byte a = (byte)sum; 18 string bin = Convert.ToString(a, 2); 19 string verify = Convert.ToInt32(bin, 2).ToString("X2"); 20 21 return verify; 22 } 23 24 #region CS校验 25 /// <summary> 26 /// CS校验 27 /// </summary> 28 /// <param name="data">待校验Byte数组</param> 29 /// <returns>CS校验值</returns> 30 public static byte CS(byte[] data) 31 { 32 byte cs = 0; 33 for (int i = 0; i < data.Length; i++) 34 { 35 cs = (byte)((cs + data[i]) % 256); 36 } 37 return cs; 38 } 39 #endregion 40 41 #region BBC 42 /// <summary> 43 /// BBC校验 44 /// </summary> 45 /// <param name="data"></param> 46 /// <returns></returns> 47 public static String GetBCC(byte[] data) 48 { 49 String ret = ""; 50 byte[] BCC = new byte[1]; 51 for (int i = 0; i < data.Length; i++) 52 { 53 BCC[0] = (byte)(BCC[0] ^ data[i]); 54 } 55 String hex = (BCC[0] & 0xFF).ToString("X"); 56 57 if (hex.Length == 1) 58 { 59 hex = '0' + hex; 60 } 61 ret += hex.ToUpper(); 62 return ret; 63 } 64 #endregion 65 66 67 #region LRC和校验 68 /// <summary> 69 /// LRC和校验 70 /// </summary> 71 /// <param name="data"></param> 72 /// <returns></returns> 73 public static byte LRC(byte[] data) 74 { 75 byte lrc = 0; 76 foreach (byte c in data) 77 { 78 lrc += c; 79 } 80 return (byte)-lrc; 81 } 82 #endregion