using System; using System.Collections.Generic; using System.Text; using System.IO; namespace csRepeat.FW6 { public abstract class FW6Device : DPDevice { protected abstract void Connect(); protected abstract void Disconnect(); /// /// Send a packet and get the response /// /// /// /// protected AckResponse SendAndWaitForAck(FW6Packet packetToSend, int timeoutMS) { int delayBeforeReadMS = 0; return SendAndWaitForAck(packetToSend, delayBeforeReadMS, timeoutMS); } /// /// Send a packet and get the response /// /// /// /// protected AckResponse SendAndWaitForAck(FW6Packet packetToSend, int delayBeforeReadMS, int timeoutMS) { FW6Packet packet = SendAndGetResponse(packetToSend, delayBeforeReadMS, timeoutMS); if (packet is AckResponse) { return packet as AckResponse; } else { return null; } } /// /// Send a command and read the response /// /// /// /// protected abstract FW6Packet SendAndGetResponse(FW6Packet packetToSend, int timeoutMS); /// /// Send a command, wait delayBeforeReadMS, and then attempt to read the response for timeoutMS /// /// /// /// /// protected abstract FW6Packet SendAndGetResponse(FW6Packet packetToSend, int delayBeforeReadMS, int timeoutMS); /// /// The maximum time to wait for an ack after sending a data packet /// protected abstract int DataPacketAckTimeoutMS { get; } public abstract int mcaChannels { get; set; } public FW6Device(InterfaceTypes interfaceType, DeviceTypes deviceType, Version firmwareVersion, Version fpgaVersion, string serialNumber) : base(interfaceType, deviceType, firmwareVersion, fpgaVersion, serialNumber) { } /// /// Call to update the FpgaState /// internal void UpdateFpgaState() { try { // connect Connect(); // we can detect unprogrammed FPGAs for FW6 devices by attempting to retrieve the spectrum SpectrumRequest spectrumRequest = new SpectrumRequest(); FW6Packet response = SendAndGetResponse(spectrumRequest, DataPacketAckTimeoutMS); if (!(response is SpectrumResponse)) { FpgaState = FpgaStates.Unprogrammed; } } finally { Disconnect(); } } public DiagnosticResponse GetDiagnosticResponse { get { try { Connect(); // retrieve the device calibration values via the DiagnosticRequest command DiagnosticRequest diagnosticRequest = new DiagnosticRequest(); int responseTimeoutMS = 2500; // spec says maximum of 2.5 seconds before the response FW6Packet response = SendAndGetResponse(diagnosticRequest, responseTimeoutMS); if (response is DiagnosticResponse) { return response as DiagnosticResponse; } else { return null; } } finally { Disconnect(); } } } public void SetAdcCalibration(byte adcGain, byte adcOffset) { try { Connect(); SetADCCalibrationRequest setAdcCalibrationRequest = new SetADCCalibrationRequest(adcGain, adcOffset); AckResponse ackResponse = SendAndWaitForAck(setAdcCalibrationRequest, DataPacketAckTimeoutMS); if (ackResponse.AckType != AckResponse.AckTypes.Ok) { InvalidOperationException exception = new System.InvalidOperationException("ack response of " + ackResponse.AckType); throw exception; } } finally { Disconnect(); } } public void SetuCTemperatureCalibration(byte uCTempCalibration) { try { Connect(); SetuCTemperatureCalibrationRequest setuCTemperatureCalibrationRequest = new SetuCTemperatureCalibrationRequest(uCTempCalibration); AckResponse ackResponse = SendAndWaitForAck(setuCTemperatureCalibrationRequest, DataPacketAckTimeoutMS); if (ackResponse.AckType != AckResponse.AckTypes.Ok) { InvalidOperationException exception = new System.InvalidOperationException("ack response of " + ackResponse.AckType); throw exception; } } finally { Disconnect(); } } public void SetPZCorrection(byte pzCorrection) { try { Connect(); SetPZCorrectionRequest setPZCorrection = new SetPZCorrectionRequest(pzCorrection); AckResponse ackResponse = SendAndWaitForAck(setPZCorrection, DataPacketAckTimeoutMS); if (ackResponse.AckType != AckResponse.AckTypes.Ok) { InvalidOperationException exception = new System.InvalidOperationException("ack response of " + ackResponse.AckType); throw exception; } } finally { Disconnect(); } } public void SetDCalCalibration(ushort dcalValue) { try { Connect(); SetDCalRequest setDCalCalibration = new SetDCalRequest(dcalValue); AckResponse ackResponse = SendAndWaitForAck(setDCalCalibration, DataPacketAckTimeoutMS); if (ackResponse.AckType != AckResponse.AckTypes.Ok) { InvalidOperationException exception = new System.InvalidOperationException("ack response of " + ackResponse.AckType); throw exception; } } finally { Disconnect(); } } public abstract bool ConnectForTesting(); public abstract void DisconnectForTesting(); public abstract bool StartDppData(); public abstract bool PauseDppData(); public abstract bool ClearDppData(); public abstract bool GetConfiguration(string strCommands, ref byte[] readBuffer, out uint bytesRead); public abstract string GetFullConfiguration(bool PC5_PRESENT, int DppType, bool isDP5_RevDxGains, byte DPP_ECO); public abstract string DisplayConfiguration(string strCommands); public abstract bool SendConfiguration(string strCommands); public abstract bool GetRawStatus(ref byte[] readBuffer, out uint bytesRead); public abstract bool GetSpectrumData(ref byte[] readBuffer, out uint bytesRead); } }