using HighWayIot.Common; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.IO; namespace UnitTest { [TestClass] public class UnitTest1 { MsgUtil msgUtil = MsgUtil.Instance; [TestMethod] public void TestMethod1() { var bytes = msgUtil.HexStrTorbytes("00 0A 00 13 01 00 00 02 10 00 00 01 99 6F 39 E9 27 00 00 02 0F 00 01"); var res = CalculateCheckSum(bytes); Assert.AreEqual(0x69, res); } public void CauCheck() { var bytes = msgUtil.HexStrTorbytes("00 0A 00 13 01 00 00 02 10 00 00 01 99 6F 39 E9 27 00 00 02 0F 00 01"); byte[] sum = new byte[1]; sum[0] = CalculateCheckSum(bytes); var res = msgUtil.bytesToHexStr(sum, sum.Length); Console.WriteLine(res); } /// /// 和校验 /// /// /// /// public byte CalculateCheckSum(byte[] bytes) { int length = bytes.Length; int check = 0; for (int i = 0; i < length; i++) { check += bytes[i]; } byte result = (byte)(255 - (check % 255)); return result; } } }