using System;
using System.Runtime.InteropServices;
using MccDaq;//Project must have reference to MccDaq
using System.IO;
namespace TemperatureVoltageReadBackground
{
class Program
{
public const int BLOCKSIZE = 50; //for best results use increments of packet size which is 32
public const int CHANCOUNT = 4;
public const int FIRSTCHANNEL = 0;
public const int LASTCHANNEL = 3;
public const int SAMPLERATE= 100;
public const int BUFFERSIZE = BLOCKSIZE * CHANCOUNT;
public const int HALFBUFFSIZE = BUFFERSIZE / 2;
public const string DEVICE = "2416";
//external function not found in MccDaq
//Used to determine InstaCal setup
//************* Compile to x86 for 32 bit function call ****************************
[DllImport("cbw32.dll")]
public extern static long cbGetConfig([MarshalAs(UnmanagedType.I4)] int InfoType,
[MarshalAs(UnmanagedType.I4)] int BoardNum,
[MarshalAs(UnmanagedType.I4)] int DevNum,
[MarshalAs(UnmanagedType.I4)] int ConfigItem,
[MarshalAs(UnmanagedType.I8)] ref long ConfigVal
);
//**********************************************************************************
public static StreamWriter fStream;
static void Main(string[] args)
{
//Project must have reference to MccDaq
MccDaq.ErrorInfo RetVal;
int BoardNum = 0;
int InfoType = 2;
int DevNum = 0;
int ConfigItem = 235; //Item number to get channel type
long ConfigVal = 0;
long ret;
int Rate = SAMPLERATE;
BoardNum = GetBoardNum(DEVICE);
if (BoardNum == -1)
{
Console.WriteLine("No USB-{0} detected!", DEVICE);
WaitForKey();
return;
}
MccBoard daq = new MccDaq.MccBoard(BoardNum);
IntPtr buffer = MccService.ScaledWinBufAllocEx(BUFFERSIZE);
if (buffer == IntPtr.Zero)
{
Console.WriteLine("Bad Memory Handle");
WaitForKey();
return;
}
short[] chType = new short[CHANCOUNT];
short[] chArray = new short[CHANCOUNT];
Range[] chRange = new Range[CHANCOUNT];
//channels I want to read
chArray[0] = 0;
chArray[1] = 1;
chArray[2] = 2;
chArray[3] = 3;
//Use InstaCal and set the first two channels to thermocouples
//and the second two to voltage. Set the Data Rate to 1000
//When using lengthy thermocouples set the channel Data Rate to 60Hz and reduce
//the program's sample rate
chType[0] = 2;//type for thermocouple
chType[1] = 2;
chType[2] = 5;//type for voltage
chType[3] = 5;
//Check to make sure the user ran InstaCal and set the channels appropriately
for (DevNum = 0; DevNum < CHANCOUNT; DevNum++)
{
ret = cbGetConfig(InfoType, BoardNum, DevNum, ConfigItem, ref ConfigVal);
if (ConfigVal != chType[DevNum])
{
Console.Write("Configuration Error!\nRun InstaCal\n" +
"\tset channels 0 - 3 to temperature\n");
WaitForKey();
return;
}
}
chRange[0] = Range.Bip10Volts;
chRange[1] = Range.Bip10Volts;
chRange[2] = Range.Bip10Volts;
chRange[3] = Range.Bip10Volts;
RetVal = daq.ALoadQueue(chArray, chRange, CHANCOUNT);
IsError(RetVal);
//ALoadQueue overrides channels and range in AInScan
RetVal = daq.AInScan( FIRSTCHANNEL,
LASTCHANNEL,
BUFFERSIZE,
ref Rate,
Range.Bip10Volts,
buffer,
ScanOptions.Background | ScanOptions.ScaleData | ScanOptions.Continuous
);
IsError(RetVal);
fStream = new StreamWriter(@"C:\Users\Public\Documents\DataFile1608G.asc");
CreateFileHeaders(chArray); //writes basic info to the beginning of the file
int Count = 0;
int Index = 0;
short daqStatus;
bool ReadLower = true;
double[] theArray = new double[BUFFERSIZE];
System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
//Loop until key press
do
{
RetVal = daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.AiFunction);
if ((Index >= HALFBUFFSIZE) & ReadLower) //check for 50% more data
{
//get lower half of buffer - ScaledWinBufToArray returns engineering units
RetVal = MccService.ScaledWinBufToArray(buffer, theArray, 0, HALFBUFFSIZE);
IsError(RetVal);
DisplayData(theArray, HALFBUFFSIZE / CHANCOUNT);
ReadLower = false; //flag that controls the next read
}
else if ((Index < HALFBUFFSIZE) & !ReadLower)
{
//get the upper half - ScaledWinBufToArray returns engineering units
RetVal = MccService.ScaledWinBufToArray(buffer, theArray, HALFBUFFSIZE, HALFBUFFSIZE);
IsError(RetVal);
DisplayData(theArray, HALFBUFFSIZE / CHANCOUNT);
ReadLower = true;//flag that controls the next read
}
} while (!Console.KeyAvailable);
cki = Console.ReadKey();
//flush any buffered data out to disk
fStream.Close();
//stop the acquisition
RetVal = daq.StopBackground(FunctionType.AiFunction);
//free up memory
MccService.WinBufFreeEx(buffer);
WaitForKey();
}