using System;
using MccDaq; // also, add reference to MccDaq to the project
//This program uses the DaqInScan function to read 4x AI, 1x DigIn, 1x Encoder simultaneously. It configures a
//buffer with a size of packet times channel count time multipliers. To make the buffer bigger/smaller change
//change the multiplier. The sample rate is 3600 samples per second. The loop is set so as to read when
//a new half buffer is available. The program retrieves one half of the buffer when it available. The print
//routine prints only the first 32 samples to prevent it from consuming available time
namespace CSharp_USB_1808_DaqInScanEncoder
{
static class Constants
{
public const int ChanCount = 6; //number of channels (4x AI, 1x DigIn, 2x Encoder)
public const int PACKET = 128;
public const int BUFFMULTIPLER = 100;
public const int BUFFSIZE = PACKET * ChanCount * BUFFMULTIPLER;
public static int bufferSize = BUFFSIZE;
public static int halfbuf = bufferSize / 2;
public const int COUNTER = 2; //first encoder channel
public static int rows = halfbuf / ChanCount;
public static MccBoard daq;
public static int rate = 3600;
public static int preTrigCount = 0;
}
class Program
{
static void Main(string[] args)
{
int BoardNum = 0;
short daqStatus = 0;
int Count = 0;
int Index = 0;
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("1808");
if (BoardNum == -1)
{
Console.WriteLine("Device not detected!");
WaitForKey();
return;
}
else
{
Constants.daq = new MccDaq.MccBoard(BoardNum);
Constants.daq.AInputMode(AInputMode.SingleEnded);
CounterMode CMODE = CounterMode.Encoder | CounterMode.EncoderModeX1;
IsError(Constants.daq.CConfigScan( 2,
CMODE,
CounterDebounceTime.DebounceNone,
CounterDebounceMode.TriggerAfterStable,
CounterEdgeDetection.RisingEdge,
CounterTickSize.Tick20ns,
0));
int i = 0;
ChanArray[i] = 0;
ChanTypeArray[i] = ChannelType.Analog;
GainArray[i] = Range.Bip10Volts;
i++;
ChanArray[i] = 1;
ChanTypeArray[i] = ChannelType.Analog;
GainArray[i] = Range.Bip10Volts;
i++;
ChanArray[i] = 2;
ChanTypeArray[i] = ChannelType.Analog;
GainArray[i] = Range.Bip10Volts;
i++;
ChanArray[i] = 3;
ChanTypeArray[i] = ChannelType.Analog;
GainArray[i] = Range.Bip10Volts;
i++;
ChanArray[i] = (int)DigitalPortType.AuxPort;
ChanTypeArray[i] = ChannelType.Digital;
GainArray[i] = Range.NotUsed;
i++;
ChanArray[i] = 2; //encoder channels are 2 & 3
ChanTypeArray[i] = ChannelType.Ctr;
GainArray[i] = Range.NotUsed;
i++;
ScanOptions Options = ScanOptions.Default
| ScanOptions.Continuous
| ScanOptions.Background;
//allocate buffer
int bufferSize = Constants.bufferSize;
IntPtr buffer = MccService.WinBufAlloc32Ex(Constants.bufferSize);
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();
Console.WriteLine("Retrieving Data\n");
int[] UserBuffer = new int[Constants.halfbuf];
bool ReadLower = true;
do
{
IsError(Constants.daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.DaqiFunction));
if ((Index >= Constants.halfbuf) & ReadLower) //check for 50% more data
{
//get lower half of buffer
IsError(MccService.WinBufToArray32(buffer, UserBuffer, 0, Constants.halfbuf));
DisplayData(UserBuffer, Constants.rows);
ReadLower = false; //flag that controls the next read
}
else if ((Index < Constants.halfbuf) & !ReadLower)
{
//get the upper half
IsError(MccService.WinBufToArray32(buffer, UserBuffer, Constants.halfbuf, Constants.halfbuf));
DisplayData(UserBuffer, Constants.rows);
ReadLower = true;//flag that controls the next read
}
} while (!Console.KeyAvailable);
cki = Console.ReadKey();
//stop background operation
IsError(Constants.daq.StopBackground(FunctionType.DaqiFunction));
//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...");
do
{ //idle loop
System.Threading.Thread.Sleep(10);
} while (!Console.KeyAvailable);
}
/*////////////////////////////////////////////////////////////////////////////////////*/
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
Console.WriteLine(e.Message);
WaitForKey();
return 1;
}
return 0;
}
//**************************************************************
public static void DisplayData(int[] datArray, int rows)
{
//Writes data to screen and to file
int i = 0;
double temp;
if (rows > 32) rows = 32;
for (int row = 0; row < rows; row++)
{
for (int c = 0; c < Constants.ChanCount; c++)
{
if (c < 4)
{
IsError(Constants.daq.ToEngUnits32(Range.Bip10Volts, datArray[i], out temp));
Console.Write("{0}\t", temp.ToString("0.000"));
}
else
Console.Write("{0}\t", datArray[i].ToString("000000"));
i++;
}
Console.Write("\r\n");
}
Console.WriteLine("\nScans of data read {0}\n", Constants.rows);
}
/*////////////////////////////////////////////////////////////////////////////////////*/
}
}