public int SetResponse(byte[] responseBytes, McFrame mcFrame) { if (mcFrame.HasFlag(McFrame.ASCII_FLAG)) { return(SetResponse(ASCIIEncoding.ASCII.GetString(responseBytes))); } ResultCode = 0xcccc; int min = (FrameType.HasFlag(McFrame.MC3E)) ? 11 : 15; if (min <= responseBytes.Length) { var btCount = new[] { responseBytes[min - 4], responseBytes[min - 3] }; var btCode = new[] { responseBytes[min - 2], responseBytes[min - 1] }; int rsCount = BitConverter.ToUInt16(btCount, 0); if (FrameType.HasFlag(McFrame.ASCII_FLAG)) { rsCount = rsCount / 2; } ResultCode = BitConverter.ToUInt16(btCode, 0); Response = new byte[rsCount - 2]; Buffer.BlockCopy(responseBytes, min, Response, 0, Response.Length); } return(ResultCode); }
public bool IsIncorrectResponse(byte[] iResponse) { if (iResponse.Length == 0) { return(false); } var min = (FrameType.HasFlag(McFrame.MC3E)) ? 11 : 15; var btCount = new[] { iResponse[min - 4], iResponse[min - 3] }; var btCode = new[] { iResponse[min - 2], iResponse[min - 1] }; var rsCount = BitConverter.ToUInt16(btCount, 0) - 2; var rsCode = BitConverter.ToUInt16(btCode, 0); return(rsCode == 0 && rsCount != (iResponse.Length - min)); }
private List <FrameType> GetActiveFrames(int requestedFrames) { FrameType frameTypeFlags = (FrameType)requestedFrames; // Parse this integer for requested frames List <FrameType> activeFrames = new List <FrameType>(); // Determine which stream bits are active foreach (FrameType ft in Enum.GetValues(typeof(FrameType)).Cast <FrameType>()) { if (frameTypeFlags.HasFlag(ft)) { activeFrames.Add(ft); } } return(activeFrames); }
public byte[] SetCommand(uint iMainCommand, uint iSubCommand, byte[] iData) { var dataLength = (uint)(iData.Length + 6); var ret = new List <byte>(iData.Length + 20); uint frame = (FrameType.HasFlag(McFrame.MC3E)) ? 0x0050u : (FrameType.HasFlag(McFrame.MC4E)) ? 0x0054u : 0x0000u; ret.Add((byte)frame); ret.Add((byte)(frame >> 8)); if (FrameType.HasFlag(McFrame.MC4E)) { ret.Add((byte)(SerialNumber >> 8)); ret.Add((byte)SerialNumber); ret.Add(0x00); ret.Add(0x00); } ret.Add((byte)NetwrokNumber); ret.Add((byte)PcNumber); ret.Add((byte)IoNumber); ret.Add((byte)(IoNumber >> 8)); ret.Add((byte)ChannelNumber); ret.Add((byte)dataLength); ret.Add((byte)(dataLength >> 8)); ret.Add((byte)CpuTimer); ret.Add((byte)(CpuTimer >> 8)); ret.Add((byte)iMainCommand); ret.Add((byte)(iMainCommand >> 8)); ret.Add((byte)iSubCommand); ret.Add((byte)(iSubCommand >> 8)); ret.AddRange(iData); return(ret.ToArray()); }