Crazy behavior with streaming and max and pre Trigger sample

Post your .Net discussions here
pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Crazy behavior with streaming and max and pre Trigger sample

Post by pfeifelpdm »

Dear guys,

first of all, you have been build a very nice library with very good examples and a not that bad API.
The description of API is also usable, but not every time easy to understand.
However I still work for 2 month with the library and the Pico 4000 Series.

I wrote an .NET GUI to configure all 8 channels for streaming with the different possible configurations like range etc.
This application works so far so good.
But : One of my functions is to define record time before starting the streaming mode. Thats why I calculate the amount of expected data and use the RunStreaming like

Code: Select all

status = (PICO_INFO)Imports.RunStreaming(_handle, ref sampleInterval, sampleTimeUnits, maxPreTriggerSamples, maxPostTriggerSamples, setAutoStop, downSampleRatio, Imports.DownSamplingMode.None, (uint)tempBufferSize);
This also works fine, for almost all values (may be less than 8 digits), expect some special numbers less than uint.MaxValue.
And this is what confuses. Using uint.MaxValue [4294967295] for maxPreTriggerSamples, maxPostTriggerSamples and lots of 8-digits numbers let the program do what I expect.
Of I course I currently didn't test completly - because with two times uint.MaxValue it means run streaming for more than 6 days with 16KHz smapling rate. For the smaller amounts like 7504000 my tool works perfect.

Using e.g. 4294959999, 4294960000 has the result that Streaming Mode (esp. GetStreamingLastesValue) returns PICO_NOT_RESPONDING Code.
Another case is e.g. 2912552000 - while Pico is starting streaming the application just finish by vshost.exe with an APPCRASH in Kernelbase.dll.

At least I like to know :
1. Does somebody know, why the triggersamples with these specific number doesn't work?
2. Does somebody know, how I may handle the appcrash?

Thx for help
Best Uwe

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hi Uwe,

Which version of the ps5000a dll are you using?

If you are building a 32-bit application try the PS5000a and PicoIpp dll files from the PicoScope 6.12 installation directory and if you require the 64-bit equivalent, please e-mail support@picotech.com

Regards,

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh,

thx for your response. I still use a ps4000a dll instead a ps5000a.dll, since it is the Pico 4824.
However, I do not understand why the reason might explainable with a 32-bit vs. 64-bit library since uint is just a 32-bit value. By the way I did install both libraries (SDK).
By debugging the code the values overhanded to the library are less than unit.MaxValue as allready described.
If the library is the reason, than it shouldn't work at any time, right?

Best Uwe

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hi Uwe,

Apologies, I had mixed up your device with another thread that I had seen :oops:

It might be worth trying the 32-bit ps4000a and PicoIpp drivers from the latest PicoScope 6.12 build and seeing if the problem persists as we are aware that the drivers supplied with the current SDK are a little out of date (there would have been bug fixes since then).

If the problem persists, please post code that can be run here or e-mail it to support@picotech.com

Regards,

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh,

since I am not sure about understanding your recommendation, I ask some details again.

on the one hand I do not understand and I wouldn't expect a solution by changing the library, since the behavior is just with parameters in beetween the allowed values a uint32 defines. It is not explainable with my knowledge about data types.

