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

USB-1808 C++ Example - DaqInScan Analog,Digital & Encoder Input...

Expand / Collapse
 

USB-1808 C++ Example - DaqInScan Analog,Digital & Encoder Input


The example program below demonstrates (using C++) how to continuously read analog channels 0 - 3, digital port (4 bits) & an encoder channel. The data returned is in counts. To convert the analog channels to voltage see cbToEngUnits in the UL Help file.

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 DaqInScan.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 1000 //sample rate
#define ChanCount 6 //number of channels
#define PACKET 64
#define BUFFSIZE PACKET * ChanCount // buffer size
#define HALFBUF BUFFSIZE / 2
#define NUMROWS PACKET / 2
#define COUNTER 3  //counter channel number

#define MAXNUMDEVS 100

void main ()
   {
   /* Variable Declarations */
   int i=0;
   int j=0;
   int index = 0;
   int BoardNum = -1; 
   int ULStat = 0;

   int Gain = BIP10VOLTS; //voltage input range
   long rate = RATE;
   long Pre = 0;
   long count = BUFFSIZE;
   long curCount,curIndex;
   short Status;
   HANDLE MemHandle = 0;
   bool NextReadUpper = false; //flag to indicate which half of buffer to read

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

   unsigned int Options;  
   unsigned int CMODE;

   short ChanArray[ChanCount];  // DaqInScan configuration arrays
   short ChanTypeArray[ChanCount];
   short GainArray[ChanCount];

   unsigned long *ADData; //holds A/D counts

   float Rev = (float)CURRENTREVNUM;
   ULStat = cbDeclareRevision(&Rev);
   cbErrHandling(PRINTALL, STOPALL);
   printf ("Demonstration of cbDaqInScan() 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;
   }
 

   ChanArray[0] = 0;
   GainArray[0] = BIP10VOLTS;
   ChanTypeArray[0] = ANALOG_SE;

   ChanArray[1] = 1;
   GainArray[1] = BIP10VOLTS;
   ChanTypeArray[1] = ANALOG_SE;

   ChanArray[2] = 2;
   GainArray[2] = BIP10VOLTS;
   ChanTypeArray[2] = ANALOG_SE;

   ChanArray[3] = 3;
   GainArray[3] = BIP10VOLTS;
   ChanTypeArray[3] = ANALOG_SE;

   ChanArray[4] = AUXPORT;
   GainArray[4] = BIP10VOLTS;//range ignored
   ChanTypeArray[4] = DIGITAL;

   ChanArray[5] = COUNTER;
   GainArray[5] = BIP10VOLTS;//range ignored
   ChanTypeArray[5] = CTR;

   CMODE = ENCODER + ENCODER_MODE_X1 + ENCODER_MODE_RANGE_LIMIT_ON; //ENCODER_MODE_CLEAR_ON_Z_ON; //counter mode of operation
 

   if(cbCConfigScan( BoardNum,
COUNTER,
CMODE,
CTR_DEBOUNCE_NONE,
CTR_TRIGGER_AFTER_STABLE,
CTR_RISING_EDGE,
CTR_TICK20ns,
0)) printf("%d",ULStat);

   if(cbCLoad32(BoardNum,MINLIMITREG3,0)) printf("%d",ULStat);
   if(cbCLoad32(BoardNum,MAXLIMITREG3,2000))printf("%d",ULStat);


   MemHandle = cbWinBufAlloc32(BUFFSIZE);
   ADData = (unsigned long*) MemHandle;
   Options= BACKGROUND + CONTINUOUS ; 
   if(cbDaqInScan ( BoardNum, 
ChanArray,
ChanTypeArray,
GainArray,
ChanCount,
&rate,
&Pre,
&count, 
MemHandle, 
Options))printf("%d",ULStat);


   while(!_kbhit())
   {

if(cbGetStatus(BoardNum,&Status,&curCount,&curIndex,DAQIFUNCTION))
{
printf("Error Code %d\n",ULStat);
break;
}
if((curIndex > HALFBUF) && (NextReadUpper == false))
{   
NextReadUpper = true;
index = 0; //print lower half
for (j = 0; j< NUMROWS; j++)      
{
for(i=0;i<ChanCount;i++)
printf ("%6d\t",ADData[index++]);
printf ("\n");
}
}
else if((curIndex < HALFBUF) && (NextReadUpper == true))
{
NextReadUpper = false;
index = HALFBUF;//print upper half

for (j = 0; j< NUMROWS; j++)      
{
for(i=0;i<ChanCount;i++)
printf ("%6d\t",ADData[index++]);
printf ("\n");
}
}
Sleep(1);
   }

cbStopBackground(BoardNum,DAQIFUNCTION);
cbWinBufFree(MemHandle);
printf("Completed...press any key to exit\n"); 
getch();
}




Rate this Article:

Attachments


VC_2008_USB-1808 DaqInScan.zip VC_2008_USB-1808 DaqInScan.zip (352.87 KB, 55 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50720

Last Modified:8/10/2018 11:08:06 AM

Article has been viewed 2,873 times.

Options