using System;
using MccDaq; // also, add reference to MccDaq to the project
//This program uses the DaqInScan function to read 8 thermocouple channels; the return data
//in the form of raw counts from both the CJC and the thermocouple; in order to get
//temperature data, as second function named GetTCValues is used
namespace CSharp_USB_1616HS_DaqInScan
{
static class Constants
{
public const int ChanCount = 16; //number of channels ( 8x TC 8x CJC)
public const int BUFFSIZE = 32 * ChanCount;
public static int bufferSize = BUFFSIZE;
public const int HALFBUFF = BUFFSIZE / 2;
public static MccBoard daq;
public static int rate = 100;
public static int preTrigCount = 0;
public const int TC_Count = 2;
}
class Program
{
static void Main(string[] args)
{
int BoardNum = 0;
int Count = 0;
int Index = 0;
short daqStatus = 0;
ushort[] ADData = new ushort[Constants.BUFFSIZE]; //raw data
float[] tempVals = new float[Constants.BUFFSIZE]; //used for temperatures
short[] ChanArray = new short[Constants.ChanCount]; // array to hold channel queue information
ChannelType[] ChanTypeArray = new ChannelType[Constants.ChanCount]; // array to hold channel type information
Range[] GainArray = new Range[Constants.ChanCount]; // array to hold gain queue information
Console.WriteLine("Locating Device...Please wait\n");
BoardNum = GetBoardNum("1616");
if (BoardNum == -1)
{
Console.WriteLine("Device not detected!");
WaitForKey();
return;
}
else
{
Constants.daq = new MccDaq.MccBoard(BoardNum);
Constants.daq.AInputMode(AInputMode.Differential);
//Channel configuration array definitions
int i = 0;
//if mixing voltage an TC, make the TC channels first in the list for easier conversion using GetTCValues.
//TC0
ChanArray[i] = 0;
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 0;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
//TC1
i++;
ChanArray[i] = 1;
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 1;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC2
ChanArray[i] = 1; //TC2 uses CJC1
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 2;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC3
ChanArray[i] = 2; //TC3 uses CJC2
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 3;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC4
ChanArray[i] = 3; //TC4 uses CJC3
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 4;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC5
ChanArray[i] = 4; //TC5 uses CJC 4
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 5;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC6
ChanArray[i] = 4; //TC6 also uses CJC4
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 6;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
i++;
//TC7
ChanArray[i] = 5; //TC7 uses CJC5
ChanTypeArray[i] = ChannelType.CJC;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 7;
ChanTypeArray[i] = ChannelType.TC;
GainArray[i] = Range.NotUsed;
ScanOptions Options = ScanOptions.ConvertData
| ScanOptions.Continuous
| ScanOptions.Background;
//allocate buffer
int bufferSize = Constants.bufferSize;
IntPtr buffer = MccService.WinBufAllocEx(Constants.bufferSize);
Console.WriteLine("Actual Rate {0}\n", Constants.rate);
WaitForKey();
IsError(Constants.daq.DaqInScan(ChanArray,
ChanTypeArray,
GainArray,
Constants.ChanCount,
ref Constants.rate,
ref Constants.preTrigCount,
ref Constants.bufferSize,
buffer,
Options));
System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
bool readLow = true;
do
{
System.Threading.Thread.Sleep(1);
IsError(Constants.daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.DaqiFunction));
if ((Index > Constants.HALFBUFF) && readLow)
{
MccService.WinBufToArray(buffer, ADData, 0, Constants.HALFBUFF);
//the buffer contains 32 scans of the 16 channels. Because the TC are first in the list the TC conversion starts at 0 for the lower portion.
//Keep in mind that GetTCValues uses scans whereas WinBufToArray uses Scans * Channel Count
IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 0, 16, TempScale.Celsius, tempVals));
readLow = false;
i = 0;
for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
{
Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"));
idx += 8;
}
}
else if ((Index < Constants.HALFBUFF) && !readLow)
{
MccService.WinBufToArray(buffer, ADData, Constants.HALFBUFF, Constants.HALFBUFF);
//the buffer contains 32 scans of the 16 channels. Because the TC are first in the list the TC conversion starts at 16 for the upper portion.
//Keep in mind that GetTCValues uses scans whereas WinBufToArray uses Scans * Channel Count
IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 16, 16, TempScale.Celsius, tempVals));
readLow = true;
i = 0;
for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
{
Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"),
tempVals[i++].ToString("00.00"));
}
}
} while (!Console.KeyAvailable);
cki = Console.ReadKey();
//stop background operation
IsError(Constants.daq.StopBackground(FunctionType.DaqiFunction));
Constants.daq.TimerOutStop(0);
//free up memory
IsError(MccService.WinBufFreeEx(buffer));
WaitForKey();
}
// end of program
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int GetBoardNum(string dev)
{
MccDaq.DaqDeviceManager.IgnoreInstaCal();
MccDaq.DaqDeviceDescriptor[] inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(MccDaq.DaqDeviceInterface.Any);
int DevicesFound = inventory.Length;
if (DevicesFound > 0)
{
for (int boardNum = 0; boardNum < DevicesFound; boardNum++)
{
try
{
if (inventory[boardNum].ProductName.Contains(dev))
{
MccDaq.MccBoard daqBoard = MccDaq.DaqDeviceManager.CreateDaqDevice(boardNum, inventory[boardNum]);
Console.WriteLine("Product Name: {0}", inventory[boardNum].ProductName);
Console.WriteLine("Device Type # : {0}", inventory[boardNum].ProductID);
Console.WriteLine("Serial # : {0}", inventory[boardNum].UniqueID);
return boardNum;
}
}
catch (ULException ule)
{
Console.WriteLine("Error occured: " + ule.Message);
}
}
}
return -1;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static void WaitForKey()
{
Console.WriteLine("\nPress any key to continue...\n");
do
{ //idle loop
System.Threading.Thread.Sleep(10);
} while (!Console.KeyAvailable);
Console.ReadKey();//get rid of the key press
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
Console.WriteLine(e.Message);
WaitForKey();
return 1;
}
return 0;
}
/*////////////////////////////////////////////////////////////////////////////////////*/
//function to convert to engineering units; if just volts set scale to 1.0 and offset to 0.0
public static double ScaleData(Range rng, ushort dataval, double scale, double offset)
{
float V2 = 0.0f;
Constants.daq.ToEngUnits(rng, dataval, out V2);
return V2 * scale + offset;
}
}
}