WitProtocolUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Wit.SDK.Utils;
  8. namespace Wit.SDK.Modular.Sensor.Utils
  9. {
  10. /// <summary>
  11. /// 维特协议工具类
  12. /// </summary>
  13. public class WitProtocolUtils
  14. {
  15. #region SUM和校验
  16. /// <summary>
  17. /// 获取SUM和校验结果
  18. /// </summary>
  19. /// <param name="data"></param>
  20. /// <returns></returns>
  21. public static byte ToSUM(byte[] data)
  22. {
  23. int sum = 0;
  24. for (int i = 0; i < data.Length; i++)
  25. {
  26. sum = sum + data[i];
  27. }
  28. //实际上num 这里已经是结果了,如果只是取int 可以直接返回了
  29. return (byte)sum;
  30. }
  31. /// <summary>
  32. /// 校验数据包和校验
  33. /// </summary>
  34. /// <param name="dataPack"></param>
  35. /// <returns></returns>
  36. public static bool CheckSUM(byte[] dataPack)
  37. {
  38. if (dataPack == null || dataPack.Length < 2)
  39. {
  40. return false;
  41. }
  42. int sum = 0;
  43. for (int i = 0; i < dataPack.Length - 1; i++)
  44. {
  45. sum = sum + dataPack[i];
  46. }
  47. //实际上num 这里已经是结果了,如果只是取int 可以直接返回了
  48. byte check = (byte)sum;
  49. //如果最后一个字节等于校验和就是通过
  50. if (dataPack[dataPack.Length - 1] == check)
  51. {
  52. return true;
  53. }
  54. return false;
  55. }
  56. /// <summary>
  57. /// 校验数据包和校验
  58. /// </summary>
  59. /// <param name="hexPackData">一包16进制字符串数据</param>
  60. /// <returns></returns>
  61. public static bool CheckPackSUM(string hexPackData)
  62. {
  63. byte[] packData = ByteArrayConvert.HexStringToByteArray(hexPackData);
  64. return CheckSUM(packData);
  65. }
  66. #endregion
  67. /// <summary>
  68. /// 获得读取的命令
  69. /// 功能:传入寄存器获得,读取寄存器的命令
  70. /// </summary>
  71. /// <param name="reg">寄存器</param>
  72. /// <returns></returns>
  73. public static byte[] GetRead(int reg)
  74. {
  75. return new byte[] { 0xff,0xaa,0x27,(byte)reg, 0x00};
  76. }
  77. /// <summary>
  78. /// 获得写入的命令
  79. /// 功能:传入寄存器和值,得到写入寄存器的命令
  80. /// </summary>
  81. /// <param name="reg">寄存器</param>
  82. /// <param name="value">值</param>
  83. /// <returns></returns>
  84. public static byte[] GetWrite(int reg, ushort value)
  85. {
  86. byte[] vs = new byte[] { 0xff, 0xaa, (byte)reg, (byte)value, (byte)(value >> 8) };
  87. return new byte[] { 0xff, 0xaa, (byte)reg, (byte)value, (byte)(value>>8) };
  88. }
  89. /// <summary>
  90. /// 查找返回的数据
  91. /// 功能:从传感器返回的数据里找到第一包55 5F开头的数据包
  92. /// </summary>
  93. /// <param name="returnData">设备返回的数据</param>
  94. /// <returns>成功返回55 5f数据包,失败返回null</returns>
  95. public static byte[] FindReturnData(byte[] returnData)
  96. {
  97. byte[] tempArr = new byte[0];
  98. for (int i = 0; i < returnData.Length; i++)
  99. {
  100. tempArr = returnData.Skip(i).Take(11).ToArray(); ;
  101. if (tempArr.Length == 11 && tempArr[0] == 0x55 && tempArr[1] == 0x5F && CheckSUM(tempArr))
  102. {
  103. return tempArr;
  104. }
  105. }
  106. return null;
  107. }
  108. }
  109. }