Change Channel

Post your .Net discussions here
Post Reply
mosine
Newbie
Posts: 0
Joined: Thu Feb 09, 2017 2:03 pm

Change Channel

Post by mosine »

Hi,
I'm developer a C# application with PicoScope 3204A.
This morning, I have a new problem: when I change acquisition channel, I'll have a crash.
I enable channel A and B. For change channel, I set trigger on new channel. This is my code:

Code: Select all

 public bool CollectBlockTriggered(uint timebase, uint samples, int type, short mVlevelMax, short mVlevelMin, int channel) //channel value 0=channelA 1=channelB
        {
            try
            {
                Imports.TriggerChannelProperties[] sourceDetails;
                if (type == 0)
                {
                    short triggerVoltage = mv_to_adc(100, (short)_channelSettings[channel].range); // ChannelInfo stores ADC counts
                    sourceDetails = new Imports.TriggerChannelProperties[] {
                                                                         new Imports.TriggerChannelProperties(triggerVoltage,
                                                                                                              256*1,
                                                                                                              triggerVoltage,
                                                                                                              256*1,
                                                                                                              (channel==0)?Imports.Channel.ChannelA:Imports.Channel.ChannelB,
                                                                                                              Imports.ThresholdMode.Level)
                                                                        };
                }
                else
                {
                    short triggerVoltageMax = mv_to_adc(mVlevelMax, (short)_channelSettings[channel].range);
                    short triggerVoltageMin = mv_to_adc(mVlevelMin, (short)_channelSettings[channel].range);

                    sourceDetails = new Imports.TriggerChannelProperties[] {
                                                                         new Imports.TriggerChannelProperties(triggerVoltageMax,
                                                                                                             10,
                                                                                                             triggerVoltageMin,
                                                                                                             10,
                                                                                                             (channel==0)?Imports.Channel.ChannelA:Imports.Channel.ChannelB,
                                                                                                             Imports.ThresholdMode.Level)};
                }
                Imports.TriggerConditions[] conditions = new Imports.TriggerConditions[] {
                        new Imports.TriggerConditions((channel==0)?Imports.TriggerState.True:Imports.TriggerState.DontCare,
                                                      (channel==1)?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[]
	                                        { 
                                                (channel==0)?Imports.ThresholdDirection.Rising:Imports.ThresholdDirection.None,
                                                (channel==1)?Imports.ThresholdDirection.Rising:Imports.ThresholdDirection.None,
                                                Imports.ThresholdDirection.None, 
                                                Imports.ThresholdDirection.None, 
                                                Imports.ThresholdDirection.None,
                                                Imports.ThresholdDirection.None };



                SetDefaults();

                /* Trigger enabled
                 * Rising edge
                 * Threshold = 1000mV */
                SetTrigger(sourceDetails, 1, conditions, 1, directions, null, 0, 0, 0);
                //SetTrigger(null, 0, null, 0, null, null, 0, 0, 0);

                _timebase = timebase;

                BlockDataHandler(0, samples, type, channel);


                return true;

            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return true;
            }

        }

 void BlockDataHandler(int offset, uint samples, int type, int channel)
        {
            uint status;
            uint sampleCount = samples;
            ushort segmentIndex = 0;
            int i = 0;
            PinnedArray[] minPinned = new PinnedArray[_channelCount];
            PinnedArray[] maxPinned = new PinnedArray[_channelCount];
            int timeIndisposed;

            short[] minBuffers = new short[sampleCount];
            short[] maxBuffers = new short[sampleCount];
            minPinned[channel] = new PinnedArray(minBuffers);
            maxPinned[channel] = new PinnedArray(maxBuffers);


            try
            {

                //status = Imports.SetDataBuffers(_handle, (Imports.Channel)channel, maxBuffers, minBuffers, (int)sampleCount, segmentIndex, Imports.RatioMode.Average);
                status = Imports.SetDataBuffers(handle, (Imports.Channel)channel, maxBuffers, minBuffers, (int)sampleCount, segmentIndex, Imports.RatioMode.None);
                if (status != Imports.PICO_OK)
                {
                }


                int maxSamples;
                while (Imports.GetTimebase(handle, _timebase, (int)sampleCount, out timeInterval, _oversample, out maxSamples, 0) != 0)
                {
                    _timebase++;
                }

                /* Start it collecting, then wait for completion*/
                _ready = false;
                _callbackDelegate = BlockCallback;

                
                status = Imports.RunBlock(handle, 0, (int)sampleCount, _timebase, _oversample, out timeIndisposed, 0, _callbackDelegate, IntPtr.Zero);
                Thread.Sleep(300);

                
                int countRetry = 0;

                while ((!_ready) && (countRetry < 10))
                {
                    Thread.Sleep(1);
                    countRetry++;
                }

                Imports.Stop(handle);

                if (_ready)
                {

                    short overflow;
                    uint startIndex = 0;
                    uint downSampleRatio = 0;
                    int numR = 0;
                    status = 10;
                    long sumValueVoltage = 0;

                    while ((status != 0) && (numR < 3))
                    {
                        Application.DoEvents();
                        status = Imports.GetValues(handle, startIndex, ref sampleCount, downSampleRatio, Imports.RatioMode.None, segmentIndex, out overflow);
                        numR++;
                    }
                    
                    if (status == (short)Imports.PICO_OK)
                    {
                        try
                        {
                            if (type == 0)
                            {
                                for (i = 0; i < sampleCount; i++)
                                {
                                    mVReadPeriod[i] = adc_to_mv(maxPinned[channel].Target[i], (int)_channelSettings[(int)(channel)].range);
                                    minVoltageRead = mVReadPeriod.Min();
                                    maxVoltageRead = mVReadPeriod.Max();
                                }
                            }
                            if (type == 1)
                            {
                                for (i = 0; i < sampleCount; i++)
                                {
                                    mVReadTFallTRis[i] = adc_to_mv(maxPinned[channel].Target[i], (int)_channelSettings[(int)(channel)].range);
                                    
                                }
                            }
                            if (type == 2)
                            {

                                for (i = 0; i < sampleCount; i++)
                                {
                                    mVReadPeriod[i] = adc_to_mv(maxPinned[channel].Target[i], (int)_channelSettings[(int)(channel)].range);
                                    sumValueVoltage += adc_to_mv(maxPinned[channel].Target[i], (int)_channelSettings[(int)(channel)].range);
                                }
                                averageVoltageValue = (int)(sumValueVoltage / sampleCount);
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Console.WriteLine(ex.Message);

                        }
                    }
                }

                Imports.Stop(handle);
                foreach (PinnedArray p in minPinned)
                {
                    if (p != null)
                        p.Dispose();
                }
                foreach (PinnedArray p in maxPinned)
                {
                    if (p != null)
                        p.Dispose();
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }

If I don't change channel, I haven't problems, but I don't read correct values.

Thanks in advance

mosine
Newbie
Posts: 0
Joined: Thu Feb 09, 2017 2:03 pm

Re: Change Channel

Post by mosine »

For information: I solved my problem.
The solution: when I must change channel, I close (Imports.CloseUnit) communication and restart it.

Thanks

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Change Channel

Post by Martyn »

Thank you for letting us know :)
Martyn
Technical Support Manager

Post Reply