Output buffer contains only zeros

Post general discussions on using our drivers to write your own software here
Post Reply
herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Output buffer contains only zeros

Post by herr_apa »

I have written a Windows application that reads out a picoscope periodically and saves the output to file. The picoscope model is 4824 and I'm using the 4000a API. For certain readout settings, everything works fine. For others, the readout buffer contains only zeros. The problem appears to be related to the sample interval, block length and readout period (see figure). I have a couple of questions. Any function and variable names used below are taken from the programmer's guide.
picoLoggerExplanation.png
1) I would expect my block length to be [Sample interval]*[nSamples - 1]. For example, 1001 samples with a sample interval of 1000 ms ought to yield a block length of 1 s. If I let the picoscope calculate the time it will spend collecting samples by checking the timeIndisposedMs variable of ps4000aRunBlock, the result is significantly greater (typically 1.4 s). Why is it so much greater? Is there a formula for estimating timeIndisposedMs analytically, without calling ps4000aRunBlock? For example, the time does not appear to be linear with the number of samples.

2) If I choose my settings such that timeIndisposedMs is smaller that my readout period, everythin works fine. If timeIndisposedMs is significantly greater than the readout period, such as in 1), then my buffers are filled with zeros after calling ps4000aGetValues. I figured the problem might be that the picoscope clears the buffers when starting to capture a new block, so I divided the picoscope memory into segment by calling ps4000aMemorySegments and related functions. However, the behavior remained the same. Why am I only getting zeros?

The relevant source file is attached in case it is needed. Thanks in advance!

//
Anders
Attachments
code.tar.gz
(6.22 KiB) Downloaded 479 times

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Anders,

The timeIndisposedMs parameter is used to indicate how long the device will spend collecting data but will also include time taken to setup the device for data collection as the settings have to be written down to the device. It will not take into account time spent waiting for a trigger.

When you get zero values, do you get a status code of 0x25 (PICO_NO_SAMPLES_AVAILABLE) when calling GetValues()? If so, that indicates that you are requesting data before the device is ready.

Once you call the RunBlock() function, you need to monitor the value of a flag that is normally set in the callback. Once this flag indicates that data is available, you can then retrieve data values from the device.

I hope this helps.

herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Re: Output buffer contains only zeros

Post by herr_apa »

Hi Hitesh,

Thanks for your reply! It turns out that when I get zeros in the buffer, GetValues returns 0x43 (PICO_DRIVER_FUNCTION). The status description reads:

"A driver function has already been called and not yet finished. Only one call to the driver can be made at any one time."

Perhaps you can help me understand why this happens. When the program is running, it executes an infinite loop that calls RunBlock and then sleeps for [readout period] ms. The callback function (the one passed to RunBlock) then calls GetValues. As far as I can see, RunBlock and GetValues are the only two driver functions that are ever called once the program is up and running.

The only possibility I can think of is that I'm calling GetValues very soon after calling RunBlock. Suppose the block settings are such that GetValues is called 1100 ms after RunBlock. Then, if my readout period is 1000 ms, there's only a 100 ms window between the RunBlock call for a given readout and the GetValues call for the previous readout. But if that were the case, I would expect the problem to go away if I increased the block length. It doesn't. As soon as I hit the problem, it doesn't matter how much longer I make my block. I still get PICO_DRIVER_FUNCTION.

Do you understand why I get this status?

//
Anders

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Anders,

We would not recommend calling any driver functions from within the callback function.

Referring to the Programmer's Guide:
This callback function receives a notification when block-mode data is ready.
If you wish to use this feature, you must create this function as part of your application. You
register it with the ps4000a driver using ps4000aRunBlock(), and the driver calls it back when a
capture is complete. You can then download the data using the ps4000aGetValues() function.
As per our examples, you should use the callback to set a flag value to indicate that the device has completed data collection.

Code: Select all

/****************************************************************************
* Block Callback
* used by ps4000a data block collection calls, on receipt of data.
* used to set global flags etc checked by user routines
****************************************************************************/
void PREF4 CallBackBlock( int16_t handle, PICO_STATUS status, void * pParameter)
{
	if (status != PICO_CANCELLED)
	{
		g_ready = TRUE;
	}
}
Once the flag indicates that the device has data, you should then call ps4000aGetValues() in your application to retrieve the data.

Regards,

herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Re: Output buffer contains only zeros

Post by herr_apa »

Hi Hitesh,

I rewrote the program according to your recommendations. The callback only sets a flag. This flag is checked by a separate thread in an infinite loop. The problem persists.

I started looking at the status codes of various driver functions. ps4000aRunBlock returns 0. However, it turns out that the "status" parameter of my ps4000aBlockReady callback is 0x3A (PICO_CANCELLED). If I subsequently try to call ps4000aGetValues, then the status is 0x25 (PICO_NO_SAMPLES_AVAILABLE) as you suggested.

At this point, the question is why the callback would report PICO_CANCELLED. Do you have any ideas?

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Andres,

Are you calling ps4000Stop() or terminating the data collection early before calling ps4000GetValues()?

It might be worth seeing the updated code.

Regards,

herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Re: Output buffer contains only zeros

Post by herr_apa »

Hi Hitesh,

The code is attached.

I experimented with ps4000Stop (see line 424) since I saw it in the examples, but it did not solve my problem. Even after calling ps4000Stop, the buffer contained only zeros. I don't believe that I'm terminating the data collection prematurely. A typical use case is calling RunBlock every 1000 ms with a timeIndisposedMs of ~1400. Since I use several memory segments, there shouldn't be a problem as far as I understand.
Attachments
MainForm.zip
(6.5 KiB) Downloaded 334 times

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Andres,

Referring to this section of code in your example:

Code: Select all

void checkReadout()
    {
      while (_acquireData)
      {
        Thread.Sleep(100); //Still experimenting with this. If the sleep time is too low, GetValues returns PICO_DRIVER_FUNCTION.
        for (uint segment = 0; segment < _nSegments; ++segment) if (_readoutIsReady[segment]) doReadout(segment);
      }
    }
I would suggest querying the _readoutIsReady[segment] in a loop until it is true. When you call Imports.GetValues() is _readoutIsReady[segment] equal to true?

Regards,

herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Re: Output buffer contains only zeros

Post by herr_apa »

Hi Hitesh,

I commented out Thread.Sleep(100) so that the loop does nothing but query _readoutIsReady[segment]. This made no difference.

When the problem is present, I never call Imports.GetValues() at all because the callback function aborts once it sees that the status is PICO_CANCELLED. If I ignore the status or set up the parameters such that I don't have the problem (e.g. with a long readout period), then _readoutIsReady[segment] is indeed equal to true.

Perhaps it is faster if you can experiment with the code directly. I have attached a full VS solution that should (hopefully) compile out of the box and work with a picoscope model 4824.

//
Anders
Attachments
picoLogger4000a.zip
(53.4 KiB) Downloaded 344 times

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Anders,

Please try this in your code:

Code: Select all

void checkReadout()
    {
      while (_acquireData)
      {
                //Thread.Sleep(100); //Still experimenting with this. If the sleep time is too low, GetValues returns PICO_DRIVER_FUNCTION.
                for (uint segment = 0; segment < _nSegments; ++segment)
                {
                    while (_readoutIsReady[segment] == false)
                    {
                        Thread.Sleep(100);
                    }

                    if (_readoutIsReady[segment])
                    {
                        doReadout(segment);
                    }
                }
                
      }
    }
You need to poll the value of _readoutIsReady[segment] until it indicates that data is ready for retrieval. There is likely to be latency, particularly when waiting for a trigger so you should not try to retrieve data until confirmation has been received that it is available.

I did notice when stepping through the code that the threads seemed to be going out of sequence. Please make sure that you call RunBlock -> Poll the driver then repeat. Retrieving data values during data collection is not possible so you can choose to retrieve them after one capture is complete before capturing the next waveform or you can wait till the end if you are segmenting the memory.

Hope this helps.

herr_apa
Newbie
Posts: 0
Joined: Mon Jun 11, 2018 12:39 pm

Re: Output buffer contains only zeros

Post by herr_apa »

Hi Hitesh,

Perhaps I have misunderstood what is and isn't possible to do via segmenting. I understand that the following sequence is possible:

Start capturing in segment 1.
Finish capturing in segment 1.
Start capturing in segment 2.
Finish capturing in segment 2.
Read out segment 1.
Read out segment 2.

What about this one? It starts capturing in segment 2 before segment 1 is finished. I'm starting to suspect this is where the problem lies.

Start capturing in segment 1.
Start capturing in segment 2.
Finish capturing in segment 1.
Finish capturing in segment 2.
Read out segment 1.
Read out segment 2.

And finally, what about this one? It captures in multiple segments simultaneously and also reads out one segment while another segment is still capturing. This is what I'm ultimately trying to accomplish.

Start capturing in segment 1.
Start capturing in segment 2.
Finish capturing in segment 1.
Read out segment 1.
Finish capturing in segment 2.
Read out segment 2.

//
Anders

Hitesh

Re: Output buffer contains only zeros

Post by Hitesh »

Hi Anders,

Unfortunately it is only possible to capture into segments sequentially i.e. capture into the first segment specified by the call to RunBlock(), then into the next segment specified and so on.

You can wait to retrieve data until after the data capture is complete, but it is not possible to retrieve data values while a data capture is in progress.

Regards,

Post Reply