PowerSupplierMessage.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using Aitex.Core.Utilities;
  2. using log4net.Core;
  3. using MECF.Framework.Common.Net;
  4. using MECF.Framework.Common.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.Remoting.Messaging;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace MECF.Framework.Common.Device.PowerSupplier
  13. {
  14. public class PowerSupplierMessage : INetMessage<PowerSupplierCommand>
  15. {
  16. #region 属性
  17. public int ProtocolHeadBytesLength { get; set; } = 3;
  18. public int ErrorCode { get; set; }
  19. public string ErrorMsg { get; set; }
  20. public byte[] HeadBytes { get; set; }
  21. public byte[] ContentBytes { get; set; }
  22. public byte[] SendBytes { get; set; }
  23. #endregion
  24. #region 内部变量
  25. IByteTransform byteTransform=new BigEndianByteTransformBase();
  26. #endregion
  27. public bool CheckDataLegal()
  28. {
  29. if (ProtocolHeadBytesLength > 1)
  30. {
  31. //第二个字节高位为0x80,设备报错
  32. if ((HeadBytes[1] & 0x80) == 0x80)
  33. {
  34. ErrorCode = (int)NetErrorCode.DeviceError;
  35. if (HeadBytes.Length > 2)
  36. {
  37. ErrorMsg = GetDeviceMessage(HeadBytes[2]);
  38. }
  39. else
  40. {
  41. ErrorMsg = GetDeviceMessage(ContentBytes[0]);
  42. }
  43. return false;
  44. }
  45. //数据长度不一致
  46. if (HeadBytes[1] == 0x03)
  47. {
  48. byte contentLength = HeadBytes[2];
  49. if (ContentBytes.Length - 2 != contentLength)
  50. {
  51. ErrorCode = (int)NetErrorCode.InvalidData;
  52. ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.InvalidData);
  53. return false;
  54. }
  55. }
  56. //CRC检验
  57. byte[] data = new byte[HeadBytes.Length + ContentBytes.Length - 2];
  58. Array.Copy(HeadBytes, 0, data, 0, HeadBytes.Length);
  59. Array.Copy(ContentBytes, 0, data, HeadBytes.Length, ContentBytes.Length - 2);
  60. byte[] crc16 = ModbusUtility.CalculateCrc(data);
  61. if (crc16.Length < 2 || (crc16[0] != ContentBytes[ContentBytes.Length - 2] || crc16[1] != ContentBytes[ContentBytes.Length - 1]))
  62. {
  63. ErrorCode = (int)NetErrorCode.CRCCheckError;
  64. ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.CRCCheckError);
  65. return false;
  66. }
  67. return true;
  68. }
  69. else
  70. {
  71. //第一个字节高位为0x80,设备报错
  72. if ((HeadBytes[0] & 0x80) == 0x90)
  73. {
  74. ErrorCode = (int)NetErrorCode.DeviceError;
  75. ErrorMsg = GetDeviceMessage(ContentBytes[0]);
  76. return false;
  77. }
  78. return true;
  79. }
  80. }
  81. /// <summary>
  82. /// 设备错误信息
  83. /// </summary>
  84. /// <returns></returns>
  85. private string GetDeviceMessage(byte deviceErrorCode)
  86. {
  87. switch(deviceErrorCode)
  88. {
  89. case 0x01:
  90. return "Invalid Function";
  91. case 0x02:
  92. return "Invalid Address";
  93. case 0x03:
  94. return "Invalid Data";
  95. case 0x04:
  96. return "Slave Device Error";
  97. case 0x05:
  98. return "Error Confirm";
  99. case 0x06:
  100. return "Slave Device Busy";
  101. default:
  102. return "Unkown Error Code";
  103. }
  104. }
  105. public bool CheckHeadBytesLegal()
  106. {
  107. //地址与指令同时一致
  108. return SendBytes[0] == HeadBytes[0] && (SendBytes[1] == HeadBytes[1]||HeadBytes[1]==(byte)(0x80|SendBytes[1]));
  109. }
  110. public bool ConfirmResponseResult()
  111. {
  112. //指令码与发送指令码一致
  113. if (HeadBytes[1] == SendBytes[1])
  114. {
  115. return true;
  116. }
  117. return false;
  118. }
  119. public PowerSupplierCommand Decode()
  120. {
  121. PowerSupplierCommand command = new PowerSupplierCommand();
  122. if (HeadBytes[1] == 0x03)
  123. {
  124. int registerCount = HeadBytes[2] / 2;
  125. command.Datas = byteTransform.TransUInt16(ContentBytes, 0, registerCount);
  126. }
  127. return command;
  128. }
  129. public int GetContentLengthByHeadBytes()
  130. {
  131. if (HeadBytes[1] == 0x03)
  132. {
  133. //2*寄存器+CRC 2个字节
  134. return HeadBytes[2] + 2;
  135. }
  136. else if (HeadBytes[1] == 0x06)
  137. {
  138. //寄存器地址2个字节+寄存器数值2个字节+CRC 2个字节
  139. return 6;
  140. }
  141. else if (HeadBytes[1] == 0x10)
  142. {
  143. //寄存器地址2个字节+寄存器数量2个字节+CRC 2个字节
  144. return 6;
  145. }
  146. //第二个字节高位为0x80,设备报错
  147. else if ((HeadBytes[1] & 0x80) == 0x80)
  148. {
  149. //仅CRC校验码
  150. if ((HeadBytes[1] & 0x0F) == 0x03)
  151. {
  152. return 2;
  153. }
  154. else
  155. {
  156. //异常码1个字节 CRC 2个字节
  157. return 3;
  158. }
  159. }
  160. else
  161. {
  162. return 0;
  163. }
  164. }
  165. public int GetHeadBytesIdentity()
  166. {
  167. return 0;
  168. }
  169. public byte[] Code(PowerSupplierCommand data)
  170. {
  171. byte[] byt = null;
  172. //读取指令
  173. if(data.CommandCode==0x03)
  174. {
  175. byt= new byte[8];
  176. byt[0] = data.Channel;
  177. byt[1] = data.CommandCode;
  178. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
  179. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 4, 2);
  180. //ushort crc16= Crc16.CRC16_Modbus(byt);
  181. byte[] crcSourceByt = new byte[byt.Length - 2];
  182. Array.Copy(byt,0,crcSourceByt,0,byt.Length-2);
  183. byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
  184. Array.Copy(crc, 0, byt, 6, 2);
  185. }
  186. //写单个寄存器地址
  187. else if(data.CommandCode==0x06)
  188. {
  189. byt = new byte[8];
  190. byt[0] = data.Channel;
  191. byt[1] = data.CommandCode;
  192. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
  193. Array.Copy(byteTransform.GetBytes(data.Datas[0]), 0, byt, 4, 2);
  194. byte[] crcSourceByt = new byte[byt.Length - 2];
  195. Array.Copy(byt, 0, crcSourceByt, 0, byt.Length - 2);
  196. byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
  197. Array.Copy(crc, 0, byt, 6, 2);
  198. }
  199. else if(data.CommandCode==0x10)
  200. {
  201. byt = new byte[2 * data.Datas.Length + 9];
  202. byt[0] = data.Channel;
  203. byt[1] = data.CommandCode;
  204. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 2, 2);
  205. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 4, 2);
  206. byt[6] = (byte)(2 * data.Datas.Length);
  207. for (int i = 0; i < data.Datas.Length; i++)
  208. {
  209. Array.Copy(byteTransform.GetBytes(data.Datas[i]), 0, byt, 7 + i * 2, 2);
  210. }
  211. byte[] crcSourceByt = new byte[byt.Length - 2];
  212. Array.Copy(byt, 0, crcSourceByt, 0, byt.Length - 2);
  213. byte[] crc = ModbusUtility.CalculateCrc(crcSourceByt);
  214. Array.Copy(crc, 0, byt, byt.Length - 2, 2);
  215. //byt = new byte[2 * data.Datas.Length + 6];
  216. //byt[0] = data.CommandCode;
  217. //Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 1, 2);
  218. //Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 3, 2);
  219. //byt[5] = (byte)(2 * data.Datas.Length);
  220. //for (int i = 0; i < data.Datas.Length; i++)
  221. //{
  222. // Array.Copy(byteTransform.GetBytes(data.Datas[i]), 0, byt, 6 + i * 2, 2);
  223. //}
  224. }
  225. return byt;
  226. }
  227. public void SetProtocolHeadBytesLength()
  228. {
  229. if (SendBytes[1] == 0x03)
  230. {
  231. ProtocolHeadBytesLength = 3;
  232. }
  233. else if (SendBytes[1] == 0x06)
  234. {
  235. ProtocolHeadBytesLength = 2;
  236. }
  237. else if (SendBytes[1] == 0x10)
  238. {
  239. ProtocolHeadBytesLength = 2;
  240. }
  241. else
  242. {
  243. ProtocolHeadBytesLength = 0;
  244. }
  245. }
  246. }
  247. }