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

USB-1616HS Microsoft Visual C DaqInScan example

Expand / Collapse
 

USB-1616HS Microsoft Visual C DaqInScan example


The example program below demonstrates (using C++) how to continuously read analog channels 0 - 3, digital port (8 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. The project is a Win32 Console app...

// 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 100 //sample rate
#define ChanCount 7 //number of channels
#define PACKET 100
#define BUFFSIZE PACKET * ChanCount // buffer size
#define HALFBUF BUFFSIZE / 2
#define NUMROWS PACKET / 2
#define COUNTER 0  //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 short *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-2523 =0xB1
//Product ID for USB-2527 =0xB2
//Product ID for USB-2533 =0xB3
//Product ID for USB-2537 =0xB4
//Product ID for USB-1616HS   =0xCB
//Product ID for USB-1616HS-2 =0xCC
//Product ID for USB-1616HS-4 =0xCD
//Product IDs can be found in ULProps.txt located in 
// C:\Program Files (x86)\Measurement Computing\DAQ

if(DeviceDescriptor.ProductID == 0xB2)
{
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] = FIRSTPORTA;
   GainArray[4] = BIP10VOLTS;//range ignored
   ChanTypeArray[4] = DIGITAL8;

   //32 bit encoder input uses two 16 bit channels
   ChanArray[5] = COUNTER;
   GainArray[5] = BIP10VOLTS;//range ignored
   ChanTypeArray[5] = CTR32LOW;

   ChanArray[6] = COUNTER;
   GainArray[6] = BIP10VOLTS;//range ignored
   ChanTypeArray[6] = CTR32HIGH;

   CMODE = ENCODER + ENCODER_MODE_X1; 
 

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

  

   MemHandle = cbWinBufAlloc(BUFFSIZE);
   ADData = (unsigned short*) 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


VC2008_USB-1616HS DaqInScan.zip VC2008_USB-1616HS DaqInScan.zip (352.92 KB, 481 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50747

Last Modified:1/22/2018 2:16:13 PM

Article has been viewed 2,899 times.

Options