味魔方demo
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace CommcationLibray
  8. {
  9. /// <summary>
  10. /// ModbusRTU / ModbusAscll / ModbusTCP
  11. /// 通用公共抽象类
  12. /// </summary>
  13. public abstract class ModbusBase
  14. {
  15. /// <summary>
  16. /// 封装公共报文区域
  17. /// </summary>
  18. /// <param name="unit_id"></param>
  19. /// <param name="func_code"></param>
  20. /// <param name="start_addr"></param>
  21. /// <param name="count"></param>
  22. /// <returns></returns>
  23. public byte[] ReadCommandBytes(byte unit_id, byte func_code, ushort start_addr, ushort count)
  24. {
  25. byte[] req_bytes = new byte[] {
  26. unit_id,
  27. func_code,
  28. (byte)(start_addr/256),
  29. (byte)(start_addr%256),
  30. (byte)(count/256),
  31. (byte)(count%256)
  32. };
  33. return req_bytes;
  34. }
  35. /// <summary>
  36. /// 获取对应的数据类型
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="data_bytes"></param>
  40. /// <returns></returns>
  41. public T[] GetValues<T>(byte[] data_bytes)
  42. {
  43. //GetValues<ushort> data_bytes 每两个字节转换成一个数字 float 4Bytes double 8
  44. //2 ushort short int16 uint16
  45. //4 int uint int32 uint32 float
  46. //8 double
  47. //16 decimal
  48. List<T> list = new List<T>();
  49. try
  50. {
  51. //检查类型的长度
  52. var type_len = Marshal.SizeOf(typeof(T));
  53. for (int i = 0; i < data_bytes.Length; i+= type_len)
  54. {
  55. //取出对应类型字节
  56. var temp_bytes = data_bytes.ToList().GetRange(i, type_len);
  57. if (BitConverter.IsLittleEndian)
  58. {
  59. temp_bytes.Reverse();
  60. }
  61. Type bitConverter_type = typeof(BitConverter);
  62. var mis = bitConverter_type.GetMethods().ToList();
  63. var mi = mis.FirstOrDefault(mi => mi.ReturnType == typeof(T) && mi.GetParameters().Length == 2);
  64. if (mi == null)
  65. {
  66. throw new Exception("数据转换类型出错!");
  67. }
  68. object value = mi.Invoke(bitConverter_type, new object[] { temp_bytes.ToArray(), 0 });
  69. list.Add((T)value);
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. throw ex;
  75. }
  76. return list.ToArray();
  77. }
  78. public object[] GetValues(byte[] data_bytes, Type[] types)
  79. {
  80. object[] values = new object[types.Length];
  81. int index = 0;
  82. int i =0;
  83. foreach (var type in types)
  84. {
  85. var type_len = Marshal.SizeOf(type);
  86. var temp_bytes = data_bytes.ToList().GetRange(index, type_len);
  87. temp_bytes.Reverse();
  88. index += type_len;
  89. Type bitConverter_type = typeof(BitConverter);
  90. var mis = bitConverter_type.GetMethods().ToList();
  91. var mi = mis.FirstOrDefault(mi => mi.ReturnType == type && mi.GetParameters().Length == 2);
  92. if (mi == null)
  93. {
  94. throw new Exception("数据转换类型出错!");
  95. }
  96. object value = mi.Invoke(bitConverter_type, new object[] { temp_bytes.ToArray(), 0 });
  97. values[i++] = value;
  98. }
  99. return values;
  100. }
  101. }
  102. }