Galil40Message.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Aitex.Core.Utilities;
  2. using MECF.Framework.Common.Device.Festo;
  3. using MECF.Framework.Common.Device.PowerSupplier;
  4. using MECF.Framework.Common.Net;
  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.Galil
  11. {
  12. public class Galil40Message : INetMessage<GalilCommand>
  13. {
  14. #region 属性
  15. public int ProtocolHeadBytesLength { get; set; } = 1;
  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 SmallEndianByteTransformBase();
  24. #endregion
  25. public bool CheckDataLegal()
  26. {
  27. return true;
  28. }
  29. public bool CheckHeadBytesLegal()
  30. {
  31. return true;
  32. }
  33. public bool ConfirmResponseResult()
  34. {
  35. return HeadBytes[0] == 0x3A;
  36. }
  37. public GalilCommand Decode()
  38. {
  39. GalilCommand command = new GalilCommand();
  40. command.ControllerData = new GalilControllerData();
  41. int offset = 0;
  42. command.ControllerData.Sample = byteTransform.TransUInt16(ContentBytes, 0);
  43. offset += 2;
  44. int inputLength = 10;
  45. command.ControllerData.Inputs = new byte[inputLength];
  46. Array.Copy(ContentBytes,2,command.ControllerData.Inputs, 0, inputLength);
  47. offset += inputLength;
  48. int outputLength = 10;
  49. command.ControllerData.Outputs = new byte[outputLength];
  50. Array.Copy(ContentBytes,offset,command.ControllerData.Outputs,0, outputLength);
  51. offset += outputLength;
  52. byte[] reservers = new byte[16];
  53. Array.Copy(ContentBytes,offset,reservers,0,reservers.Length);
  54. offset += reservers.Length;
  55. byte[] ethernet = new byte[8];
  56. Array.Copy(ContentBytes, offset,ethernet,0,ethernet.Length);
  57. offset += ethernet.Length;
  58. command.ControllerData.ErrorCode = ContentBytes[offset];
  59. offset += 1;
  60. command.ControllerData.Status = ContentBytes[offset];
  61. offset += 1;
  62. byte[] ampliferStatus = new byte[4];
  63. Array.Copy(ContentBytes,offset,ampliferStatus,0,ampliferStatus.Length);
  64. offset += ampliferStatus.Length;
  65. byte[] counterModel=new byte[6];
  66. Array.Copy(ContentBytes,offset,counterModel,0,counterModel.Length);
  67. offset += counterModel.Length;
  68. int sBlockLength =10;
  69. command.ControllerData.SBlocks = new byte[sBlockLength];
  70. Array.Copy(ContentBytes, offset, command.ControllerData.SBlocks, 0, sBlockLength);
  71. offset += sBlockLength;
  72. command.ControllerData.TBlocks = new byte[sBlockLength];
  73. Array.Copy(ContentBytes,offset, command.ControllerData.TBlocks, 0, sBlockLength);
  74. offset += sBlockLength;
  75. int axisLength = 36;
  76. command.ControllerData.GalilAxisDatas = new List<GalilAxisData>();
  77. for(int i = offset; i < ContentBytes.Length; i += axisLength)
  78. {
  79. if(ContentBytes.Length-i < axisLength)
  80. {
  81. break;
  82. }
  83. byte[] axisByt=new byte[axisLength];
  84. Array.Copy(ContentBytes,i,axisByt,0,axisLength);
  85. GalilAxisData galilAxisData = AnalyseAxisData(axisByt);
  86. command.ControllerData.GalilAxisDatas.Add(galilAxisData);
  87. }
  88. return command;
  89. }
  90. private GalilAxisData AnalyseAxisData(byte[] data)
  91. {
  92. int offset = 0;
  93. GalilAxisData axisData = new GalilAxisData();
  94. axisData.IsSwitchOn = (data[0] & 0x01)==0x00;
  95. axisData.Status=byteTransform.TransUInt16(data,offset);
  96. offset += 2;
  97. axisData.Switches = data[offset];
  98. axisData.ForwardLimit = ((axisData.Switches >> 3) & 0x01) == 0x01;
  99. axisData.ReverseLimit = ((axisData.Switches >> 2) & 0x01) == 0x01;
  100. offset += 1;
  101. axisData.StopCode = data[offset];
  102. offset += 1;
  103. axisData.ReferencePosition=byteTransform.TransInt32(data,offset);
  104. offset += 4;
  105. axisData.MotorPosition = byteTransform.TransInt32(data, offset);
  106. offset += 4;
  107. axisData.PositionError=byteTransform.TransInt32(data,offset);
  108. offset += 4;
  109. axisData.AuxiliaryPosition=byteTransform.TransInt32(data,offset);
  110. offset += 4;
  111. axisData.Velocity=byteTransform.TransInt32(data,offset);
  112. offset += 4;
  113. axisData.Torque = byteTransform.TransInt32(data, offset);
  114. offset += 4;
  115. axisData.Res=byteTransform.TransInt16(data,offset);
  116. byte hallInput=data[offset];
  117. offset += 1;
  118. byte reserved=data[offset];
  119. offset += 1;
  120. byte[] userVariables = new byte[4];
  121. Array.Copy(data,offset,userVariables,0,userVariables.Length);
  122. offset += userVariables.Length;
  123. return axisData;
  124. }
  125. public int GetContentLengthByHeadBytes()
  126. {
  127. if (HeadBytes.Length > 1)
  128. {
  129. return byteTransform.TransInt16(HeadBytes, 2)-ProtocolHeadBytesLength+1;
  130. }
  131. else
  132. {
  133. return 0;
  134. }
  135. }
  136. public int GetHeadBytesIdentity()
  137. {
  138. return byteTransform.TransInt16(HeadBytes, 0);
  139. }
  140. public byte[] Code(GalilCommand data)
  141. {
  142. if (data.CommandCode == 3)
  143. {
  144. ProtocolHeadBytesLength = 4;
  145. }
  146. else
  147. {
  148. ProtocolHeadBytesLength = 1;
  149. }
  150. return ASCIIEncoding.ASCII.GetBytes(data.SetData);
  151. }
  152. public void SetProtocolHeadBytesLength()
  153. {
  154. }
  155. }
  156. }