终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

212 regels
6.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BPASmartClient.Modbus
  7. {
  8. public class MODBUS
  9. {
  10. /// <summary>
  11. /// 赋值string
  12. /// </summary>
  13. /// <param name="src"></param>
  14. /// <param name="start"></param>
  15. /// <param name="value"></param>
  16. /// <returns></returns>
  17. public static void SetString(ushort[] src, int start, string value)
  18. {
  19. byte[] bytesTemp = Encoding.UTF8.GetBytes(value);
  20. ushort[] dest = Bytes2Ushorts(bytesTemp);
  21. dest.CopyTo(src, start);
  22. }
  23. /// <summary>
  24. /// 获取string
  25. /// </summary>
  26. /// <param name="src"></param>
  27. /// <param name="start"></param>
  28. /// <param name="len"></param>
  29. /// <returns></returns>
  30. public static string GetString(ushort[] src, int start, int len)
  31. {
  32. ushort[] temp = new ushort[len];
  33. for (int i = 0; i < len; i++)
  34. {
  35. temp[i] = src[i + start];
  36. }
  37. byte[] bytesTemp = Ushorts2Bytes(temp);
  38. string res = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' });
  39. return res;
  40. }
  41. /// <summary>
  42. /// 赋值Real类型数据
  43. /// </summary>
  44. /// <param name="src"></param>
  45. /// <param name="start"></param>
  46. /// <param name="value"></param>
  47. public static void SetReal(ushort[] src, int start, float value)
  48. {
  49. byte[] bytes = BitConverter.GetBytes(value);
  50. ushort[] dest = Bytes2Ushorts(bytes);
  51. dest.CopyTo(src, start);
  52. }
  53. /// <summary>
  54. /// 获取float类型数据
  55. /// </summary>
  56. /// <param name="src"></param>
  57. /// <param name="start"></param>
  58. /// <returns></returns>
  59. public static float GetReal(ushort[] src, int start)
  60. {
  61. ushort[] temp = new ushort[2];
  62. for (int i = 0; i < 2; i++)
  63. {
  64. temp[i] = src[i + start];
  65. }
  66. byte[] bytesTemp = Ushorts2Bytes(temp);
  67. float res = BitConverter.ToSingle(bytesTemp, 0);
  68. return res;
  69. }
  70. /// <summary>
  71. /// 赋值Short类型数据
  72. /// </summary>
  73. /// <param name="src"></param>
  74. /// <param name="start"></param>
  75. /// <param name="value"></param>
  76. public static void SetShort(ushort[] src, int start, short value)
  77. {
  78. byte[] bytes = BitConverter.GetBytes(value);
  79. ushort[] dest = Bytes2Ushorts(bytes);
  80. dest.CopyTo(src, start);
  81. }
  82. /// <summary>
  83. /// 获取short类型数据
  84. /// </summary>
  85. /// <param name="src"></param>
  86. /// <param name="start"></param>
  87. /// <returns></returns>
  88. public static short GetShort(ushort[] src, int start)
  89. {
  90. ushort[] temp = new ushort[1];
  91. temp[0] = src[start];
  92. byte[] bytesTemp = Ushorts2Bytes(temp);
  93. short res = BitConverter.ToInt16(bytesTemp, 0);
  94. return res;
  95. }
  96. public static bool[] GetBools(ushort[] src, int start, int num)
  97. {
  98. ushort[] temp = new ushort[num];
  99. for (int i = start; i < start + num; i++)
  100. {
  101. temp[i] = src[i + start];
  102. }
  103. byte[] bytes = Ushorts2Bytes(temp);
  104. bool[] res = Bytes2Bools(bytes);
  105. return res;
  106. }
  107. private static bool[] Bytes2Bools(byte[] b)
  108. {
  109. bool[] array = new bool[8 * b.Length];
  110. for (int i = 0; i < b.Length; i++)
  111. {
  112. for (int j = 0; j < 8; j++)
  113. {
  114. array[i * 8 + j] = (b[i] & 1) == 1;//判定byte的最后一位是否为1,若为1,则是true;否则是false
  115. b[i] = (byte)(b[i] >> 1);//将byte右移一位
  116. }
  117. }
  118. return array;
  119. }
  120. private static byte Bools2Byte(bool[] array)
  121. {
  122. if (array != null && array.Length > 0)
  123. {
  124. byte b = 0;
  125. for (int i = 0; i < 8; i++)
  126. {
  127. if (array[i])
  128. {
  129. byte nn = (byte)(1 << i);//左移一位,相当于×2
  130. b += nn;
  131. }
  132. }
  133. return b;
  134. }
  135. return 0;
  136. }
  137. private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
  138. {
  139. int len = src.Length;
  140. byte[] srcPlus = new byte[len + 1];
  141. src.CopyTo(srcPlus, 0);
  142. int count = len >> 1;
  143. if (len % 2 != 0)
  144. {
  145. count += 1;
  146. }
  147. ushort[] dest = new ushort[count];
  148. if (reverse)
  149. {
  150. for (int i = 0; i < count; i++)
  151. {
  152. dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
  153. }
  154. }
  155. else
  156. {
  157. for (int i = 0; i < count; i++)
  158. {
  159. dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
  160. }
  161. }
  162. return dest;
  163. }
  164. private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
  165. {
  166. int count = src.Length;
  167. byte[] dest = new byte[count << 1];
  168. if (reverse)
  169. {
  170. for (int i = 0; i < count; i++)
  171. {
  172. dest[i * 2] = (byte)(src[i] >> 8);
  173. dest[i * 2 + 1] = (byte)(src[i] >> 0);
  174. }
  175. }
  176. else
  177. {
  178. for (int i = 0; i < count; i++)
  179. {
  180. dest[i * 2] = (byte)(src[i] >> 0);
  181. dest[i * 2 + 1] = (byte)(src[i] >> 8);
  182. }
  183. }
  184. return dest;
  185. }
  186. }
  187. }