WagoMessage.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Wago
  11. {
  12. public class WagoMessage : INetMessage<WagoCommand>
  13. {
  14. #region 常量
  15. private const int DI_READ_CMD = 1;
  16. private const int DO_READ_CMD = 2;
  17. private const int AI_READ_CMD = 3;
  18. private const int AO_READ_CMD = 4;
  19. #endregion
  20. #region 属性
  21. public int ProtocolHeadBytesLength { get; set; } = 8;
  22. public int ErrorCode { get; set; }
  23. public string ErrorMsg { get; set; }
  24. public byte[] HeadBytes { get; set; }
  25. public byte[] ContentBytes { get; set; }
  26. public byte[] SendBytes { get; set; }
  27. #endregion
  28. #region 内部变量
  29. IByteTransform byteTransform = new BigEndianByteTransformBase();
  30. #endregion
  31. public bool CheckDataLegal()
  32. {
  33. //第二个字节高位为0x80,设备报错
  34. if ((HeadBytes[HeadBytes.Length-1] & 0x80) == 0x80)
  35. {
  36. ErrorCode = (int)NetErrorCode.DeviceError;
  37. ErrorMsg = GetDeviceMessage(ContentBytes[0]);
  38. return false;
  39. }
  40. return true;
  41. }
  42. /// <summary>
  43. /// 设备错误信息
  44. /// </summary>
  45. /// <returns></returns>
  46. private string GetDeviceMessage(byte deviceErrorCode)
  47. {
  48. switch (deviceErrorCode)
  49. {
  50. case 0x01:
  51. return "Invalid Function";
  52. case 0x02:
  53. return "Invalid Address";
  54. case 0x03:
  55. return "Invalid Data";
  56. case 0x04:
  57. return "Slave Device Error";
  58. case 0x05:
  59. return "Error Confirm";
  60. case 0x06:
  61. return "Slave Device Busy";
  62. default:
  63. return "Unkown Error Code";
  64. }
  65. }
  66. public bool CheckHeadBytesLegal()
  67. {
  68. //地址与指令同时一致
  69. return SendBytes[0] == HeadBytes[0] && SendBytes[1] == HeadBytes[1] && HeadBytes[2] == 0x00 && HeadBytes[3] == 0x00;
  70. }
  71. public bool ConfirmResponseResult()
  72. {
  73. //指令码与发送指令码一致
  74. if (HeadBytes[HeadBytes.Length-1] == SendBytes[HeadBytes.Length-1])
  75. {
  76. return true;
  77. }
  78. return false;
  79. }
  80. public WagoCommand Decode()
  81. {
  82. WagoCommand command = new WagoCommand();
  83. //DI或DO
  84. if (command.CommandCode==DI_READ_CMD||command.CommandCode==DO_READ_CMD)
  85. {
  86. byte bytLength = ContentBytes[0];
  87. //byte长度*8
  88. bool[] datas = new bool[bytLength*8];
  89. byte[] bytes = new byte[bytLength];
  90. Array.Copy(ContentBytes,1,bytes, 0, bytes.Length);
  91. for (int i = 0; i < bytes.Length; i++)
  92. {
  93. for (int j = 0; j < 8; j++)
  94. {
  95. if (j == 0)
  96. {
  97. datas[i + j] = (bytes[i] * 0x01 == 0x01);
  98. }
  99. else
  100. {
  101. datas[i + j] = ((bytes[i] >> j) * 0x01 == 0x01);
  102. }
  103. }
  104. }
  105. command.Datas = datas;
  106. }
  107. //DI或DO
  108. else if (command.CommandCode == AI_READ_CMD || command.CommandCode == AO_READ_CMD)
  109. {
  110. byte bytLength = ContentBytes[0];
  111. byte[] bytes = new byte[bytLength];
  112. Array.Copy(ContentBytes, 1, bytes, 0, bytes.Length);
  113. command.Datas = bytes;
  114. }
  115. return command;
  116. }
  117. public int GetContentLengthByHeadBytes()
  118. {
  119. //第二个字节高位为0x80,设备报错
  120. if ((HeadBytes[HeadBytes.Length - 1] & 0x80) == 0x80)
  121. {
  122. return 1;
  123. }
  124. else
  125. {
  126. //长度减去unit identifier+modbus code
  127. return byteTransform.TransInt16(HeadBytes, 4)-2;
  128. }
  129. }
  130. public int GetHeadBytesIdentity()
  131. {
  132. return byteTransform.TransInt16(HeadBytes, 0);
  133. }
  134. /// <summary>
  135. /// 编码
  136. /// </summary>
  137. /// <param name="data"></param>
  138. /// <returns></returns>
  139. public byte[] Code(WagoCommand data)
  140. {
  141. byte[] byt = null;
  142. //读取DI
  143. if (IsReadCommand(data.CommandCode))
  144. {
  145. byt = new byte[12];
  146. //事务处理标识
  147. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  148. //固定码0x0000
  149. byt[2] = 0x00;
  150. byt[3] = 0x00;
  151. //数据长度
  152. byt[4] = 0x00;
  153. byt[5] = 0x06;
  154. //通道
  155. byt[6] = data.Channel;
  156. //指令
  157. byt[7] = data.CommandCode;
  158. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  159. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 10, 2);
  160. }
  161. //写单个寄存器地址
  162. else if (data.CommandCode == 0x05)
  163. {
  164. byt = new byte[12];
  165. //事务处理标识
  166. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  167. //固定码0x0000
  168. byt[2] = 0x00;
  169. byt[3] = 0x00;
  170. //数据长度
  171. byt[4] = 0x00;
  172. byt[5] = 0x06;
  173. //通道
  174. byt[6] = data.Channel;
  175. //指令
  176. byt[7] = data.CommandCode;
  177. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  178. byt[10] = (byte)data.WriteValue;
  179. byt[11] = 0x00;
  180. }
  181. else if (data.CommandCode == 0x06)
  182. {
  183. byt = new byte[12];
  184. //事务处理标识
  185. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  186. //固定码0x0000
  187. byt[2] = 0x00;
  188. byt[3] = 0x00;
  189. //数据长度
  190. byt[4] = 0x00;
  191. byt[5] = 0x06;
  192. //通道
  193. byt[6] = data.Channel;
  194. //指令
  195. byt[7] = data.CommandCode;
  196. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  197. Array.Copy((byte[])data.WriteValue, 0, byt, 10, 2);
  198. }
  199. return byt;
  200. }
  201. /// <summary>
  202. /// 是否为读取指令
  203. /// </summary>
  204. /// <param name="command"></param>
  205. /// <returns></returns>
  206. private bool IsReadCommand(byte command)
  207. {
  208. switch (command)
  209. {
  210. case DI_READ_CMD:
  211. case DO_READ_CMD:
  212. case AI_READ_CMD:
  213. case AO_READ_CMD:
  214. return true;
  215. default:
  216. return false;
  217. }
  218. }
  219. public void SetProtocolHeadBytesLength()
  220. {
  221. }
  222. }
  223. }