Windows C Program USB-1608G Continuous with Digital IO


This an example specific to the USB-1608G. It uses the Discovery interface to locate the USB-1608G and program it. Running InstaCal is not necessary but you must have it installed. The program is a simple 32-bit console program created in Visual Studio.  To run the program, create a 32-bit Console project, add the cbw.h and cbw32.dll files to it along with the code file, which is attached at the bottom of this article. The cbw.h and cbw32.dll can be found in C:\Users\Public\Documents\Measurement Computing\DAQ\C\.

The program not only read four analog inputs in a continuous fashion, but also programs the first four digital lines as input and the second four as outputs. In the loop that reads the data, the digital inputs are read and then written out to the four output lines. This method works well for slow speed acquisitions however, for high speed care must be taken to assure data is not lost while read/writing the digital IO. This because the digital IO is a bottleneck with a maximum update rate of 200 reads or write per second.

/* 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 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(ANY_IFC, inventory, &numberOfDevices);
for (int i = 0; i < numberOfDevices; i++)
{
DeviceDescriptor = inventory[i];

//Product ID for 1608G = 0x110
//Product ID for 1608GX = 0x111
//Product ID for 1608GX-2AO = 0x112
//Product IDs can be found in ULProps.txt located in 
// C:\Program Files (x86)\Measurement Computing\DAQ
if (DeviceDescriptor.ProductID == 0x110)
{
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;
}

for (int i = 0; i < 4; i++)
ULStat = cbDConfigBit(BoardNum, AUXPORT, i, DIGITALIN);
for (int i = 4; i < 8; i++)
ULStat = cbDConfigBit(BoardNum, AUXPORT, i, DIGITALOUT);
if (ULStat != 0)
printf("%d", ULStat);

ULStat = cbAInputMode(BoardNum, SINGLE_ENDED);
if (ULStat != 0)
printf("%d", ULStat);

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

double ScaledData[COUNT];

unsigned long RawData[COUNT];  //read counts

unsigned Options = BACKGROUND + CONTINUOUS + SCALEDATA;

ULStat = cbAInputMode(BoardNum, SINGLE_ENDED);
if (ULStat != 0)
printf("%d", ULStat);

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

//use for digital io
USHORT bitVal[4] = { 0, 0, 0, 0 };
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);

//read the four digital input lines
for (int i = 0; i < 4; i++)
ULStat = cbDBitIn(BoardNum, AUXPORT, i, &bitVal[i]);

for (int j = 0; j< NumRows; j++)
{
for (int i = 0; i<ChanCount; i++)
printf("%2.3f\t", ScaledData[index++]);
for (int i = 0; i<4; i++)
printf("%d\t", bitVal[i]);

printf("\n");
}
//write the four digital output lines
for (int i = 4; i < 8; i++)
ULStat = cbDBitOut(BoardNum, AUXPORT, i, bitVal[i]);
}
else if ((curIndex < halfbuf) && (NextReadUpper == true))
{
NextReadUpper = false;
index = 0;
cbScaledWinBufToArray(MemHandle, ScaledData, halfbuf, halfbuf);

//read the four digital input lines
for (int i = 0; i < 4; i++)
ULStat = cbDBitIn(BoardNum, AUXPORT, i, &bitVal[i]);

for (int j = 0; j< NumRows; j++)
{
for (int i = 0; i<ChanCount; i++)
printf("%2.3f\t", ScaledData[index++]);

for (int i = 0; i<4; i++)
printf("%d\t", bitVal[i]);
printf("\n");
}
//write the four digital output lines
for (int i = 4; i < 8; i++)
ULStat = cbDBitOut(BoardNum, AUXPORT, i, bitVal[i]);
}

}


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

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


Posted 9/16/2021 7:55:18 AM by Administrator
https://kb.mccdaq.com/KnowledgebaseArticle50852.aspx