using System; using System.IO; using System.Linq; using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.diagnosticLed; using TBF.Rig.TestMethods.iPerlCommunication.communication.C4.protocolCommons; namespace TBF.Rig.TestMethods.iPerlCommunication.iPerlHead { internal class SimulationServices { const int Q2CorrFactorsAddr = 0x1878; internal static int ReadRequest(TestMethodCfg cfg, IperlHead iperlHead, MessageID messageID, int offset, int length, out byte[] buffer) { string pcbStr = iperlHead.RfidComPortNr.ToString().PadRight(10,'0') + iperlHead.Position.ToString("D2"); long decVal = Convert.ToInt64(pcbStr); string nHexStr = decVal.ToString("X4"); ConfigStruct configStruct = new ConfigStruct(); configStruct.PCBNumberString = nHexStr; configStruct.StatusMode = ProtocolStatuses.Active; configStruct.OpthoStatusMode = DiagnosticLedState.State4; configStruct.Version = "Good Version: 123456"; byte[] configurationBuffer; using (var ms = new MemoryStream()) using (var writer = new BinaryWriter(ms)) { configStruct.WriteBinary(writer); writer.Flush(); configurationBuffer = ms.ToArray(); // ← this is the binary output } byte[] calibrationBuffer = new byte[CalibrationStructV4.Length] { 3, 0, 150, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0, 150, 10 }; buffer = new byte[length]; return 0;//switch off if (messageID == MessageID.Configuration) { Array.Copy(GetPCB(iperlHead), 0, configurationBuffer, 16, 5); // set PCB Number according to iPerlHead configuration Array.Copy(configurationBuffer,offset,buffer,0,length); } else if (messageID == MessageID.Calibration) { Array.Copy(calibrationBuffer, offset, buffer, 0, length); } else if ((messageID == MessageID.MetrologyMemory) && (offset == Q2CorrFactorsAddr) && (length == 2)) { buffer = new byte[2] { 0, 0 }; } else { buffer = new byte[length]; } return iperlHead.Name.Equals("iPerl13") ? 2 : 0; /// Simulates an error on position 13 } internal static int WriteRequest(TestMethodCfg cfg, IperlHead iperlHead, MessageID messageID, int offset, int length, byte[] buffer) { return 0; } private static Array GetPCB(IperlHead iperlHead) { string pcbStr = iperlHead.RfidComPortNr.ToString().PadRight(10,'0') + iperlHead.Position.ToString("D2"); long decVal = Convert.ToInt64(pcbStr); string nHexStr = decVal.ToString("X4"); string hexStr = ""; for (int a = nHexStr.Length; a >= 1; a = a - 2) { hexStr = hexStr + nHexStr.Substring(a - 2, 2); } return Enumerable.Range(0, hexStr.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hexStr.Substring(x, 2), 16)) .ToArray(); } } }