using Aitex.Core.Utilities; using MECF.Framework.Common.Device.Festo; using MECF.Framework.Common.Device.PowerSupplier; using MECF.Framework.Common.Net; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Device.Galil { public class GalilMessage : INetMessage { #region 属性 public int ProtocolHeadBytesLength { get; set; } = 1; public int ErrorCode { get; set; } public string ErrorMsg { get; set; } public byte[] HeadBytes { get; set; } public byte[] ContentBytes { get; set; } public byte[] SendBytes { get; set; } #endregion #region 内部变量 IByteTransform byteTransform = new SmallEndianByteTransformBase(); #endregion public bool CheckDataLegal() { return true; } public bool CheckHeadBytesLegal() { return true; } public bool ConfirmResponseResult() { return HeadBytes[0] == 0x3A; } public GalilCommand Decode() { GalilCommand command = new GalilCommand(); command.ControllerData = new GalilControllerData(); int offset = 0; command.ControllerData.Sample = byteTransform.TransUInt16(ContentBytes, 0); offset += 2; int inputLength = 10; command.ControllerData.Inputs = new byte[inputLength]; Array.Copy(ContentBytes,2,command.ControllerData.Inputs, 0, inputLength); offset += inputLength; int outputLength = 10; command.ControllerData.Outputs = new byte[outputLength]; Array.Copy(ContentBytes,offset,command.ControllerData.Outputs,0, outputLength); offset += outputLength; command.ControllerData.ErrorCode = ContentBytes[offset]; offset += 1; command.ControllerData.Status = ContentBytes[offset]; offset += 1; int sBlockLength = 8; command.ControllerData.SBlocks = new byte[sBlockLength]; Array.Copy(ContentBytes, offset, command.ControllerData.SBlocks, 0, sBlockLength); offset += sBlockLength; command.ControllerData.TBlocks = new byte[sBlockLength]; Array.Copy(ContentBytes,offset, command.ControllerData.TBlocks, 0, sBlockLength); offset += sBlockLength; int axisLength = 28; command.ControllerData.GalilAxisDatas = new List(); for(int i = offset; i < ContentBytes.Length; i += axisLength) { if(ContentBytes.Length-i < axisLength) { break; } byte[] axisByt=new byte[axisLength]; Array.Copy(ContentBytes,i,axisByt,0,axisLength); GalilAxisData galilAxisData = AnalyseAxisData(axisByt); command.ControllerData.GalilAxisDatas.Add(galilAxisData); } return command; } private GalilAxisData AnalyseAxisData(byte[] data) { int offset = 0; GalilAxisData axisData = new GalilAxisData(); axisData.IsSwitchOn = (data[0] & 0x01)==0x00; axisData.Status=byteTransform.TransUInt16(data,offset); offset += 2; axisData.Switches = data[offset]; axisData.ForwardLimit = ((axisData.Switches >> 3) & 0x01) == 0x01; axisData.ReverseLimit = ((axisData.Switches >> 2) & 0x01) == 0x01; offset += 1; axisData.StopCode = data[offset]; offset += 1; axisData.ReferencePosition=byteTransform.TransInt32(data,offset); offset += 4; axisData.MotorPosition = byteTransform.TransInt32(data, offset); offset += 4; axisData.PositionError=byteTransform.TransInt32(data,offset); offset += 4; axisData.AuxiliaryPosition=byteTransform.TransInt32(data,offset); offset += 4; axisData.Velocity=byteTransform.TransInt32(data,offset); offset += 4; axisData.Torque = byteTransform.TransInt16(data, offset); offset += 2; axisData.Res=byteTransform.TransInt16(data,offset); return axisData; } public int GetContentLengthByHeadBytes() { if (HeadBytes.Length > 1) { return byteTransform.TransInt16(HeadBytes, 2)-ProtocolHeadBytesLength+1; } else { return 0; } } public int GetHeadBytesIdentity() { return byteTransform.TransInt16(HeadBytes, 0); } public byte[] Code(GalilCommand data) { if (data.CommandCode == 3) { ProtocolHeadBytesLength = 4; } else { ProtocolHeadBytesLength = 1; } return ASCIIEncoding.ASCII.GetBytes(data.SetData); } public void SetProtocolHeadBytesLength() { } } }