Extension.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using uint16_t = System.UInt16;
  8. using uint8_t = System.Byte;
  9. using int16_t = System.Int16;
  10. using int8_t = System.SByte;
  11. using System.Windows.Threading;
  12. namespace WpfTest1.ComAgent
  13. {
  14. public unsafe static class Extension
  15. {
  16. public static ushort cal_crc(byte[] ptr)
  17. {
  18. ushort crc = 0;
  19. byte num = 0;
  20. int len = ptr.Length;
  21. while (len-- != 0)
  22. {
  23. for (byte i = 128; i != 0; i = (byte)((int)i / 2))
  24. {
  25. if ((crc & 0x8000u) != 0)
  26. {
  27. crc = (ushort)(crc * 2);
  28. crc = (ushort)(crc ^ 0x1021u);
  29. }
  30. else
  31. {
  32. crc = (ushort)(crc * 2);
  33. }
  34. if ((ptr[num] & i) != 0)
  35. {
  36. crc = (ushort)(crc ^ 0x1021u);
  37. }
  38. }
  39. num = (byte)(num + 1);
  40. }
  41. return crc;
  42. }
  43. private static byte[] pack0x10(byte[] source)
  44. {
  45. List<byte> list = new List<byte>();
  46. for (int i = 0; i < source.Length; i++)
  47. {
  48. if (source[i] == 16)
  49. {
  50. list.Add(16);
  51. }
  52. list.Add(source[i]);
  53. }
  54. return list.ToArray();
  55. }
  56. private static byte[] unpack0x10(byte[] dest)
  57. {
  58. List<byte> list = new List<byte>();
  59. for (int i = 0; i < dest.Length; i++)
  60. {
  61. if (dest[i] == 16)
  62. {
  63. i++;
  64. }
  65. list.Add(dest[i]);
  66. }
  67. return list.ToArray();
  68. }
  69. public unsafe static byte[] copyz(ref byte* begin, int size)
  70. {
  71. byte[] list = new byte[size];
  72. for (int i = 0; i < size; i++)
  73. {
  74. list[i] = *begin++;
  75. }
  76. return list;
  77. }
  78. public static bool bEquals(this byte[] lhs, byte[] rhs)
  79. {
  80. if (lhs.Length != rhs.Length)
  81. {
  82. return false;
  83. }
  84. for (int i = 0; i < lhs.Length; i++)
  85. {
  86. if (lhs[i] != rhs[i])
  87. {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. public static bool bEquals(this byte[] lhs, byte[] rhs, int len)
  94. {
  95. if (lhs.Length < len || rhs.Length < len)
  96. {
  97. return false;
  98. }
  99. for (int i = 0; i < len; i++)
  100. {
  101. if (lhs[i] != rhs[i])
  102. {
  103. return false;
  104. }
  105. }
  106. return true;
  107. }
  108. public static T[] pack<T>(this T first, params T[] other)
  109. {
  110. T[] t = new T[other.Length + 1];
  111. t[0] = first;
  112. for (int i = 0; i < other.Length; i++)
  113. {
  114. t[i + 1] = other[i];
  115. }
  116. return t;
  117. }
  118. public static void shutdown(this ISerialPortAdapter sp)
  119. {
  120. try
  121. {
  122. sp?.Close();
  123. sp?.Dispose();
  124. }
  125. catch
  126. {
  127. }
  128. }
  129. public static void send(this ISerialPortAdapter sp, byte[] data)
  130. {
  131. sp.Write(data, 0, data.Length);
  132. }
  133. public static void read(this ISerialPortAdapter sp, byte[] data, int len)
  134. {
  135. int cursor = 0;
  136. while ((cursor += sp.Read(data, cursor, len - cursor)) < len)
  137. {
  138. }
  139. }
  140. public static byte b(this int i)
  141. {
  142. return (byte)i;
  143. }
  144. public static void addRange<T>(this List<T> list, T[] buff, int len)
  145. {
  146. for (int i = 0; i < len; i++)
  147. {
  148. list.Add(buff[i]);
  149. }
  150. }
  151. public static byte[] buff(this int i)
  152. {
  153. return BitConverter.GetBytes(i);
  154. }
  155. public static byte[] buff(this uint i)
  156. {
  157. return BitConverter.GetBytes(i);
  158. }
  159. public static byte[] buff(this byte i)
  160. {
  161. return i.pack();
  162. }
  163. public static byte[] buff(this ushort i)
  164. {
  165. byte[] buff = BitConverter.GetBytes(i);
  166. Array.Reverse(buff);
  167. return buff;
  168. }
  169. public static byte[] buff(this short i)
  170. {
  171. byte[] buff = BitConverter.GetBytes(i);
  172. Array.Reverse(buff);
  173. return buff;
  174. }
  175. public static byte[] packData(this byte[] inputData)
  176. {
  177. byte[] data = pack0x10(inputData);
  178. byte[] data_size = ((ushort)(data.Length + 2)).buff();
  179. List<byte> buff = new List<byte>();
  180. buff.Add(16);
  181. buff.Add(2);
  182. buff.AddRange(data_size);
  183. buff.AddRange(data);
  184. buff.AddRange(cal_crc(data_size.add(data)).buff());
  185. buff.Add(16);
  186. buff.Add(3);
  187. return buff.ToArray();
  188. }
  189. public static T[] add<T>(this T[] obj, T[] other)
  190. {
  191. T[] list = new T[obj.Length + other.Length];
  192. for (int j = 0; j < obj.Length; j++)
  193. {
  194. list[j] = obj[j];
  195. }
  196. for (int i = 0; i < other.Length; i++)
  197. {
  198. list[i + obj.Length] = other[i];
  199. }
  200. return list;
  201. }
  202. public unsafe static byte[] unpackData(this byte[] inputData)
  203. {
  204. byte[] buff = null;
  205. fixed (byte* ptr_constraint = inputData)
  206. {
  207. byte* ptr = ptr_constraint;
  208. if (!copyz(ref ptr, 2).bEquals(new byte[2] { 16, 2 }))
  209. {
  210. throw new ComException("Invaild package head.");
  211. }
  212. ushort length = BitConverter.ToUInt16(copyz(ref ptr, 2).Reverse().ToArray(), 0);
  213. buff = copyz(ref ptr, length - 2);
  214. ushort ucrc = BitConverter.ToUInt16(copyz(ref ptr, 2), 0);
  215. #if !IGNCRC
  216. if (cal_crc(buff) != ucrc)
  217. {
  218. throw new ComException("CRC check failed.");
  219. }
  220. #endif
  221. if (!copyz(ref ptr, 2).bEquals(new byte[2] { 16, 3 }))
  222. {
  223. throw new ComException("Invaild package foot.");
  224. }
  225. }
  226. return buff;
  227. }
  228. public static T[] subArray<T>(this T[] array, int begin, int len)
  229. {
  230. T[] arr = new T[len];
  231. for (int i = 0; i < len; i++)
  232. {
  233. arr[i] = array[begin + i];
  234. }
  235. return arr;
  236. }
  237. public static void async(this object any, Action act)
  238. {
  239. any.async(act, App.Current.Dispatcher);
  240. }
  241. public static void async(this object any, Action act, Dispatcher dispatcher)
  242. {
  243. dispatcher.Invoke(act);
  244. }
  245. public static string print16(this byte[] buff)
  246. {
  247. StringBuilder sb = new StringBuilder();
  248. for (int i = 0; i < buff.Length; i++)
  249. {
  250. sb.AppendFormat("{0:x2} ", buff[i]);
  251. }
  252. return sb.ToString();
  253. }
  254. }
  255. }