WagoMessage.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. byte commandCode = HeadBytes[HeadBytes.Length - 1];
  84. //DI或DO
  85. if (commandCode==DI_READ_CMD||commandCode==DO_READ_CMD)
  86. {
  87. byte bytLength = ContentBytes[0];
  88. //byte长度*8
  89. bool[] datas = new bool[bytLength*8];
  90. byte[] bytes = new byte[bytLength];
  91. Array.Copy(ContentBytes,1,bytes, 0, bytes.Length);
  92. for (int i = 0; i < bytes.Length; i++)
  93. {
  94. for (int j = 0; j < 8; j++)
  95. {
  96. if (j == 0)
  97. {
  98. datas[i*8 + j] = ((bytes[i] & 0x01) == 0x01);
  99. }
  100. else
  101. {
  102. datas[i*8 + j] = (((bytes[i] >> j) & 0x01) == 0x01);
  103. }
  104. }
  105. }
  106. command.Datas = datas;
  107. }
  108. //DI或DO
  109. else if (commandCode == AI_READ_CMD || commandCode == AO_READ_CMD)
  110. {
  111. byte bytLength = ContentBytes[0];
  112. byte[] bytes = new byte[bytLength];
  113. Array.Copy(ContentBytes, 1, bytes, 0, bytes.Length);
  114. command.Datas = bytes;
  115. }
  116. return command;
  117. }
  118. public int GetContentLengthByHeadBytes()
  119. {
  120. //第二个字节高位为0x80,设备报错
  121. if ((HeadBytes[HeadBytes.Length - 1] & 0x80) == 0x80)
  122. {
  123. return 1;
  124. }
  125. else
  126. {
  127. //长度减去unit identifier+modbus code
  128. return byteTransform.TransInt16(HeadBytes, 4)-2;
  129. }
  130. }
  131. public int GetHeadBytesIdentity()
  132. {
  133. return byteTransform.TransInt16(HeadBytes, 0);
  134. }
  135. /// <summary>
  136. /// 编码
  137. /// </summary>
  138. /// <param name="data"></param>
  139. /// <returns></returns>
  140. public byte[] Code(WagoCommand data)
  141. {
  142. byte[] byt = null;
  143. //读取DI
  144. if (IsReadCommand(data.CommandCode))
  145. {
  146. byt = new byte[12];
  147. //事务处理标识
  148. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  149. //固定码0x0000
  150. byt[2] = 0x00;
  151. byt[3] = 0x00;
  152. //数据长度
  153. byt[4] = 0x00;
  154. byt[5] = 0x06;
  155. //通道
  156. byt[6] = data.Channel;
  157. //指令
  158. byt[7] = data.CommandCode;
  159. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  160. Array.Copy(byteTransform.GetBytes(data.RegisterCount), 0, byt, 10, 2);
  161. }
  162. //写单个寄存器地址
  163. else if (data.CommandCode == 0x05)
  164. {
  165. byt = new byte[12];
  166. //事务处理标识
  167. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  168. //固定码0x0000
  169. byt[2] = 0x00;
  170. byt[3] = 0x00;
  171. //数据长度
  172. byt[4] = 0x00;
  173. byt[5] = 0x06;
  174. //通道
  175. byt[6] = data.Channel;
  176. //指令
  177. byt[7] = data.CommandCode;
  178. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  179. byt[10] = (byte)data.WriteValue;
  180. byt[11] = 0x00;
  181. }
  182. else if (data.CommandCode == 0x06)
  183. {
  184. byt = new byte[12];
  185. //事务处理标识
  186. Array.Copy(WagoFlag.Instance.GenerateDataFlagBuffer(byteTransform), 0, byt, 0, 2);
  187. //固定码0x0000
  188. byt[2] = 0x00;
  189. byt[3] = 0x00;
  190. //数据长度
  191. byt[4] = 0x00;
  192. byt[5] = 0x06;
  193. //通道
  194. byt[6] = data.Channel;
  195. //指令
  196. byt[7] = data.CommandCode;
  197. Array.Copy(byteTransform.GetBytes(data.Address), 0, byt, 8, 2);
  198. Array.Copy((byte[])data.WriteValue, 0, byt, 10, 2);
  199. }
  200. return byt;
  201. }
  202. /// <summary>
  203. /// 是否为读取指令
  204. /// </summary>
  205. /// <param name="command"></param>
  206. /// <returns></returns>
  207. private bool IsReadCommand(byte command)
  208. {
  209. switch (command)
  210. {
  211. case DI_READ_CMD:
  212. case DO_READ_CMD:
  213. case AI_READ_CMD:
  214. case AO_READ_CMD:
  215. return true;
  216. default:
  217. return false;
  218. }
  219. }
  220. public void SetProtocolHeadBytesLength()
  221. {
  222. }
  223. }
  224. }