FestoMessage.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Aitex.Core.Utilities;
  2. using MECF.Framework.Common.Device.PowerSupplier;
  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.Text;
  9. using System.Threading.Tasks;
  10. namespace MECF.Framework.Common.Device.Festo
  11. {
  12. public class FestoMessage : INetMessage<FestoCommand>
  13. {
  14. #region 属性
  15. public int ProtocolHeadBytesLength { get; set; } = 6;
  16. public int ErrorCode { get; set; }
  17. public string ErrorMsg { get; set; }
  18. public byte[] HeadBytes { get; set; }
  19. public byte[] ContentBytes { get; set; }
  20. public byte[] SendBytes { get; set; }
  21. #endregion
  22. #region 内部变量
  23. IByteTransform byteTransform = new BigEndianByteTransformBase();
  24. #endregion
  25. public bool CheckDataLegal()
  26. {
  27. //第二个字节高位为0x80,设备报错
  28. if ((ContentBytes[1] & 0x80) == 0x80)
  29. {
  30. ErrorCode = (int)NetErrorCode.DeviceError;
  31. ErrorMsg = GetDeviceMessage(ContentBytes[2]);
  32. return false;
  33. }
  34. //数据长度不一致
  35. if (ContentBytes[1] == 0x03)
  36. {
  37. byte contentLength = ContentBytes[2];
  38. if (ContentBytes.Length - 3 != contentLength)
  39. {
  40. ErrorCode = (int)NetErrorCode.InvalidData;
  41. ErrorMsg = EnumUtil.GetEnumDescription(NetErrorCode.InvalidData);
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. /// <summary>
  48. /// 设备错误信息
  49. /// </summary>
  50. /// <returns></returns>
  51. private string GetDeviceMessage(byte deviceErrorCode)
  52. {
  53. switch (deviceErrorCode)
  54. {
  55. case 0x01:
  56. return "Invalid Function";
  57. case 0x02:
  58. return "Invalid Address";
  59. case 0x03:
  60. return "Invalid Data";
  61. case 0x04:
  62. return "Slave Device Error";
  63. case 0x05:
  64. return "Error Confirm";
  65. case 0x06:
  66. return "Slave Device Busy";
  67. default:
  68. return "Unkown Error Code";
  69. }
  70. }
  71. public bool CheckHeadBytesLegal()
  72. {
  73. //地址与指令同时一致
  74. return SendBytes[0] == HeadBytes[0] && SendBytes[1] == HeadBytes[1] && HeadBytes[2] == 0x00 && HeadBytes[3] == 0x00;
  75. }
  76. public bool ConfirmResponseResult()
  77. {
  78. //指令码与发送指令码一致
  79. if (HeadBytes[1] == SendBytes[1])
  80. {
  81. return true;
  82. }
  83. return false;
  84. }
  85. public FestoCommand Decode()
  86. {
  87. FestoCommand command = new FestoCommand();
  88. if (ContentBytes[1] == 0x03)
  89. {
  90. command.Datas = new byte[ContentBytes.Length - 3];
  91. Array.Copy(ContentBytes,3,command.Datas, 0, command.Datas.Length);
  92. }
  93. return command;
  94. }
  95. public int GetContentLengthByHeadBytes()
  96. {
  97. return byteTransform.TransInt16(HeadBytes, 4);
  98. }
  99. public int GetHeadBytesIdentity()
  100. {
  101. return byteTransform.TransInt16(HeadBytes, 0);
  102. }
  103. public byte[] Code(FestoCommand data)
  104. {
  105. byte[] byt = null;
  106. //读取指令
  107. if (data.CommandCode == 0x03)
  108. {
  109. byt = new byte[12];
  110. //事务处理标识
  111. Array.Copy(FestoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  112. //固定码0x0000
  113. byt[2] = 0x00;
  114. byt[3] = 0x00;
  115. //数据长度
  116. byt[4] = 0x00;
  117. byt[5] = 0x06;
  118. //通道
  119. byt[6] = data.Channel;
  120. //指令
  121. byt[7] = data.CommandCode;
  122. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  123. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 10, 2);
  124. }
  125. //写单个寄存器地址
  126. else if (data.CommandCode == 0x06)
  127. {
  128. byt = new byte[12];
  129. //事务处理标识
  130. Array.Copy(FestoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  131. //固定码0x0000
  132. byt[2] = 0x00;
  133. byt[3] = 0x00;
  134. //数据长度
  135. byt[4] = 0x00;
  136. byt[5] = 0x06;
  137. //通道
  138. byt[6] = data.Channel;
  139. //指令
  140. byt[7] = data.CommandCode;
  141. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  142. byt[10] = 0x00;
  143. byt[11] = data.Datas[0];
  144. }
  145. else if (data.CommandCode == 0x10)
  146. {
  147. byt = new byte[6 + 7 + 2 * data.Datas.Length];
  148. //事务处理标识
  149. Array.Copy(FestoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  150. //固定码0x0000
  151. byt[2] = 0x00;
  152. byt[3] = 0x00;
  153. //数据长度
  154. byt[4] = 0x00;
  155. byt[5] = 0x06;
  156. //通道
  157. byt[6] = data.Channel;
  158. //指令
  159. byt[7] = data.CommandCode;
  160. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  161. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 10, 2);
  162. byt[12] = (byte)(2 * data.Datas.Length);
  163. for (int i = 0; i < data.Datas.Length; i++)
  164. {
  165. byt[13 + i] = 0x00;
  166. byt[14+i]=data.Datas[i];
  167. }
  168. }
  169. return byt;
  170. }
  171. public void SetProtocolHeadBytesLength()
  172. {
  173. }
  174. }
  175. }