public bool CaptureScopeSignal(int[] channels, Imports.Range range, int sampleTimeMs) { short overflow; int timeIndisposed; //Set timeout values int timeoutCount = 0; int maxTimeoutCount = sampleTimeMs + 500; //Set number of samples and calculate time base int numReadings = 1000000; //Get 1M readings uint sampleCount = (uint)numReadings; double frequency = 1000 * (numReadings / sampleTimeMs); uint timebase = (uint)(MaxSampleRate / frequency) - 1; //Sample at 1 MHz if (channels.Length < 1 || channels.Length > Imports.OCTO_SCOPE) { return(false); } //Disable trigger Imports.SetSimpleTrigger(handle, 0, Imports.Channel.CHANNEL_A + channels[0], 0, Imports.ThresholdDirection.Rising, 0, 0); foreach (int channel in channels) { //Enable scope with given range settings EnableScopeChannel(channel, range, true); InitScopeBuffer(channel, numReadings); } //Start block read Imports.ps4000aBlockReady _callbackDelegate = BlockCallback; blockReady = false; status = Imports.RunBlock(handle, 0, numReadings, timebase, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero); //Wait for data to be ready or timeout to occur while (!blockReady && (timeoutCount < maxTimeoutCount)) { Thread.Sleep(1); timeoutCount++; } //Ensure we did not timeout if (timeoutCount >= maxTimeoutCount) { return(false); } //Import values status = Imports.GetValues(handle, 0, ref sampleCount, 1, Imports.DownSamplingMode.None, 0, out overflow); //Check if reading was successful if (sampleCount != numReadings || status != 0) { return(false); } return(true); }
/**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { Imports.MaximumValue(_handle, out _maxValue); string[] description = { "Driver Version", "USB Version", "Hardware Version", "Variant Info", "Serial", "Cal Date", "Kernel", "Digital H/W", "Analogue H/W", "Firmware 1", "Firmware 2" }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (int i = 0; i < 11; i++) { short requiredSize; Imports.GetUnitInfo(_handle, line, 80, out requiredSize, (uint)i); Console.WriteLine("{0}: {1}", description[i], line); if (i == 3) // Variant information { _channelCount = int.Parse(line[1].ToString()); _channelSettings = new ChannelSettings[_channelCount]; if (_channelCount == 8) { _firstRange = Imports.Range.Range_10MV; _lastRange = Imports.Range.Range_50V; } else { _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_200V; } } } Console.WriteLine(); Console.WriteLine("Channel Voltage Ranges:"); Console.WriteLine(); for (int ch = 0; ch < _channelCount; ch++) { Imports.SetChannel(_handle, Imports.Channel.CHANNEL_A + ch, 1, Imports.Coupling.DC, Imports.Range.Range_5V, 0); _channelSettings[ch].enabled = true; _channelSettings[ch].range = Imports.Range.Range_5V; Console.WriteLine("Channel {0}: 5V", (char)('A' + ch)); } } }
/**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { string[] description = { "Driver Version", "USB Version", "Hardware Version", "Variant Info", "Serial", "Calibration Date", "Kernel Version", "Digital Hardware", "Analogue Hardware", "Firmware 1", "Firmware 2" }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (int i = 0; i < description.Length; i++) { short requiredSize; Imports.GetUnitInfo(_handle, line, 80, out requiredSize, (uint)i); Console.WriteLine("{0}: {1}", description[i], line); if (_powerSupplyConnected) { if (i == 3) { _channelCount = int.Parse(line[1].ToString()); } } else { _channelCount = 2; } _channelSettings = new ChannelSettings[_channelCount]; _noEnabledChannels = _channelCount; } Console.WriteLine(); Imports.Range voltageRange = Imports.Range.Range_5V; for (uint ch = 0; ch < _channelCount; ch++) { Imports.SetChannel(_handle, Imports.Channel.ChannelA + ch, 1, Imports.Coupling.PS5000A_DC, voltageRange, 0); _channelSettings[ch].enabled = true; _channelSettings[ch].range = voltageRange; } } }
public double GetScopeMeterReading(ScopeInput.MuxedInputSignal signal, Imports.Range range) { //Set the scope address outputController.SetScopeAddr((int)signal); Thread.Sleep(50); double reading = scopeInput.GetScopeQuickValue(mainScopeChannel, range); //Reset the scope address outputController.SetScopeAddr(0); return(reading); }
private bool EnableScopeChannel(int channel, Imports.Range range, bool enable) { short enabled = 0; if (enable) { enabled = 1; } //Try to set the channel based on the given settings if (Imports.SetChannel(handle, Imports.Channel.CHANNEL_A + channel, enabled, 1, range, 0) != 0) { return(false); } return(true); }
/**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; string[] description = { "Driver Version ", "USB Version ", "Hardware Version ", "Variant Info ", "Serial ", "Cal Date ", "Error Code ", "Kernel Ver ", }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (short i = 0; i < description.Length; i++) { Imports.GetUnitInfo(_handle, line, 80, i); if (i == 3) { if ((_channelCount = Convert.ToInt16(line[1]) - 48) == 1) { _firstRange = Imports.Range.Range_100MV; } else if ((Convert.ToInt16(line[3]) - 48) >= 3) // 2203, 2204, 2204A, 2205 and 2205A models { _hasFastStreaming = true; } } if (i != 6) { Console.WriteLine("{0}: {1}", description[i], line); } } Console.WriteLine(); } }
public bool SetChannel(short channelA_OnOff, short channelB_OnOff, Imports.Range VoltageLevel) { short a = PS2000ACSConsole.Imports.SetTriggerChannelProperties(handle, null, 0, 0, 0); //Disable Trigger //Channel A short b = PS2000ACSConsole.Imports.SetChannel(handle, Imports.Channel.ChannelA, channelA_OnOff, coupling_type, VoltageLevel, 0 ); //Channel B short c = PS2000ACSConsole.Imports.SetChannel(handle, Imports.Channel.ChannelB, channelB_OnOff, coupling_type, VoltageLevel, 0 ); //Disable Digitalports Imports.Channel port; short status = 0; // Disable Digital ports /////////////////////////////////////////////////////////////////////////////////////////////////////////// for (port = Imports.Channel.PS2000A_DIGITAL_PORT0; port < Imports.Channel.PS2000A_DIGITAL_PORT1; port++) // { // status = Imports.SetDigitalPort(handle, port, 0, 0); // } // /////////////////////////////////////////////////////////////////////////////////////////////////////////// if ((a == 0) && (b == 0) && (c == 0) && (status == 0)) { return(true); } else { return(false); } }
/**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { int variant = 0; string[] description = { "Driver Version ", "USB Version ", "Hardware Version ", "Variant Info ", "Serial ", "Cal Date ", "Kernel Ver " }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (int i = 0; i < 7; i++) { short requiredSize; Imports.GetUnitInfo(_handle, line, 80, out requiredSize, i); if (i == 3) { line.Length = 4; variant = Convert.ToInt16(line.ToString()); } Console.WriteLine("{0}: {1}", description[i], line); } switch (variant) { case (int)Imports.Model.PS4223: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_100V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4224: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4423: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_100V; _channelCount = QUAD_SCOPE; break; case (int)Imports.Model.PS4424: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; break; case (int)Imports.Model.PS4226: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4227: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4262: _firstRange = Imports.Range.Range_10MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; } } }
public void Run() { // setup devices GetDeviceInfo(); _timebase = 1; _firstRange = Imports.Range.Range_100MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = true; _channelSettings[i].range = Imports.Range.Range_5V; } // main loop - read key and call routine char ch = ' '; while (ch != 'X') { Console.WriteLine(""); Console.WriteLine("B - immediate block V - Set voltages"); Console.WriteLine("T - triggered block I - Set timebase"); Console.WriteLine("R - rapid block F - toggle signal generator on/off"); Console.WriteLine("E - ETS block A - ADC counts/mV"); Console.WriteLine("S - immediate streaming "); Console.WriteLine("W - triggered streaming"); Console.WriteLine(" X - exit"); Console.WriteLine("Operation:"); ch = char.ToUpper(Console.ReadKey().KeyChar); Console.WriteLine("\n"); switch (ch) { case 'B': CollectBlockImmediate(); break; case 'T': CollectBlockTriggered(); break; case 'R': CollectBlockRapid(); break; case 'S': CollectStreamingImmediate(); break; case 'W': CollectStreamingTriggered(); break; case 'F': SetSignalGenerator(); break; case 'E': CollectBlockEts(); break; case 'V': SetVoltages(); break; case 'I': SetTimebase(); break; case 'A': _scaleVoltages = !_scaleVoltages; if (_scaleVoltages) { Console.WriteLine("Readings will be scaled in mV"); } else { Console.WriteLine("Readings will be scaled in ADC counts"); } break; case 'X': /* Handled by outer loop */ break; default: Console.WriteLine("Invalid operation"); break; } } }
/**************************************************************************** * SetTrigger * this function sets all the required trigger parameters, and calls the * triggering functions ****************************************************************************/ short SetTrigger(Imports.TriggerChannelProperties[] channelProperties, short nChannelProperties, Imports.TriggerConditions[] triggerConditions, short nTriggerConditions, Imports.ThresholdDirection[] directions, Pwq pwq, uint delay, short auxOutputEnabled, int autoTriggerMs) { short status; if ( (status = Imports.SetTriggerChannelProperties(_handle, channelProperties, nChannelProperties, auxOutputEnabled, autoTriggerMs)) != 0) { return(status); } if ((status = Imports.SetTriggerChannelConditions(_handle, triggerConditions, nTriggerConditions)) != 0) { return(status); } if (directions == null) { directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None } } ; if ((status = Imports.SetTriggerChannelDirections(_handle, directions[(int)Imports.Channel.ChannelA], directions[(int)Imports.Channel.ChannelB], directions[(int)Imports.Channel.ChannelC], directions[(int)Imports.Channel.ChannelD], directions[(int)Imports.Channel.External], directions[(int)Imports.Channel.Aux])) != 0) { return(status); } if ((status = Imports.SetTriggerDelay(_handle, delay)) != 0) { return(status); } if (pwq == null) { pwq = new Pwq(null, 0, Imports.ThresholdDirection.None, 0, 0, Imports.PulseWidthType.None); } status = Imports.SetPulseWidthQualifier(_handle, pwq.conditions, pwq.nConditions, pwq.direction, pwq.lower, pwq.upper, pwq.type); return(status); } /**************************************************************************** * CollectBlockImmediate * this function demonstrates how to collect a single block of data * from the unit (start collecting immediately) ****************************************************************************/ void CollectBlockImmediate() { Console.WriteLine("Collect block immediate..."); Console.WriteLine("Press a key to start"); WaitForKey(); SetDefaults(); /* Trigger disabled */ SetTrigger(null, 0, null, 0, null, null, 0, 0, 0); BlockDataHandler("First 10 readings", 0); } /**************************************************************************** * CollectBlockRapid * this function demonstrates how to collect blocks of data * using the RapidCapture function ****************************************************************************/ void CollectBlockRapid() { ushort numRapidCaptures = 1; bool valid = false; Console.WriteLine("Collect rapid block..."); Console.WriteLine("Specify number of captures:"); do { try { numRapidCaptures = ushort.Parse(Console.ReadLine()); valid = true; } catch { valid = false; Console.WriteLine("\nEnter numeric values only"); } } while (Imports.SetNoOfRapidCaptures(_handle, numRapidCaptures) > 0 || !valid); int maxSamples; Imports.MemorySegments(_handle, numRapidCaptures, out maxSamples); Console.WriteLine("Collecting {0} rapid blocks. Press a key to start", numRapidCaptures); WaitForKey(); SetDefaults(); /* Trigger is optional, disable it for now */ SetTrigger(null, 0, null, 0, null, null, 0, 0, 0); RapidBlockDataHandler(numRapidCaptures); } /**************************************************************************** * CollectBlockTriggered * this function demonstrates how to collect a single block of data from the * unit, when a trigger event occurs. ****************************************************************************/ void CollectBlockTriggered() { short triggerVoltage = mv_to_adc(1000, (short)_channelSettings[(int)Imports.Channel.ChannelA].range); // ChannelInfo stores ADC counts Imports.TriggerChannelProperties[] sourceDetails = new Imports.TriggerChannelProperties[] { new Imports.TriggerChannelProperties(triggerVoltage, 256 * 10, triggerVoltage, 256 * 10, Imports.Channel.ChannelA, Imports.ThresholdMode.Level) }; Imports.TriggerConditions[] conditions = new Imports.TriggerConditions[] { new Imports.TriggerConditions(Imports.TriggerState.True, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare) }; Imports.ThresholdDirection[] directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.Rising, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None }; Console.WriteLine("Collect block triggered..."); Console.Write("Collects when value rises past {0}", (_scaleVoltages) ? adc_to_mv(sourceDetails[0].ThresholdMajor, (int)_channelSettings[(int)Imports.Channel.ChannelA].range) : sourceDetails[0].ThresholdMajor); Console.WriteLine("{0}", (_scaleVoltages) ? ("mV") : ("ADC Counts")); Console.WriteLine("Press a key to start..."); WaitForKey(); SetDefaults(); /* Trigger enabled * Rising edge * Threshold = 1000mV */ SetTrigger(sourceDetails, 1, conditions, 1, directions, null, 0, 0, 0); BlockDataHandler("Ten readings after trigger", 0); } /**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { int variant = 0; string[] description = { "Driver Version ", "USB Version ", "Hardware Version ", "Variant Info ", "Serial ", "Cal Date ", "Kernel Ver " }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (int i = 0; i < 7; i++) { short requiredSize; Imports.GetUnitInfo(_handle, line, 80, out requiredSize, i); if (i == 3) { line.Length = 4; variant = Convert.ToInt16(line.ToString()); } Console.WriteLine("{0}: {1}", description[i], line); } switch (variant) { case (int)Imports.Model.PS4223: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_100V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4224: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4423: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_100V; _channelCount = QUAD_SCOPE; break; case (int)Imports.Model.PS4424: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; break; case (int)Imports.Model.PS4226: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4227: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; case (int)Imports.Model.PS4262: _firstRange = Imports.Range.Range_10MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; break; } } } /**************************************************************************** * Select input voltage ranges for channels A and B ****************************************************************************/ void SetVoltages() { bool valid = false; /* See what ranges are available... */ for (int i = (int)_firstRange; i <= (int)_lastRange; i++) { Console.WriteLine("{0} . {1} mV", i, inputRanges[i]); } /* Ask the user to select a range */ Console.WriteLine("Specify voltage range ({0}..{1})", _firstRange, _lastRange); Console.WriteLine("99 - switches channel off"); for (int ch = 0; ch < _channelCount; ch++) { Console.WriteLine(""); uint range = 8; do { try { Console.WriteLine("Channel: {0}", (char)('A' + ch)); range = uint.Parse(Console.ReadLine()); valid = true; } catch { valid = false; Console.WriteLine("\nEnter numeric values only"); } } while ((range != 99 && (range < (uint)_firstRange || range > (uint)_lastRange) || !valid)); if (range != 99) { _channelSettings[ch].range = (Imports.Range)range; Console.WriteLine(" = {0} mV", inputRanges[range]); _channelSettings[ch].enabled = true; } else { Console.WriteLine("Channel Switched off"); _channelSettings[ch].enabled = false; } } SetDefaults(); // Set defaults now, so that if all but 1 channels get switched off, timebase updates to timebase 0 will work } /**************************************************************************** * * Select _timebase, set _oversample to on and time units as nano seconds * ****************************************************************************/ void SetTimebase() { int timeInterval; int maxSamples; bool valid = false; Console.WriteLine("Specify timebase"); do { try { _timebase = uint.Parse(Console.ReadLine()); valid = true; } catch { valid = false; Console.WriteLine("\nEnter numeric values only"); } } while (!valid); while (Imports.GetTimebase(_handle, _timebase, BUFFER_SIZE, out timeInterval, 1, out maxSamples, 0) != 0) { Console.WriteLine("Selected timebase {0} could not be used", _timebase); _timebase++; } Console.WriteLine("Timebase {0} - {1} ns", _timebase, timeInterval); _oversample = 1; } /**************************************************************************** * Stream Data Handler * - Used by the two stream data examples - untriggered and triggered * Inputs: * - preTrigger - the number of samples in the pre-trigger phase * (0 if no trigger has been set) ***************************************************************************/ void StreamDataHandler(uint preTrigger) { uint sampleCount = BUFFER_SIZE * 10; /* *10 is to make sure buffer large enough */ short[][] minBuffers = new short[_channelCount][]; short[][] maxBuffers = new short[_channelCount][]; PinnedArray <short>[] minPinned = new PinnedArray <short> [_channelCount]; PinnedArray <short>[] maxPinned = new PinnedArray <short> [_channelCount]; uint totalsamples = 0; uint triggeredAt = 0; short status; uint sampleInterval = 1; for (int i = 0; i < _channelCount; i++) // create data buffers { minBuffers[i] = new short[sampleCount]; maxBuffers[i] = new short[sampleCount]; minPinned[i] = new PinnedArray <short>(minBuffers[i]); maxPinned[i] = new PinnedArray <short>(maxBuffers[i]); status = Imports.SetDataBuffers(_handle, (Imports.Channel)i, minBuffers[i], maxBuffers[i], (int)sampleCount); } Console.WriteLine("Waiting for trigger...Press a key to abort"); _autoStop = false; status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, preTrigger, 1000000 - preTrigger, true, 1000, sampleCount); Console.WriteLine("Run Streaming : {0} ", status); Console.WriteLine("Streaming data...Press a key to abort"); TextWriter writer = new StreamWriter("stream.txt", false); writer.Write("For each of the {0} Channels, results shown are....", _channelCount); writer.WriteLine(); writer.WriteLine("Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV"); writer.WriteLine(); for (int i = 0; i < _channelCount; i++) { writer.Write("Ch Max ADC Max mV Min ADC Min mV "); } writer.WriteLine(); while (!_autoStop && !Console.KeyAvailable) { /* Poll until data is received. Until then, GetStreamingLatestValues wont call the callback */ Thread.Sleep(100); _ready = false; Imports.GetStreamingLatestValues(_handle, StreamingCallback, IntPtr.Zero); if (_ready && _sampleCount > 0) /* can be ready and have no data, if autoStop has fired */ { if (_trig > 0) { triggeredAt = totalsamples + _trigAt; } totalsamples += (uint)_sampleCount; Console.Write("\nCollected {0,4} samples, index = {1,5} Total = {2,5}", _sampleCount, _startIndex, totalsamples); if (_trig > 0) { Console.Write("\tTrig at Index {0}", triggeredAt); } for (uint i = _startIndex; i < (_startIndex + _sampleCount); i++) { for (int ch = 0; ch < _channelCount; ch++) { if (_channelSettings[ch].enabled) { writer.Write("Ch{0} {1,7} {2,7} {3,7} {4,7} ", (char)('A' + ch), minPinned[ch].Target[i], adc_to_mv(minPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range), maxPinned[ch].Target[i], adc_to_mv(maxPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.ChannelA + ch)].range)); } } writer.WriteLine(); } } } if (Console.KeyAvailable) { Console.ReadKey(true); // clear the key } Imports.Stop(_handle); writer.Close(); if (!_autoStop) { Console.WriteLine("\ndata collection aborted"); } foreach (PinnedArray <short> p in minPinned) { if (p != null) { p.Dispose(); } } foreach (PinnedArray <short> p in maxPinned) { if (p != null) { p.Dispose(); } } } /**************************************************************************** * CollectStreamingImmediate * this function demonstrates how to collect a stream of data * from the unit (start collecting immediately) ***************************************************************************/ void CollectStreamingImmediate() { SetDefaults(); Console.WriteLine("Collect streaming..."); Console.WriteLine("Data is written to disk file (stream.txt)"); Console.WriteLine("Press a key to start"); WaitForKey(); /* Trigger disabled */ SetTrigger(null, 0, null, 0, null, null, 0, 0, 0); StreamDataHandler(0); } /**************************************************************************** * CollectStreamingTriggered * this function demonstrates how to collect a stream of data * from the unit (start collecting on trigger) ***************************************************************************/ void CollectStreamingTriggered() { short triggerVoltage = mv_to_adc(1000, (short)_channelSettings[(int)Imports.Channel.ChannelA].range); // ChannelInfo stores ADC counts Imports.TriggerChannelProperties[] sourceDetails = new Imports.TriggerChannelProperties[] { new Imports.TriggerChannelProperties(triggerVoltage, 256 * 10, triggerVoltage, 256 * 10, Imports.Channel.ChannelA, Imports.ThresholdMode.Level) }; Imports.TriggerConditions[] conditions = new Imports.TriggerConditions[] { new Imports.TriggerConditions(Imports.TriggerState.True, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare, Imports.TriggerState.DontCare) }; Imports.ThresholdDirection[] directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.Rising, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None }; Console.WriteLine("Collect streaming triggered..."); Console.WriteLine("Data is written to disk file (stream.txt)"); Console.WriteLine("Press a key to start"); WaitForKey(); SetDefaults(); /* Trigger enabled * Rising edge * Threshold = 1000mV */ SetTrigger(sourceDetails, 1, conditions, 1, directions, null, 0, 0, 0); StreamDataHandler(100000); } /**************************************************************************** * DisplaySettings * Displays information about the user configurable settings in this example ***************************************************************************/ void DisplaySettings() { int ch; int voltage; Console.WriteLine("\n\nReadings will be scaled in {0}", (_scaleVoltages) ? ("mV") : ("ADC counts")); for (ch = 0; ch < _channelCount; ch++) { voltage = inputRanges[(int)_channelSettings[ch].range]; Console.Write("Channel {0} Voltage Range = ", (char)('A' + ch)); if (voltage < 1000) { Console.WriteLine("{0}mV", voltage); } else { Console.WriteLine("{0}V", voltage / 1000); } } Console.WriteLine(); }
/**************************************************************************** * Select resolution of device ****************************************************************************/ void SetResolution() { bool valid; int maxSelection = 2; uint status; Console.WriteLine("0 : 8 bits"); Console.WriteLine("1 : 12 bits"); Console.WriteLine("2 : 14 bits"); if (_noEnabledChannels <= 2) { Console.WriteLine("3 : 15 bits"); //can only use up to 2 channels with 15 bit mode if (_noEnabledChannels < 2) { Console.WriteLine("4 : 16 bits"); //can only use 1 channel with 16 bit mode maxSelection = 4; } else { maxSelection = 3; } } Console.WriteLine(); do { try { Console.WriteLine("Resolution: "); _resolution = (Imports.DeviceResolution)(uint.Parse(Console.ReadLine())); valid = true; } catch (FormatException e) { valid = false; Console.WriteLine("Error: " + e.Message); } if (_resolution > (Imports.DeviceResolution)maxSelection) { Console.WriteLine("Please select a number stated above"); valid = false; } } while (!valid); if ((status = Imports.SetDeviceResolution(_handle, _resolution)) != 0) { Console.WriteLine("Resolution not set Error code: {0)", status); } switch (_resolution) { case Imports.DeviceResolution.PS5000A_DR_8BIT: _firstRange = Imports.Range.Range_10mV; break; case Imports.DeviceResolution.PS5000A_DR_12BIT: _firstRange = Imports.Range.Range_20mV; break; default: _firstRange = Imports.Range.Range_50mV; break; } }
/**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { string[] description = { "Driver Version", "USB Version", "Hardware Version", "Variant Info", "Serial", "Error Code" }; int variant=0; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (int i = 0; i < 5; i++) { short requiredSize; Imports.GetUnitInfo(_handle, line, 80, out requiredSize, i); Console.WriteLine("{0}: {1}", description[i], line); if (i == 3) { if (line.Length == 4) // Standard model, not A or B variant, convert model number into Hex i.e. 6402 -> 0x6402 { variant = int.Parse(line.ToString()); variant += 0x4B00; } else if (line.Length == 5) // A or B variant { if (line[4] == 'A' || line[4] == 'a') // i.e. 6402A -> 0xA402 { line = line.Remove(4, 1); variant = int.Parse(line.ToString()); variant += 0x8B00; } else if (line[4] == 'B' || line[4] == 'b') // i.e. 6402B -> 0xB402 { line = line.Remove(4, 1); variant = int.Parse(line.ToString()); variant += 0x9B00; } } } } switch ((Imports.MODEL_TYPE)variant) { case Imports.MODEL_TYPE.MODEL_PS6402: case Imports.MODEL_TYPE.MODEL_PS6402B: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = true; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6402A: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = false; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6403: case Imports.MODEL_TYPE.MODEL_PS6403B: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = true; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6403A: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = false; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6404: case Imports.MODEL_TYPE.MODEL_PS6404B: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = true; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6404A: _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = QUAD_SCOPE; _AWG = false; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_1M; _channelSettings[i].range = Imports.Range.Range_5V; } break; case Imports.MODEL_TYPE.MODEL_PS6407: _firstRange = Imports.Range.Range_100MV; // fixed 100mV input on 6407 _lastRange = Imports.Range.Range_100MV; _channelCount = QUAD_SCOPE; _channelSettings = new ChannelSettings[MAX_CHANNELS]; for (int i = 0; i < MAX_CHANNELS; i++) { _channelSettings[i].enabled = true; _channelSettings[i].DCcoupled = Imports.PS6000Coupling.PS6000_DC_50R; // fixed 50ohm imput impedance on 6407 _channelSettings[i].range = Imports.Range.Range_100MV; } break; } } }
public int GetScopeCrossingCounts(int channel, double expectedVoltage, int sampleTimeMs) { crossingIdx = new List <int>(); int count = 0; double hysteresisFactor = 0.05; double crossThreshold = expectedVoltage / 2; Imports.Range range = MaxRange; bool rising = true; double voltage; //Get the voltage range using the expected voltage of the signal for (int i = 0; i < inputRanges.Length; i++) { if (expectedVoltage < inputRanges[i]) { range = (Imports.Range)(MinxRange + i); break; } } double scaling = inputRanges[(int)range] / Imports.MaxValue; int[] channels = { channel }; //Capture PWM waveform if (!CaptureScopeSignal(channels, range, sampleTimeMs)) { return(-1); } //Set rising or falling based on initial sample level if ((double)dataBuffers[channel].Target[0] * scaling < crossThreshold) { rising = true; } else { rising = false; } //System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\MainBd_TestStand_2\Desktop\waveform.txt"); //Iterate over results and get crossing counts for ( int i = 0; i < dataBuffers[channel].Target.Length; i++) { double adc = (double)dataBuffers[channel].Target[i]; //if(i<1000000) // file.WriteLine(adc); //Convert voltage voltage = (adc * scaling); //If rising, check for voltage above threshold + hysteresis if (rising) { if (voltage > (crossThreshold * (1 + hysteresisFactor))) { crossingIdx.Add(i); count++; rising = false; } } //If falling, check for voltage below threshold + hysteresis else { if (voltage < (crossThreshold * (1 - hysteresisFactor))) { crossingIdx.Add(i); count++; rising = true; } } } return(count); }
public double GetScopeQuickValue(int channel, Imports.Range range) { double avg = 0; int numReadings = 500; uint sampleCount = (uint)numReadings; int timeoutCount = 0; int maxTimeoutCount = 100; int timeIndisposed; short overflow; if (range > MaxRange) { range = MaxRange; } //Enable scope with given range settings EnableScopeChannel(channel, range, true); //Disable trigger Imports.SetSimpleTrigger(handle, 0, Imports.Channel.CHANNEL_A, 0, Imports.ThresholdDirection.Rising, 0, 0); //Initialize the scope buffer InitScopeBuffer(channel, numReadings); //Start block read uint timebase = (MaxSampleRate / 1000000) - 1; //Sample at 1 MHz Imports.ps4000aBlockReady _callbackDelegate = BlockCallback; blockReady = false; status = Imports.RunBlock(handle, 0, numReadings, timebase, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero); //Wait for read to finish or timeout while (!blockReady && (timeoutCount < maxTimeoutCount)) { Thread.Sleep(1); timeoutCount++; } //Ensure we did not timeout if (timeoutCount >= maxTimeoutCount) { return(-1); } //Try to get readings status = Imports.GetValues(handle, 0, ref sampleCount, 1, Imports.DownSamplingMode.None, 0, out overflow); //Check if reading was successful if (sampleCount != numReadings || status != 0) { return(-1); } //Get average value for (int i = 0; i < numReadings; i++) { avg += dataBuffers[channel].Target[i]; } EnableScopeChannel(channel, range, false); dataBuffers[channel] = null; //Conversion from adc value to volts avg = (avg / numReadings); double scaling = inputRanges[(int)range] / Imports.MaxValue; return((double)avg * scaling); }
/**************************************************************************** * SetTrigger * this function sets all the required trigger parameters, and calls the * triggering functions ****************************************************************************/ short SetTrigger(Imports.TriggerChannelProperties[] channelProperties, short nChannelProperties, Imports.TriggerConditions[] triggerConditions, short nTriggerConditions, Imports.ThresholdDirection[] directions, Pwq pwq, uint delay, int autoTriggerMs) { short status; if ((status = Imports.SetTriggerChannelProperties(_handle, channelProperties, nChannelProperties, autoTriggerMs)) == 0) { return(status); } if ((status = Imports.SetTriggerChannelConditions(_handle, triggerConditions, nTriggerConditions)) == 0) { return(status); } if (directions == null) { directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None, Imports.ThresholdDirection.None } } ; if ((status = Imports.SetTriggerChannelDirections(_handle, directions[(int)Imports.Channel.ChannelA], directions[(int)Imports.Channel.ChannelB], directions[(int)Imports.Channel.ChannelC], directions[(int)Imports.Channel.ChannelD], directions[(int)Imports.Channel.External])) == 0) { return(status); } if ((status = Imports.SetTriggerDelay(_handle, delay, 0)) == 0) { return(status); } if (pwq == null) { pwq = new Pwq(null, 0, Imports.ThresholdDirection.None, 0, 0, Imports.PulseWidthType.None); } status = Imports.SetPulseWidthQualifier(_handle, pwq.conditions, pwq.nConditions, pwq.direction, pwq.lower, pwq.upper, pwq.type); return(status); } /**************************************************************************** * CollectBlockImmediate * this function demonstrates how to collect a single block of data * from the unit (start collecting immediately) ****************************************************************************/ void CollectBlockImmediate() { Console.WriteLine("Collect Block Immediate"); Console.WriteLine("Data is written to disk file ({0})", BlockFile); Console.WriteLine("Press a key to start..."); Console.WriteLine(); WaitForKey(); SetDefaults(); /* Trigger disabled */ SetTrigger(null, 0, null, 0, null, null, 0, 0); BlockDataHandler("First 10 readings", 0); } /**************************************************************************** * CollectBlockTriggered * this function demonstrates how to collect a single block of data from the * unit, when a trigger event occurs. ****************************************************************************/ void CollectBlockTriggered() { short triggerVoltage = mv_to_adc(1000, (short)_channelSettings[(int)Imports.Channel.ChannelA].range); // ChannelInfo stores ADC counts Imports.TriggerChannelProperties[] sourceDetails = new Imports.TriggerChannelProperties[] { new Imports.TriggerChannelProperties(triggerVoltage, triggerVoltage, 256 * 10, Imports.Channel.ChannelA, Imports.ThresholdMode.Level) }; Imports.TriggerConditions[] conditions = new Imports.TriggerConditions[] { new Imports.TriggerConditions(Imports.TriggerState.True, // Channel A Imports.TriggerState.DontCare, // Channel B Imports.TriggerState.DontCare, // Channel C Imports.TriggerState.DontCare, // Channel C Imports.TriggerState.DontCare, // external Imports.TriggerState.DontCare // pwq ) }; Imports.ThresholdDirection[] directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.Rising, // Channel A Imports.ThresholdDirection.None, // Channel B Imports.ThresholdDirection.None, // Channel C Imports.ThresholdDirection.None, // Channel D Imports.ThresholdDirection.None }; // ext Console.WriteLine("Collect Block Triggered"); Console.WriteLine("Data is written to disk file ({0})", BlockFile); Console.Write("Collects when value rises past {0}mV", adc_to_mv(sourceDetails[0].ThresholdMajor, (int)_channelSettings[(int)Imports.Channel.ChannelA].range)); Console.WriteLine("Press a key to start..."); WaitForKey(); SetDefaults(); /* Trigger enabled * Rising edge * Threshold = 1000mV */ SetTrigger(sourceDetails, 1, conditions, 1, directions, null, 0, 0); BlockDataHandler("Ten readings after trigger", 0); } /**************************************************************************** * Stream * this function demonstrates how to stream data * from the unit (start collecting immediately) ****************************************************************************/ void Stream() { Console.WriteLine("Stream Data Immediate"); Console.WriteLine("Data is written to disk file ({0})", StreamFile); Console.WriteLine("Press a key to start..."); Console.WriteLine(); WaitForKey(); SetDefaults(); /* Trigger disabled */ SetTrigger(null, 0, null, 0, null, null, 0, 0); StreamDataHandler(false); } /**************************************************************************** * TriggeredFastStream * this function demonstrates how to stream data from the * unit, and stop after trigger has occured ****************************************************************************/ void TriggeredFastStream() { short triggerVoltage = mv_to_adc(1000, (short)_channelSettings[(int)Imports.Channel.ChannelA].range); // ChannelInfo stores ADC counts Imports.TriggerChannelProperties[] sourceDetails = new Imports.TriggerChannelProperties[] { new Imports.TriggerChannelProperties(triggerVoltage, triggerVoltage, 256 * 10, Imports.Channel.ChannelA, Imports.ThresholdMode.Level) }; Imports.TriggerConditions[] conditions = new Imports.TriggerConditions[] { new Imports.TriggerConditions(Imports.TriggerState.True, // Channel A Imports.TriggerState.DontCare, // Channel B Imports.TriggerState.DontCare, // Channel C Imports.TriggerState.DontCare, // Channel C Imports.TriggerState.DontCare, // external Imports.TriggerState.DontCare // pwq ) }; Imports.ThresholdDirection[] directions = new Imports.ThresholdDirection[] { Imports.ThresholdDirection.Rising, // Channel A Imports.ThresholdDirection.None, // Channel B Imports.ThresholdDirection.None, // Channel C Imports.ThresholdDirection.None, // Channel D Imports.ThresholdDirection.None }; // ext Console.WriteLine("Stream Data until 1000 samples after Triggered"); Console.WriteLine("Data is written to disk file ({0})", StreamFile); Console.WriteLine("Press a key to start..."); WaitForKey(); SetDefaults(); /* Trigger enabled * Rising edge * Threshold = 1000mV */ SetTrigger(sourceDetails, 1, conditions, 1, directions, null, 0, 0); StreamDataHandler(true); } /**************************************************************************** * FastStream * this function demonstrates how to stream data fast * from the unit (start collecting immediately) ****************************************************************************/ void FastStream() { Console.WriteLine("Fast Stream Data Immediate"); Console.WriteLine("Data is written to disk file ({0})", StreamFile); Console.WriteLine("Press a key to start..."); Console.WriteLine(); WaitForKey(); SetDefaults(); /* Trigger disabled */ SetTrigger(null, 0, null, 0, null, null, 0, 0); StreamDataHandler(true); } /**************************************************************************** * Initialise unit' structure with Variant specific defaults ****************************************************************************/ void GetDeviceInfo() { _firstRange = Imports.Range.Range_50MV; _lastRange = Imports.Range.Range_20V; _channelCount = DUAL_SCOPE; string[] description = { "Driver Version ", "USB Version ", "Hardware Version ", "Variant Info ", "Serial ", "Cal Date ", "Kernel Ver ", "Digital Hardware ", "Analogue Hardware " }; System.Text.StringBuilder line = new System.Text.StringBuilder(80); if (_handle >= 0) { for (short i = 0; i < 9; i++) { Imports.GetUnitInfo(_handle, line, 80, i); if (i == 3) { if ((_channelCount = Convert.ToInt16(line[1]) - 48) == 1) { _firstRange = Imports.Range.Range_100MV; } else if ((Convert.ToInt16(line[3]) - 48) >= 3) { _hasFastStreaming = true; } } Console.WriteLine("{0}: {1}", description[i], line); } } } /**************************************************************************** * Select input voltage ranges for channels A and B ****************************************************************************/ void SetVoltages() { bool valid = false; /* See what ranges are available... */ for (int i = (int)_firstRange; i <= (int)_lastRange; i++) { Console.WriteLine("{0} . {1} mV", i, inputRanges[i]); } /* Ask the user to select a range */ Console.WriteLine("Specify voltage range ({0}..{1})", _firstRange, _lastRange); Console.WriteLine("99 - switches channel off"); for (int ch = 0; ch < _channelCount; ch++) { Console.WriteLine(""); uint range = 8; do { try { Console.WriteLine("Channel: {0}", (char)('A' + ch)); range = uint.Parse(Console.ReadLine()); valid = true; } catch (FormatException) { valid = false; Console.WriteLine("\nEnter numeric values only"); } } while ((range != 99 && (range < (uint)_firstRange || range > (uint)_lastRange) || !valid)); if (range != 99) { _channelSettings[ch].range = (Imports.Range)range; Console.WriteLine(" = {0} mV", inputRanges[range]); _channelSettings[ch].enabled = 1; } else { Console.WriteLine("Channel Switched off"); _channelSettings[ch].enabled = 0; } } SetDefaults(); // Set defaults now, so that if all but 1 channels get switched off, timebase updates to timebase 0 will work } /**************************************************************************** * * Select _timebase, set _oversample to on and time units as nano seconds * ****************************************************************************/ void SetTimebase() { int timeInterval; int maxSamples; short timeunit; bool valid = false; Console.WriteLine("Specify timebase"); do { try { _timebase = short.Parse(Console.ReadLine()); valid = true; } catch (FormatException) { valid = false; Console.WriteLine("\nEnter numeric values only"); } } while (!valid); while ((Imports.GetTimebase(_handle, _timebase, BUFFER_SIZE, out timeInterval, out timeunit, _oversample, out maxSamples)) == 0) { Console.WriteLine("Selected timebase {0} could not be used", _timebase); _timebase++; } Console.WriteLine("Timebase {0} - {1} ns", _timebase, timeInterval); _oversample = 1; } /**************************************************************************** * DisplaySettings * Displays information about the user configurable settings in this example ***************************************************************************/ void DisplaySettings() { int ch; int voltage; for (ch = 0; ch < _channelCount; ch++) { if (_channelSettings[ch].enabled == 0) { Console.WriteLine("Channel {0} Voltage Range = Off", (char)('A' + ch)); } else { voltage = inputRanges[(int)_channelSettings[ch].range]; Console.Write("Channel {0} Voltage Range = ", (char)('A' + ch)); if (voltage < 1000) { Console.WriteLine("{0}mV", voltage); } else { Console.WriteLine("{0}V", voltage / 1000); } } } Console.WriteLine(); }
int adc_to_mv(int raw, Imports.Range range) { return((raw * inputRanges[(int)(range)]) / Imports.MaxValue); }
short mv_to_adc(int mv, Imports.Range range) { return((short)((mv * Imports.MaxValue) / inputRanges[(int)(range)])); }