CmdUtils.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Wit.SDK.Modular.Sensor.Device.Entity;
  7. using Wit.SDK.Utils;
  8. namespace Wit.SDK.Modular.Sensor.Utils
  9. {
  10. /// <summary>
  11. /// 脚本解释器
  12. /// </summary>
  13. public class CmdUtils
  14. {
  15. /// <summary>
  16. /// 构建指令
  17. /// </summary>
  18. /// <returns></returns>
  19. public static string GenerationCmd(string cmd, Dictionary<string, object> dataResource, string value, string deviceName, string text, int valueLength = 0)
  20. {
  21. return Encoding.Default.GetString(GenerationCmd(cmd, false, false, dataResource, value, deviceName, text, valueLength));
  22. }
  23. /// <summary>
  24. /// 生成要发送的byte数组
  25. /// </summary>
  26. /// <param name="cmdBean"></param>
  27. /// <returns></returns>
  28. public static byte[] GenerationCmd(CmdBean cmdBean, Dictionary<string, object> dataResource) {
  29. return GenerationCmd(cmdBean.sendData, cmdBean.sendHex, cmdBean.sendNewLine, dataResource);
  30. }
  31. /// <summary>
  32. /// 生成要发送的byte数组
  33. /// </summary>
  34. /// <param name="cmd"></param>
  35. /// <param name="sendHex"></param>
  36. /// <param name="sendNewLine"></param>
  37. /// <param name="dataResource"></param>
  38. /// <param name="value"></param>
  39. /// <returns></returns>
  40. public static byte[] GenerationCmd(string cmd, bool sendHex, bool sendNewLine, Dictionary<string, object> dataResource, string value = null, string deviceName = null, string text = null, int valueLegnth = 0)
  41. {
  42. try
  43. {
  44. if (sendHex && !string.IsNullOrEmpty(value))
  45. {
  46. value = int.Parse(value).ToString("X") + "";
  47. if (valueLegnth != 0 && value.Length < valueLegnth)
  48. {
  49. int t = valueLegnth - value.Length;
  50. value = value.PadLeft(valueLegnth, '0');
  51. }
  52. if (value.Length > 2)
  53. {
  54. value = value.Substring(0, 2) + " " + value.Substring(2);
  55. }
  56. }
  57. if (value != null)
  58. //替换值到命令里
  59. cmd = cmd.Replace("${VAL}", value);
  60. if (deviceName != null)
  61. //替换值到命令里
  62. cmd = cmd.Replace("${DEVICE_NAME}", deviceName);
  63. if (text != null)
  64. //替换值到命令里
  65. cmd = cmd.Replace("${TEXT}", text);
  66. if (sendHex)
  67. {
  68. //替换变量到命令里
  69. cmd = cmdParse(cmd, dataResource, true);
  70. }
  71. else
  72. {
  73. cmd = cmdParse(cmd, dataResource, false);
  74. }
  75. //要发送的数据
  76. byte[] data = new byte[0];
  77. //如果发送回车换行
  78. if (sendNewLine)
  79. {
  80. cmd = cmd + "\r\n";
  81. }
  82. //检查是不是要加crc校验,如果要就先把占位符换出来
  83. bool isCrc16 = false;
  84. if (cmd.Contains("${CRC16}"))
  85. {
  86. isCrc16 = true;
  87. cmd = cmd.Replace("${CRC16}", "");
  88. }
  89. //去除前面和后面的空格
  90. cmd = cmd.Trim(' ');
  91. //如果是16进制发送
  92. if (sendHex)
  93. {
  94. data = ByteArrayConvert.HexStringToByteArray(cmd);
  95. }
  96. else
  97. {
  98. data = ByteArrayConvert.StringToByteArray(cmd);
  99. }
  100. //如果加crc16校验
  101. if (isCrc16)
  102. {
  103. byte[] crc16 = Modbus16Utils.GetCrc16(data);
  104. List<byte> l = data.ToList();
  105. l.AddRange(crc16);
  106. data = l.ToArray();
  107. }
  108. return data;
  109. }
  110. catch (Exception ex)
  111. {
  112. return Encoding.Default.GetBytes(ex.Message);
  113. }
  114. }
  115. /// <summary>
  116. /// 将变量代入到命令里
  117. /// </summary>
  118. /// <param name="cmd"></param>
  119. /// <returns></returns>
  120. public static string cmdParse(string cmd, Dictionary<string, object> dataResource, bool ishex)
  121. {
  122. string[] keyArr = dataResource.Keys.ToArray();
  123. for (int i = 0; i < keyArr.Length; i++)
  124. {
  125. string key = keyArr[i];
  126. string value = dataResource[key]!=null ? dataResource[key].ToString(): "";
  127. //变量名称
  128. string vname = "${" + key + "}";
  129. //如果命令里包含变量就把值代入进去
  130. if (cmd.Contains(vname))
  131. {
  132. if (ishex)
  133. {
  134. cmd = cmd.Replace(vname, int.Parse(value).ToString("X"));
  135. }
  136. else
  137. {
  138. cmd = cmd.Replace(vname, value);
  139. }
  140. }
  141. }
  142. return cmd;
  143. }
  144. }
  145. }