PowerSupplierTcpModbusMessage.cs 6.4 KB

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