You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB
C#

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);
}
/// <summary>
/// 和校验
/// </summary>
/// <param name="bytes"></param>
/// <param name="length"></param>
/// <returns></returns>
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;
}
}
}