Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

USB-1808 C++ Example - Continuous Analog Input

Expand / Collapse
 

USB-1808 C++ Example - Continuous Analog Input


The example program below demonstrates (using C++) how to continuously read channels 0 - 3. It also demonstrates device discovery which doesn't require the user to run InstaCal. InstaCal must still be installed because doing so installs the device drivers. At the bottom of this article you will find a Zip file that contains the Visual Studio 2008 project.

// VC_2008_USB-1808_Continuous.cpp : Defines the entry point for the console application.
#include "stdafx.h"


/* Include files */
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "cbw.h"

#define RATE 100
#define LowChan 0
#define HighChan 4
#define ChanCount 5
#define PACKET 64
#define COUNT PACKET * ChanCount

#define MAXNUMDEVS 100

void main ()
   {
   /* Variable Declarations */
   int i=0;
   int j=0;
   int index = 0;
   long curCount,curIndex;

   int ULStat = 0;

   int Gain = BIP10VOLTS;
   long Rate = RATE;
   short Status;
   long halfbuf = COUNT/2;
   long NumRows = PACKET/2;
   bool NextReadUpper = false;


   int numberOfDevices = MAXNUMDEVS;
   DaqDeviceDescriptor inventory[MAXNUMDEVS];
   DaqDeviceDescriptor DeviceDescriptor;

   int BoardNum = -1;
 
   float Rev = (float)CURRENTREVNUM;
   ULStat = cbDeclareRevision(&Rev);
   cbErrHandling(PRINTALL, STOPALL);

   printf ("Demonstration of cbAInScan() in BACKGROUND mode\n\n");

//Ignore InstaCal device discovery
cbIgnoreInstaCal();

//locate USB devices
ULStat = cbGetDaqDeviceInventory(USB_IFC, inventory, &numberOfDevices);
for( i = 0; i < numberOfDevices; i++)
{
DeviceDescriptor = inventory[i];

//Product ID for USB-1808 = 0x13D
//Product ID for USB-1808X = 0x13E
//Product IDs can be found in ULProps.txt located in 
// C:\Program Files (x86)\Measurement Computing\DAQ

if(DeviceDescriptor.ProductID == 0x13E)
{
BoardNum = i;
ULStat = cbCreateDaqDevice(BoardNum, DeviceDescriptor);
break;
}
}

   if(BoardNum < 0)
   {
   printf("USB device not found...press any key to exit\n");
   getch();
   return;
   }

   //set all eight channel to single ended common ground; other option is DIFFERENTIAL
   for(i=0; i<8;i++)
   cbAInputMode(BoardNum,SINGLE_ENDED);

   //allocate buffer
   HANDLE MemHandle = 0;
   MemHandle = cbWinBufAlloc32(COUNT);

   double ScaledData[COUNT];
   unsigned long RawData[COUNT];  //read counts

   unsigned Options= BACKGROUND + CONTINUOUS + SCALEDATA;
   //unsigned Options= BACKGROUND + CONTINUOUS; //read counts

   //Start acquisition
   ULStat = cbAInScan ( BoardNum, 
LowChan, 
HighChan, 
COUNT, 
&Rate,
Gain, 
MemHandle, 
Options);
   if(ULStat != 0)
printf("%d",ULStat);


   while(!_kbhit())
   {
   //this loop reads the low half of the buffer then the upper half continuously.
   //it uses a NextReadUpper flag so that each half of the buffer is read once

ULStat = cbGetStatus(BoardNum,&Status,&curCount,&curIndex,AIFUNCTION);
if(ULStat != 0){
printf("Error Code %d\n",ULStat);
break;
}
if((curIndex > halfbuf) && (NextReadUpper == false))
{   
NextReadUpper = true;
index = 0;

cbScaledWinBufToArray(MemHandle, ScaledData, 0, halfbuf);
//cbWinBufToArray32(MemHandle, RawData, 0, halfbuf); //read counts

for (j = 0; j< NumRows; j++)      
{
for(i=0;i<ChanCount;i++)
{
printf ("%2.2f\t",ScaledData[index++]);
//printf ("%lu\t",RawData[index++]); //read counts
}
printf ("\n");
}
}
else if((curIndex < halfbuf) && (NextReadUpper == true))
{
NextReadUpper = false;
index = 0;
cbScaledWinBufToArray(MemHandle, ScaledData, halfbuf, halfbuf);
//cbWinBufToArray32(MemHandle, RawData, halfbuf, halfbuf); //read counts
for (j = 0; j< NumRows; j++)      
{
for(i=0;i<ChanCount;i++)
printf ("%2.2f\t",ScaledData[index++]);
//printf ("%lu\t",RawData[index++]); //read counts
}
printf ("\n");
}

}
Sleep(1);
   }

cbStopBackground(BoardNum,AIFUNCTION);
cbWinBufFree(MemHandle);
cbReleaseDaqDevice(BoardNum);

printf("Completed...press any key to exit\n"); 
getch();
}





Rate this Article:

Attachments


VC_2008_USB-1808_Continuous.zip VC_2008_USB-1808_Continuous.zip (352.75 KB, 152 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50717

Last Modified:8/10/2018 10:44:04 AM

Article has been viewed 2,571 times.

Options