Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

NI 5112 Simultaneous Sampling using C++

Hello Finomad,

You may have noticed that when using the SaveToFile example, if you try changing the channel in SaveToFile.c from "0" to "0:1", it gives you an error stating that you cannot do this.

This is why I wanted you to look at the code for the Simulated Acquisition, which CAN do multiple channels.  So what we'll do here is change some of the code in SaveToFile and be on our way:

1.  GenericSaveToFile.c has a check for more than one waveform, which gives you that error.  Comment that out:
//        if (numWaveform > 1)        // COMMENTED OUT BY BERTO
//            handleErr (-1);


2.  Now, the main problem if you try running now is that the niScope_Fetch function will crash.  This is because it doesn't know the right size of the Waveform, and cannot put data into it.  At this point, you can steal some code from the GenericSimulatedAcquisition.c file that can help just after the niScope_ActualRecordLength function:

      if (wfmInfoPtr)
         free (wfmInfoPtr);
      wfmInfoPtr = malloc (sizeof (struct niScope_wfmInfo) * numWaveform);
      if (waveformPtr)
         free (waveformPtr);
      waveformPtr = malloc (sizeof (ViReal64) * actualRecordLength * numWaveform);

Notice how this is a bit different than the one in the GenericSaveToFile.c since that one never multiplied it by the number of waveforms, as we do now above.

3.  Now we're at the point where niScope_Fetch won't blow up.  What you now need to do is look at the help file and see how the Fetch function places the two different channels:

Start >> Programs >> National Instruments >> NI-SCOPE >> Documentation >> NI High Speed Digitizers Help
and then check out Programming >> NI-SCOPE Function Reference Help >> Functions >> Acquisition Functions >> niScope_Fetch.

Check out the wfm parameter info.  As you can see, the data is going to be ch0,ch1,ch0,ch1,ch0,ch1, etc...

So now, when you're writing your file, you will need to take that into account.  Some ideas are to go two samples per line, with a tab between them, or to loop through the list twice and check for even and odd indices as you're looping, and have two long flat lists.

This should be enough to get that example working for you.
mike



0 Kudos
Message 11 of 17
(2,042 Views)

Hey Berto,

I made the changes that you have suggested. However, we can still only get one channel at a time.

Here are the canges that we made:

     // Find out the current number of waveforms
      handleErr(niScope_ActualNumWfms(vi, channelName, &numWaveform));
      // Do not support multiple channels in this example
   // To support multiple channels, the next two lines of code are commended out
      //if (numWaveform > 1)
      //   handleErr (-1);

      // Configure immediate triggering
      handleErr(niScope_ConfigureTriggerImmediate (vi));
 
      // Initiate the acquisition
      handleErr(niScope_InitiateAcquisition(vi));

      // Find out the current record length
      handleErr(niScope_ActualRecordLength (vi, &actualRecordLength));
  
      // Allocate space for the waveform and waveform info according to the record length and number of waveforms

   if (wfmInfoPtr)
         free (wfmInfoPtr);
      wfmInfoPtr = malloc (sizeof (struct niScope_wfmInfo) * numWaveform);
      if (waveformPtr)
         free (waveformPtr);
      waveformPtr = malloc (sizeof (ViReal64) * actualRecordLength * numWaveform);

 

 

And then, for the channel input, we used "0:1"

   int option;
   strcpy(channelName,"0:1");
   *verticalRange = 10.0;
   *minSampleRate = 2000000;
   *minRecordLength = 1000;

 

However, it only get the data from channel 0.

 

I was looking into one of the help files in NI-Digitizer help. It's called "Fetching Multiple-Record Acquisitions". It has some code that was similar to the one you gave but it has some other codes.  Here is that code:

// Set numWfms to 6, because the acquisition is for 2 channels times 3 records.
niScope_ActualNumWfms (vi, "0,1", &numWfms);

// Fetch the coerced record length
niScope_ActualRecordLength (vi, &actualRecordLength);

// Declare memory for the waveforms and waveform info structs
wfm = malloc (sizeof (ViReal64) * actualRecordLength * numWfms);
wfmInfo = malloc (sizeof (struct niScope_wfmInfo) * numWfms);

