Test and Measurement Forum

Returned value is -12851

Post your C and C++ discussions here

Returned value is -12851

Postby mcsquared2718 » Mon Sep 26, 2011 12:46 pm

Hi,

Is there a special significance attached to the value -12851 in the c driver code?
I have a program based around the example code for the 5204 scope which streams data from the scope at 0.6MS/s into a buffer. Every 100ms, new values are requested from the driver which updates the buffer. The new values are then written to disk in binary format. (Code copied below). At the end I have a few lines of code to open the binary files and write the first few (10000) samples to a text file so that I can check them out. It appears to work fine most of the time. Sometimes, however, after a certain point, correct values stop being returned and are replaced by the value -12851. I have noticed that this frequently occurs at the 8192 sample. (since this is 32768/4 this surely cannot be a coincidence?)

In other test programs I have made, I have noticed this value appearing a lot in the buffer for data min when aggregation is enabled. But what does it mean and why do I see it when I am using a single buffer with aggregation set to 1. I note that others seem to have recorded this value too. For example in the following post: post17943.html?hilit=12851#p17943

In the code below, a single buffer is registered with each channel using ps5000SetDataBuffer, which I believe is valid since DownSampleRatio is set to 1 when ps5000RunStreaming is called so aggregation is not enabled.

If anyone has any ideas they would be gratefully received.
Thanks in advance.

Code: Select all
   unsigned long bufferSize = 30000000;  // size of the software buffer is 30MB
   FILE * fpA, * fpB;
   FILE * fp1, * fp2;
   short * buffers[2];
   PICO_STATUS status;
   unsigned long sampleInterval = 1670;

   for (i = 0; i < unit->ChannelCount; i++) // create data buffers
   {
      buffers[i] = (short*) malloc(bufferSize * sizeof(short));
      ps5000SetDataBuffer(unit->handle, (PS5000_CHANNEL) i, buffers[i], bufferSize);
   }

   printf("Waiting for trigger...Press a key to abort\n");
   g_autoStop = FALSE;
   status = ps5000RunStreaming(unit->handle, &sampleInterval, PS5000_NS,
                               preTrigger, 30000000 - preTrigger, FALSE, 1,
                               bufferSize);
   if (status != PICO_OK)
   {
      printf("Streaming Mode: fail to call run_streaming successfully \n");
      return;
   }

   printf("Streaming data...Press a key to abort\n");

   fpA = fopen("channelA.bin", "w");
   fpB = fopen("channelB.bin", "w");
   while (!_kbhit() && !g_autoStop)
   {
      /* Poll until data is received. Until then, GetStreamingLatestValues wont call the callback */
      Sleep(100);
      g_ready = FALSE;
      status = ps5000GetStreamingLatestValues(unit->handle,
                                              CallBackStreaming, NULL);

      if (g_ready && g_sampleCount > 0) /* can be ready and have no data, if autoStop has fired */
      {
         printf("Collected %i samples, index = %i\n", g_sampleCount, g_startIndex);

      fwrite(&buffers[0][g_startIndex], sizeof(short), g_sampleCount, fpA);
      fwrite(&buffers[1][g_startIndex], sizeof(short), g_sampleCount, fpB);
      }
   }

   ps5000Stop(unit->handle);
   fclose(fpA);
   fclose(fpB);

   if (!g_autoStop) {
      printf("data collection aborted\n");
      _getch();
   }

   for (i = 0; i < unit->ChannelCount; i++) {
      free(buffers[i]);
   }


   // Read out from the binary file and save as text
   printf("Opening binary file and saving as text...\n");
      for (i = 0; i < unit->ChannelCount; i++) // create data buffers
   {
      buffers[i] = (short*) malloc(bufferSize * sizeof(short));
   }
   fpA = fopen("channelA.bin", "r");
   fpB = fopen("channelB.bin", "r");
   fp1 = fopen("channelA.txt","w");
   fp2 = fopen("channelB.txt","w");
   fread(buffers[0], sizeof(short), 10000, fpA);
   fread(buffers[1], sizeof(short), 10000, fpB);

   for (i = 0; i < 10000; i++) {
      fprintf(fp1,"%i, %d\n", i, buffers[0][i]);
      fprintf(fp2,"%i, %d\n", i, buffers[1][i]);
   }
   fclose(fpA);
   fclose(fpB);
   fclose(fp1);
   fclose(fp2);
   for (i = 0; i < unit->ChannelCount; i++) {
      free(buffers[i]);
   }
mcsquared2718
Newbie
 
Posts: 0
Joined: Wed Aug 24, 2011 3:30 pm

Re: Returned value is -12851

Postby Martyn » Tue Sep 27, 2011 10:09 am

What type of signal, signal level, are you measuring and how have you set up your channels ?
Martyn
Technical Specialist
Martyn
Site Admin
Site Admin
 
Posts: 2285
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Returned value is -12851

Postby mcsquared2718 » Tue Sep 27, 2011 1:40 pm

The signal is a 0-5v (actually more like 0-4.8v so it doesn't actually go out of the 5v voltage range) 1khz square wave (from the probe test on an oscilloscope).

The oscilloscope is set up as default by the 5000 series example code: so as follows:
for (i = 0; i < MAX_CHANNELS; i++) {
unit.channelSettings[i].enabled = TRUE;
unit.channelSettings[i].DCcoupled = TRUE;
unit.channelSettings[i].range = PS5000_5V;
}
Which then get set into the scope when the setDefaults function is called in the CollectStreamingImmediate function.

I have made some further observations about the behaviour of the system:

When ps5000SetDataBuffers is called it appears to initialise all values in the buffer to -12851.
When ps5000RunStreaming is called the values in the buffer start being modified. (even before ps5000GetStreamingLatestValues is called).
When only one of the channels is connected to the signal and the other is left floating the problem with erroneous -12851 values being returned is only observed on the channel connected to the signal - The channel which is left unconnected seems to operate fine. (I attach a sample text file which reports 30000 values from the saved binary file. The format for the file is SampleNumer, ChannelA_adcCount, ChannelB_adcCount. Scroll down to sample 28672 to see what I'm talking about. In this case the signal is attached to channel A)

Another possibility is that there is some sort of issue with the way in which I have written the buffer to disk using the fwrite and read it using the fread c commands. I suspect this because the point at which the erroneous values appear seems to scale with the number of samples which I write or read from the file. But I can't see where the problem is in the code which I included in my original post....

One further question is: When ps5000GetStreamingLatestValues is called is it guaranteed that there will be g_sampleCount samples available in the buffers for ALL channels.
Attachments
channelAandB.txt
(480.36 KiB) Downloaded 152 times
mcsquared2718
Newbie
 
Posts: 0
Joined: Wed Aug 24, 2011 3:30 pm

Re: Returned value is -12851

Postby mcsquared2718 » Tue Sep 27, 2011 5:16 pm

So I think I've found the problem now.

When I called fopen I used fopen("channelA.bin", "w") instead of fopen("channelA.bin", "wb");

*Expletives*

Sorry for the trouble and thanks again.
mcsquared2718
Newbie
 
Posts: 0
Joined: Wed Aug 24, 2011 3:30 pm


Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests