Tool.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Runtime.InteropServices;
  2. using System.Text;
  3. namespace EasyTemplate.Service
  4. {
  5. public class Tool
  6. {
  7. public const int NozzleState_Offline = 0;
  8. public const int NozzleState_Idle = 1;
  9. public const int NozzleState_Filling = 2;
  10. public const int WarningState_Normal = 0;
  11. public const int WarningState_Prewarning = 1;
  12. public const int WarningState_Warning = 2;
  13. // 计算单个字节的CRC
  14. public static ushort calccrc(byte crcbuf, ushort crc)
  15. {
  16. byte i;
  17. crc = (ushort)(crc ^ crcbuf);
  18. for (i = 0; i < 8; i++)
  19. {
  20. byte chk;
  21. chk = (byte)(crc & 1);
  22. crc = (ushort)(crc >> 1);
  23. crc = (ushort)(crc & 0x7fff);
  24. if (chk == 1)
  25. crc = (ushort)(crc ^ 0xa001); /* CRC polynom=0xa001 */
  26. crc = (ushort)(crc & 0xffff);
  27. }
  28. return crc;
  29. }
  30. // 带自定义多项式的CRC计算
  31. public static ushort calccrc(byte crcbuf, ushort crc, ushort polynom)
  32. {
  33. byte i;
  34. crc = (ushort)(crc ^ crcbuf);
  35. for (i = 0; i < 8; i++)
  36. {
  37. byte chk;
  38. chk = (byte)(crc & 1);
  39. crc = (ushort)(crc >> 1);
  40. crc = (ushort)(crc & 0x7fff);
  41. if (chk == 1)
  42. crc = (ushort)(crc ^ polynom);
  43. crc = (ushort)(crc & 0xffff);
  44. }
  45. return crc;
  46. }
  47. // 标准CRC校验
  48. public static ushort chkcrc(byte[] buf, ushort len)
  49. {
  50. ushort i;
  51. ushort crc;
  52. crc = 0x0000; /* Initial crc value=0x0000 */
  53. for (i = 0; i < len; i++)
  54. {
  55. crc = calccrc(buf[i], crc);
  56. }
  57. return crc;
  58. }
  59. // 带多项式的CRC校验
  60. public static ushort chkcrc(byte[] buf, ushort len, ushort polynom)
  61. {
  62. ushort i;
  63. ushort crc;
  64. crc = 0x0000;
  65. for (i = 0; i < len; i++)
  66. {
  67. crc = calccrc(buf[i], crc, polynom);
  68. }
  69. return crc;
  70. }
  71. // Modbus CRC校验
  72. public static ushort chkcrc_modbus(byte[] buf, ushort len, ushort polynom)
  73. {
  74. ushort i;
  75. ushort crc;
  76. crc = 0xffff;
  77. for (i = 0; i < len; i++)
  78. {
  79. crc = calccrc(buf[i], crc, polynom);
  80. }
  81. return crc;
  82. }
  83. public static void crc_calc_sinopec(byte[] ptr_buffer, ushort length)
  84. {
  85. ushort crc_result;
  86. /* calculate CRC */
  87. crc_result = chkcrc(ptr_buffer, length);
  88. /* ... and store it */
  89. ptr_buffer[length] = (byte)(crc_result >> 8);
  90. ptr_buffer[length + 1] = (byte)crc_result;
  91. }
  92. public static string BytesToBcd(byte[] Bytes)
  93. {
  94. if (Bytes == null || Bytes.Length == 0)
  95. return string.Empty;
  96. StringBuilder result = new StringBuilder();
  97. foreach (byte b in Bytes)
  98. {
  99. // 提取高4位和低4位
  100. byte highNibble = (byte)((b >> 4) & 0x0F);
  101. byte lowNibble = (byte)(b & 0x0F);
  102. // 转换为字符
  103. result.Append((char)('0' + highNibble));
  104. result.Append((char)('0' + lowNibble));
  105. }
  106. return result.ToString();
  107. }
  108. public static T ByteToStructure<T>(Byte[] dataBuffer)
  109. {
  110. object structure = null;
  111. int size = Marshal.SizeOf(typeof(T));
  112. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  113. try
  114. {
  115. Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
  116. structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
  117. }
  118. catch(Exception e)
  119. {
  120. }
  121. finally
  122. {
  123. Marshal.FreeHGlobal(allocIntPtr);
  124. }
  125. return (T)structure;
  126. }
  127. public static byte[] StructToBytes(object structObj)
  128. {
  129. //得到结构体的大小
  130. int size = Marshal.SizeOf(structObj);
  131. //创建byte数组
  132. byte[] bytes = new byte[size];
  133. //分配结构体大小的内存空间
  134. IntPtr structPtr = Marshal.AllocHGlobal(size);
  135. //将结构体拷到分配好的内存空间
  136. Marshal.StructureToPtr(structObj, structPtr, false);
  137. //从内存空间拷到byte数组
  138. Marshal.Copy(structPtr, bytes, 0, size);
  139. //释放内存空间
  140. Marshal.FreeHGlobal(structPtr);
  141. //返回byte数组
  142. return bytes;
  143. }
  144. /// <summary>
  145. /// 将字节数组按大端序转换为整数
  146. /// </summary>
  147. /// <param name="bytes">字节数组</param>
  148. /// <returns>转换后的整数</returns>
  149. public static int BytesToInt(byte[] bytes)
  150. {
  151. if (bytes == null)
  152. throw new ArgumentNullException(nameof(bytes));
  153. if (bytes.Length == 0)
  154. return 0;
  155. if (bytes.Length > 4)
  156. throw new ArgumentException("字节数组长度不能超过4个字节");
  157. int result = 0;
  158. for (int i = 0; i < bytes.Length; i++)
  159. {
  160. result = (result << 8) | bytes[i];
  161. }
  162. return result;
  163. }
  164. }
  165. }