as you can see here, niScope_ActualNumWfms (vi, "0,1", &numWfms);  but which is not in the code you suggested. do we need to add that somehow

Do we have to change the ViInt32 numRecords = 1;   we changed that to a 2 but nothing happend.

what else can we do to make it work?

Thanks for the help, Berto. We really need it.

0 Kudos
Message 12 of 17
(2,027 Views)
Oh, yes, it looks like I missed that.  What is the update on your program now?
0 Kudos
Message 13 of 17
(2,011 Views)

Hey Berto,

No success yet. I have made the changes, however, we can still only get one channel at a time.

Here is how the code look so far:


   int option;
   strcpy(channelName,"0,1");
   *verticalRange = 10.0;
   *minSampleRate = 20000000;
   *minRecordLength = 1000;

 


      // Find out the current number of waveforms
      handleErr(niScope_ActualNumWfms(vi, channelName, &numWaveform));
      // Do not support multiple channels in this example
   // To support multiple channels, the next two lines of code are commended out
      //if (numWaveform > 1)
        // handleErr (-1);

      // Configure immediate triggering
      handleErr(niScope_ConfigureTriggerImmediate (vi));
 
      // Initiate the acquisition
      handleErr(niScope_InitiateAcquisition(vi));

      // Find out the current record length
      handleErr(niScope_ActualRecordLength (vi, &actualRecordLength));
  
      // Allocate space for the waveform and waveform info according to the record length and number of waveforms

   niScope_ActualNumWfms (vi, "0,1", &numWaveform);

   niScope_ActualRecordLength (vi, &actualRecordLength);

   if (wfmInfoPtr)
         free (wfmInfoPtr);
      wfmInfoPtr = malloc (sizeof (struct niScope_wfmInfo) * numWaveform);
      if (waveformPtr)
         free (waveformPtr);
      waveformPtr = malloc (sizeof (ViReal64) * actualRecordLength * numWaveform);

What do you think the problem might be? We really appreciate your help.

Thanks

0 Kudos
Message 14 of 17
(2,003 Views)
Finomad,

You are 99% there.  I am thinking that you are Fetching the data just fine, but not printing it out right.  I am including everything I've been working on, but I've been using simulated mode, so you'll have to realize that this uses niScope_initWithOptions.

Here's what I have for the filewriting part:

      if (fileHandle)
      {
          // Write the data
          printf("Number of Waveforms: %d\n", numWaveform);
          for (i = 0; i<actualRecordLength; i++)
          {
            fprintf(fileHandle,"%f\n",waveformPtr[i]);
          }
          waveformPtr = waveformPtr + wfmInfoPtr[0].actualSamples;   //  THIS IS OUR IMPORTANT LINE

          fprintf(fileHandle,"Time for next set\n");

          for (i = 0; i<actualRecordLength; i++)
          {
            fprintf(fileHandle,"%f\n",waveformPtr[i]);
            printf("Wrote %d\n",i);
          }
          waveformPtr = waveformPtr - wfmInfoPtr[0].actualSamples;
          // Do this to get the AsciiPlot happy again and pointing
      fclose(fileHandle);
      }

I figured this out by looking at the SimulatedAcquisition and realizing how it was calling AsciiPlot the second time after re-setting the waveformPtr as noted above.

I'll attach my VisualStudio project so that you can see the rest.

Thanks, and I appreciate all of your patience!
mike
0 Kudos
Message 15 of 17
(1,990 Views)
Berto,

I can't believe it. The code you gave works. Wow. Now we can get data from both channels at the same time and output that to a text file. We turned off the simulation as you have suggested before. So now we get the actual data. Now we are trying to analyze the data.

We would like to thank you for all your help. We could not have done it without you. Thanks again.
0 Kudos
Message 16 of 17
(1,975 Views)
Awesome!  Thanks for all of your patience finomad - we were SO close the entire time.  I cleaned up the example just a little bit and am making it public on the web so that it's out there for everyone.

Good luck in the rest of your project, hopefully you have time to get the analysis done.

mike
0 Kudos
Message 17 of 17
(1,966 Views)