on the other hand I analyze where the dll is located, since I cannot add the libraries directly to a .NET Visual Studio as a dynamic library. What you suggest? See my folders content.
Shell I replace all ps4000a.dll with the versions of Version of PicoScope v6.12? Am I able to download these libraries explicitly (because I don't want to install the PicoScope Software on the development environment)?
What is about the wrappers also located there?

Best Uwe

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh,

I think I allready did test the newer library without knowing it.
My environment looks like the following:
I use an dev system with MS Visual Studio 2015 and installed SDK for 32- and 64-bit with dll version 1.0.4.48 - as you can see in my post before.
The production environment has the PicoScope Software installed with the 32-bit version dll version 1.0.6.13 and have still the same crazy behavior, for some parameters less the unit.MaxValue.

Best Uwe

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

I just modified few line in your existing Example PS4000AStreamingCon.
As I wrote if you set max and minPostTriggerSamples to uint.MaxValue ... no Problem after Start.
If you use Code like now, PICO becomes "busy" ...

Code: Select all

void StreamDataHandler(uint preTrigger)
        {
            DateTime start, stop, curr;
            int numberOfLoops = 0, counter = 0;
            int tempBufferSize = 1024 * 100; /*  *100 is to make sure buffer large enough */

            appBuffers = new short[_channelCount * 2][];
            buffers = new short[_channelCount * 2][];

            short setAutoStop = 0; // before it was 1
            
            uint totalSamples = 0;
            uint triggeredAt = 0;
            uint sampleInterval = 1;
            uint downSampleRatio = 1;
            uint status;

            uint maxPostTriggerSamples = uint.MaxValue; // 1000000 - preTrigger;
            uint minPostTriggerSamples = uint.MaxValue; // will replace preTrigger

            maxPostTriggerSamples = 4294960000; // maxPostTriggerSamples;
            minPostTriggerSamples = 4294960000; // minPostTriggerSamples;

            // Use Pinned Arrays for the application buffers
            PinnedArray[] appBuffersPinned = new PinnedArray[_channelCount * 2];

            for (int ch = 0; ch < _channelCount * 2; ch += 2) // create data buffers
            {
                if (_channelSettings[ch / 2].enabled == true)
                {
                    buffers[ch] = new short[tempBufferSize];
                    buffers[ch + 1] = new short[tempBufferSize];

                    appBuffers[ch] = new short[tempBufferSize];
                    appBuffers[ch + 1] = new short[tempBufferSize];

                    appBuffersPinned[ch] = new PinnedArray(appBuffers[ch]);
                    appBuffersPinned[ch + 1] = new PinnedArray(appBuffers[ch + 1]);

                    status = Imports.SetDataBuffers(_handle, (Imports.Channel)(ch / 2), buffers[ch], buffers[ch + 1], tempBufferSize, 0, Imports.DownSamplingMode.None);
                }
            }

            Console.WriteLine("Waiting for trigger...Press a key to abort");
            _autoStop = false;

            status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, minPostTriggerSamples, maxPostTriggerSamples, setAutoStop, downSampleRatio,
                                                Imports.DownSamplingMode.None, (uint)tempBufferSize);
            
            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 enabled Channels, results shown are....");
            //writer.WriteLine();
            //writer.WriteLine("Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV");
            //writer.WriteLine();
            writer.WriteLine("Started" + DateTime.Now.ToLongTimeString() + "." + DateTime.Now.Millisecond + "\t");
            writer.Write("Time1\t\t\tidx\tsc\t");
            for (int ch = 0; ch < _channelCount; ch++)
            {
                if (_channelSettings[ch].enabled)
                {
                    writer.Write("Ch  Max ADC    Max mV   Min ADC    Min mV   ");
                }
            }
            writer.WriteLine();

            while (!_autoStop && !Console.KeyAvailable)
            {
                // changed by @uk 
                numberOfLoops++;

                /* Poll until data is received. Until then, GetStreamingLatestValues wont call the callback */
                Thread.Sleep(0);
                _ready = false;

                start = DateTime.Now;
                status = Imports.GetStreamingLatestValues(_handle, StreamingCallback, IntPtr.Zero);
                stop = DateTime.Now;               
                
                Console.Write((status > 0 && status != 39 /*PICO_BUSY*/) ? "Status =  {0}\n" : "", status);

                if (_ready && _sampleCount > 0) /* can be ready and have no data, if autoStop has fired */
                {
                    if (_trig > 0)
                    {
                        triggeredAt = (uint)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);
                    }

                    curr = start;
                    for (uint i = _startIndex; i < (_startIndex + _sampleCount); i++)
                    {
                        // changed by @uk 
                        counter++;
                        curr = curr.AddMilliseconds(1);
                        writer.Write("[" + counter + "]\t" + curr.ToLongTimeString() + "." + curr.Millisecond + "\t");
                        writer.Write(numberOfLoops + "\t" + i + "\t" + _sampleCount + "\t");
                        for (int ch = 0; ch < _channelCount * 2; ch += 2)
                        {
                            if (_channelSettings[ch / 2].enabled)
                            {
                                writer.Write("Ch{0} {1,7}   {2,7}   {3,7}   {4,7}   ",
                                                            (char)('A' + (ch / 2)),
                                                            appBuffersPinned[ch].Target[i],
                                                            adc_to_mv(appBuffersPinned[ch].Target[i], (int)_channelSettings[(int)(Imports.Channel.CHANNEL_A + (ch / 2))].range),
                                                            appBuffersPinned[ch + 1].Target[i],
                                                            adc_to_mv(appBuffersPinned[ch + 1].Target[i], (int)_channelSettings[(int)(Imports.Channel.CHANNEL_A + (ch / 2))].range));
                            }
                        }
                        writer.WriteLine();
                    }
                }
            }

            if (Console.KeyAvailable)
            {
                Console.ReadKey(true); // clear the key
            }

            writer.WriteLine("Stopped : " + DateTime.Now.ToLongTimeString() + "." + DateTime.Now.Millisecond + "\t");

            Imports.Stop(_handle);
            writer.Close();

            if (!_autoStop)
            {
                Console.WriteLine();
                Console.WriteLine("Data collection aborted - press any key to continue.");
                WaitForKey();
            }
        }

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hi Uwe,

