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

USB-1808 C++ Example - Continuous Analog Input & Output

Expand / Collapse
 

USB-1808 C++ Example - Continuous Analog Input & Output



The example program below demonstrates (using C++) using the USB-1808 to simultaneously stream analog input and output. 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 100

#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);
printf("Device Name: %s\n", DeviceDescriptor.ProductName);
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 = cbScaledWinBufAlloc(COUNT);
   //MemHandle = cbWinBufAlloc32(COUNT);

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

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


int timerNum = 0;
double freq = 2.0f;
double duty = 0.5f;
double delay = 5.0f;
int Numofpulses = 0;
//output one pulse to trigger board
ULStat = cbPulseOutStart( BoardNum,timerNum,&freq,&duty,Numofpulses,&delay,0,0);
if(ULStat != 0)
printf("%d",ULStat);

   //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);
   }


cbPulseOutStop(BoardNum,timerNum);
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.87 KB, 32 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50719

Last Modified:8/10/2018 1:18:41 PM

Article has been viewed 2,364 times.

Options