GalilMessage.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 GalilMessage : 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. command.ControllerData.ErrorCode = ContentBytes[offset];
  53. offset += 1;
  54. command.ControllerData.Status = ContentBytes[offset];
  55. offset += 1;
  56. int sBlockLength = 8;
  57. command.ControllerData.SBlocks = new byte[sBlockLength];
  58. Array.Copy(ContentBytes, offset, command.ControllerData.SBlocks, 0, sBlockLength);
  59. offset += sBlockLength;
  60. command.ControllerData.TBlocks = new byte[sBlockLength];
  61. Array.Copy(ContentBytes,offset, command.ControllerData.TBlocks, 0, sBlockLength);
  62. offset += sBlockLength;
  63. int axisLength = 28;
  64. command.ControllerData.GalilAxisDatas = new List<GalilAxisData>();
  65. for(int i = offset; i < ContentBytes.Length; i += axisLength)
  66. {
  67. if(ContentBytes.Length-i < axisLength)
  68. {
  69. break;
  70. }
  71. byte[] axisByt=new byte[axisLength];
  72. Array.Copy(ContentBytes,i,axisByt,0,axisLength);
  73. GalilAxisData galilAxisData = AnalyseAxisData(axisByt);
  74. command.ControllerData.GalilAxisDatas.Add(galilAxisData);
  75. }
  76. return command;
  77. }
  78. private GalilAxisData AnalyseAxisData(byte[] data)
  79. {
  80. int offset = 0;
  81. GalilAxisData axisData = new GalilAxisData();
  82. axisData.IsSwitchOn = (data[0] & 0x01)==0x00;
  83. axisData.Status=byteTransform.TransUInt16(data,offset);
  84. offset += 2;
  85. axisData.Switches = data[offset];
  86. axisData.ForwardLimit = ((axisData.Switches >> 3) & 0x01) == 0x01;
  87. axisData.ReverseLimit = ((axisData.Switches >> 2) & 0x01) == 0x01;
  88. offset += 1;
  89. axisData.StopCode = data[offset];
  90. offset += 1;
  91. axisData.ReferencePosition=byteTransform.TransInt32(data,offset);
  92. offset += 4;
  93. axisData.MotorPosition = byteTransform.TransInt32(data, offset);
  94. offset += 4;
  95. axisData.PositionError=byteTransform.TransInt32(data,offset);
  96. offset += 4;
  97. axisData.AuxiliaryPosition=byteTransform.TransInt32(data,offset);
  98. offset += 4;
  99. axisData.Velocity=byteTransform.TransInt32(data,offset);
  100. offset += 4;
  101. axisData.Torque = byteTransform.TransInt16(data, offset);
  102. offset += 2;
  103. axisData.Res=byteTransform.TransInt16(data,offset);
  104. return axisData;
  105. }
  106. public int GetContentLengthByHeadBytes()
  107. {
  108. if (HeadBytes.Length > 1)
  109. {
  110. return byteTransform.TransInt16(HeadBytes, 2)-ProtocolHeadBytesLength+1;
  111. }
  112. else
  113. {
  114. return 0;
  115. }
  116. }
  117. public int GetHeadBytesIdentity()
  118. {
  119. return byteTransform.TransInt16(HeadBytes, 0);
  120. }
  121. public byte[] Code(GalilCommand data)
  122. {
  123. if (data.CommandCode == 3)
  124. {
  125. ProtocolHeadBytesLength = 4;
  126. }
  127. else
  128. {
  129. ProtocolHeadBytesLength = 1;
  130. }
  131. return ASCIIEncoding.ASCII.GetBytes(data.SetData);
  132. }
  133. public void SetProtocolHeadBytesLength()
  134. {
  135. }
  136. }
  137. }