MessageCoder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using ProjectBase.Data.Logs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SIASUN.Autopilot.Device
  8. {
  9. public static class MessageCoder
  10. {
  11. // can总线数据包长度
  12. private static int ByteLength = 8;
  13. /// <summary>
  14. /// 序列化数据对象
  15. /// </summary>
  16. /// <param name="dataModel">数据对象</param>
  17. public static byte[] Serialize(object dataModel)
  18. {
  19. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  20. byte[] data = new byte[8];
  21. foreach (System.Reflection.PropertyInfo field in dataModel.GetType().GetProperties())
  22. {
  23. object[] objs = field.GetCustomAttributes(typeof(DataAttribute), false);
  24. if (objs.Length > 0)
  25. {
  26. DataAttribute fieldAttr = (DataAttribute)objs[0];
  27. if (fieldAttr.DataType == "LIST")
  28. {
  29. System.Collections.IList fieldValues = field.GetValue(dataModel, null) as System.Collections.IList;
  30. foreach (object fieldValue in fieldValues)
  31. {
  32. byte[] bytefiled = Serialize(fieldValue);
  33. ms.Write(bytefiled, 0, bytefiled.Length);
  34. }
  35. }
  36. else if (fieldAttr.DataType == "binary")
  37. {
  38. string fieldValue = field.GetValue(dataModel, null) == null ? "" : field.GetValue(dataModel, null).ToString();
  39. byte[] bytefiled = serializeField(fieldAttr.BitStart, fieldValue, fieldAttr.BitLength);
  40. ms.Write(bytefiled, fieldAttr.BitStart, bytefiled.Length);
  41. }
  42. else
  43. {
  44. LogHelper.log.Error("数据类型不是binary");
  45. }
  46. }
  47. }
  48. return ms.ToArray();
  49. }
  50. /// <summary>
  51. /// 反序列化数据对象
  52. /// </summary>
  53. /// <param name="dataModel">数据对象</param>
  54. /// <param name="ms">数据流</param>
  55. public static object DeSerialize(object dataModel, byte[] bytes)
  56. {
  57. foreach (System.Reflection.PropertyInfo field in dataModel.GetType().GetProperties())
  58. {
  59. object[] objs = field.GetCustomAttributes(typeof(DataAttribute), false);
  60. if (objs.Length > 0)
  61. {
  62. DataAttribute fieldAttr = (DataAttribute)objs[0];
  63. if (fieldAttr.DataType == "binary")
  64. {
  65. int byteth = fieldAttr.BitStart / ByteLength; // 第几个字节
  66. if (bytes.Length <= byteth)
  67. {
  68. LogHelper.log.Error(string.Format("代表{0}的字段长度出现错误", fieldAttr.Description));
  69. return null;
  70. }
  71. field.SetValue(dataModel, de_serializeField(fieldAttr.BitStart, fieldAttr.BitLength, bytes), null);
  72. }
  73. else
  74. {
  75. LogHelper.log.Error("数据类型不是binary");
  76. }
  77. }
  78. }
  79. return dataModel;
  80. }
  81. /// <summary>
  82. /// 解析位
  83. /// </summary>
  84. /// <param name="startBit"></param>
  85. /// <param name="lengthBit"></param>
  86. /// <param name="bytedata"></param>
  87. /// <returns></returns>
  88. private static string de_serializeField(int startBit, int lengthBit, byte[] bytedata)
  89. {
  90. int byteth = startBit / ByteLength; // 第几个字节
  91. int bytesLength = (startBit%ByteLength + lengthBit )/ ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
  92. byte[] data = new byte[bytesLength];
  93. byte[] resultByteData = new byte[bytesLength];
  94. Buffer.BlockCopy(bytedata, byteth, data, 0, bytesLength);
  95. BitOR(resultByteData, RightShift(data, startBit % ByteLength));
  96. return de_serialize_hex(resultByteData);
  97. }
  98. private static byte[] serializeField(int startBit, string value, int lengthBit)
  99. {
  100. int byteth = startBit / ByteLength; // 第几个字节
  101. int bytesLength = (startBit % ByteLength + lengthBit) / ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
  102. byte[] byteArray = serialize_hex(value, bytesLength);
  103. byte[] resultByteData = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00};
  104. if (byteth< 8)
  105. {
  106. resultByteData = BitOR(resultByteData, LeftShift (byteArray, startBit % ByteLength));
  107. }
  108. else
  109. {
  110. LogHelper.log.Error(string.Format("第{0}位赋值不成功", startBit));
  111. }
  112. return resultByteData;
  113. }
  114. public static byte[] serialize_hex(string value, int length)
  115. {
  116. byte[] returnBytes = new byte[length];
  117. bool ishexstring = false;
  118. if (string.IsNullOrEmpty(value))
  119. {
  120. return returnBytes;
  121. }
  122. ishexstring = true;
  123. value = value.Replace(" ", "");
  124. if ((value.Length % 2) != 0)
  125. {
  126. value = "0" + value;
  127. }
  128. for (int i = 0; i < value.Length / 2; i++)
  129. {
  130. if (ishexstring)
  131. {
  132. returnBytes[i] = Convert.ToByte(value.Substring(i * 2, 2), 16);
  133. }
  134. else
  135. {
  136. returnBytes[i] = Convert.ToByte(value.Substring(i, 1), 16);
  137. }
  138. }
  139. return returnBytes;
  140. }
  141. public static string de_serialize_hex(byte[] bsdata)
  142. {
  143. string returnStr = "";
  144. if (bsdata != null)
  145. {
  146. for (int i = 0; i < bsdata.Length; i++)
  147. {
  148. returnStr += bsdata[i].ToString("X2");
  149. }
  150. }
  151. return returnStr;
  152. }
  153. /// <summary>
  154. /// byte转化成bit
  155. /// </summary>
  156. /// <param name="b"></param>
  157. /// <returns></returns>
  158. private static int byteToBit(byte bytedata, int bitLocation)
  159. {
  160. return (int)((bytedata) >> bitLocation) & 0x1;
  161. }
  162. private static byte[] BitAnd(this byte[] ba1, byte[] ba2)
  163. {
  164. if (ba1.Length != ba2.Length)
  165. {
  166. return new byte[0];
  167. }
  168. var ba3 = new byte[ba1.Length];
  169. for (int i = 0; i < ba3.Length; i++)
  170. {
  171. ba3[i] = (byte)((byte)ba1[i] & (byte)ba2[i]);
  172. }
  173. return ba3;
  174. }
  175. private static byte[] BitOR(this byte[] ba1, byte[] ba2)
  176. {
  177. if (ba1.Length != ba2.Length)
  178. {
  179. return new byte[0];
  180. }
  181. var ba3 = new byte[ba1.Length];
  182. for (int i = 0; i < ba3.Length; i++)
  183. {
  184. ba3[i] = (byte)((byte)ba1[i] | (byte)ba2[i]);
  185. }
  186. return ba3;
  187. }
  188. private static byte[] RightShift(this byte[] ba, int n)
  189. {
  190. if (n < 0)
  191. {
  192. return LeftShift(ba, Math.Abs(n));
  193. }
  194. byte[] ba2 = null;
  195. ba2 = ba.Clone() as byte[];
  196. int loop = (int)Math.Ceiling(n / 8.0);
  197. byte tempByte = 0;
  198. byte tempByte2 = 0;
  199. byte Header = 0;
  200. for (int i = 0; i < loop; i++)
  201. {
  202. var tempN = i + 1 == loop ? n % 8 : 8;
  203. if (tempN == 0 && n != 0)
  204. {
  205. tempN = 8;
  206. }
  207. for (int j = 0; j < ba.Length; j++)
  208. {
  209. if (j == 0)
  210. {
  211. Header = (byte)((ba2.First() & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
  212. tempByte = (byte)((ba2[ba.Length - 1 - j] & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
  213. ba2[ba.Length - 1 - j] >>= tempN;
  214. }
  215. else
  216. {
  217. tempByte2 = (byte)((ba2[ba.Length - 1 - j] & ((byte)(Math.Pow(2, tempN) - 1))) << (8 - tempN));
  218. ba2[ba.Length - 1 - j] >>= tempN;
  219. ba2[ba.Length - 1 - j] |= tempByte;
  220. tempByte = tempByte2;
  221. if (j + 1 == ba.Length)
  222. {
  223. ba2[j] |= Header;
  224. }
  225. }
  226. }
  227. }
  228. return ba2;
  229. }
  230. private static byte[] LeftShift(this byte[] ba, int n)
  231. {
  232. if (n < 0)
  233. {
  234. return RightShift(ba, Math.Abs(n));
  235. }
  236. byte[] ba2 = null;
  237. ba2 = ba.Clone() as byte[];
  238. int loop = (int)Math.Ceiling(n / 8.0);
  239. byte tempByte = 0;
  240. byte tempByte2 = 0;
  241. byte Header = 0;
  242. for (int i = 0; i < loop; i++)
  243. {
  244. var tempN = i + 1 == loop ? n % 8 : 8;
  245. if (tempN == 0 && n != 0)
  246. {
  247. tempN = 8;
  248. }
  249. for (int j = 0; j < ba.Length; j++)
  250. {
  251. if (j == 0)
  252. {
  253. Header = (byte)(ba2.Last() & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
  254. tempByte = (byte)(ba2[j] & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
  255. ba2[j] <<= tempN;
  256. }
  257. else
  258. {
  259. tempByte2 = (byte)(ba2[j] & ((byte)(Math.Pow(2, tempN) - 1) << (8 - tempN)));
  260. ba2[j] <<= tempN;
  261. ba2[j] |= (byte)(tempByte >> (8 - tempN));
  262. tempByte = tempByte2;
  263. if (j + 1 == ba.Length)
  264. {
  265. ba2[0] |= (byte)(Header >> (8 - tempN));
  266. }
  267. }
  268. }
  269. }
  270. return ba2;
  271. }
  272. }
  273. }