Thank you for the code.

I have reported a bug to our Development Team who will investigate further.

Regards,

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh,

two questions :
1. Are there updates regarding the issue?

I did also lot's of analyzes, especially with the issue of an APPCRASH.
The reason might be a error or exception inside the dll which not has been handled.

For some older Win32 Code / Libraries, there is a Attribute available which can be used like :

Code: Select all

[DllImport(_DRIVER_FILENAME, EntryPoint = "ps4000aRunStreaming", SetLastError = true)]
        public static extern UInt32 RunStreaming(
                                                    short handle,
                                                    ref uint sampleInterval,
                                                    ReportedTimeUnits sampleIntervalTimeUnits,
                                                    uint maxPreTriggerSamples,
                                                    uint maxPostPreTriggerSamples,
                                                    short autoStop,
                                                    uint downSampleRatio,
                                                    DownSamplingMode downSamplingRatioMode,
                                                    uint overviewBufferSize);
But however a try/catch around this function call doesn't take any effect.

Now my 2nd question:
Is there a exception/error handling offered by this API? Or otherwise. May I can get the source code/project/extension (if somehow available) to enable exeption handling or generate somehow an wrap mechanism?

Best Uwe

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hi Uwe,

I've escalated the bug report.

Unfortunately, we are unable to provide source code, but you could create a wrapper if you wish. The status codes will indicate what the issue is.

Update: I have been advised that the number of samples is too large for the driver (corresponds to about 8GB) so please reduce the number of samples (probably less than 4 GB in total). There might be an update to guard against this scenario but I cannot advise on timescales.

Regards,

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Thank you for this advice - I will limit to 4G.
Does this mean, that I am only able to record around 4.6 hours using 8 channels with 16kHz sample frequency, and half this time with 32kHz?

Regarding the wrapper: Since I don't get status codes in AppCrash cases, I am not able to handle that with a wrapper any how.

Best Uwe

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh,

again thx for the advice. I started to limit the stream time to get only 4GB data.
But again the issue occurs, that the PICO does not work as expected through the API.

Lets calculate : 4GB => 4*1024MB = 4*1024*1024KB => 4*1024*1024*1024Bytes = 4294967296 Bytes = (Uint32.MaxValue +1)
Each value consumes 2Bytes because it is a short(Uint32).

Assuming we want to use sample frequency 16000kHz means :
maxValue / f / channelCount / shortsize = 42949672965 / 16000 / 2 / 2

if I didn't made a mistake i should be able to record 67108 seconds. right?
That means 67108 * 16000 = 1073728000 as value for parameter maxPreTriggerSamples either setting maxPreTriggerSamples to 536864000 and maxPostTriggerSamples 536864000 as well.

But as discussed before PICO_NOT_RESPONDING.
What is wrong?

Best Uwe

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hello Uwe,

By 4GB, I mean the total number of bytes across all channels. With a 2-byte data type (UInt16 or ushort), this corresponds to 2 GS shared across all channels.

With 1 channel enabled, 1 GS at 16 kHz sampling frequency would be equal to ~ 67108 seconds.

However, even with 1 GS in total, this is still 2 GB which would be too much for a 32-bit driver. With a 64-bit driver, you can specify 536870912 samples for pre-trigger and post-trigger.

When testing with a 32-bit driver, I found in testing that if I halve the above value to 268435456, then this will work. In total, you probably do not want to specify more than 1.5 GB (750 MS) in total when using a 32-bit driver.

Hope this helps,

pfeifelpdm
Newbie
Posts: 1
Joined: Tue Feb 14, 2017 3:12 pm

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by pfeifelpdm »

Dear Hitesh, thx
Yes this helps.

Just a question regarding the driver version.

Lets asume I didnt install PICO 6, but both SDK's 32bit and 64bit.
Which library is beeing used?
I got the experience it does not matter if I deploy any dll within the project, because I cannot add a reference to the project.

I would expect in case I develop a 64bit app than it will use the 64 version, in all other cases e.g.32-bit or AnyCPU the 32 bit-version. Am I right?

Best Uwe

Hitesh

Re: Crazy behavior with streaming and max and pre Trigger sa

Post by Hitesh »

Hi Uwe,

The dll locations are added to the Windows PATH environment variable when you install the PicoSDK.

If you are building for an x86 platform, the application will pick up the dll from the 32-bit PicoSDK install lib folder.

If you build for an x64 platform, the application will pick up the dll from 64-bit PicoSDK install lib folder.

If you use the Any CPU setting, I suspect it will try to pickup the 64-bit dll on a 64-bit operating system based on previous experience.

Regards,

Post Reply