using System;
using MccDaq;
namespace AnalogOutputScan
{
class Program
{
public const int BLOCKSIZE = 50;
public const int CHANCOUNT = 1;
public const int FIRSTCHANNEL = 0;
public const int LASTCHANNEL = 0;
public const int FREQ = 100;
public const int BUFFERSIZE = BLOCKSIZE * CHANCOUNT;
public const string DEVICE = "1608G";
static void Main(string[] args)
{
MccDaq.ErrorInfo RetVal;
int Rate = FREQ;
int BoardNum = GetBoardNum(DEVICE);
if (BoardNum == -1)
Console.WriteLine("No USB-{0} detected!", DEVICE);
else
{
MccBoard daq = new MccDaq.MccBoard(BoardNum);
IntPtr OutBuf = MccService.ScaledWinBufAllocEx(BUFFERSIZE);
IntPtr buffer = MccService.ScaledWinBufAllocEx(BUFFERSIZE);
if( (OutBuf == IntPtr.Zero) || (buffer == IntPtr.Zero) )
{
Console.WriteLine("Insufficient Memory");
return;
}
double angle = 6.28318 / BUFFERSIZE;
double value;
int index = 0;
ushort[] dataArray = new ushort[BUFFERSIZE];
for (int j = 0; j < CHANCOUNT; j++)
{
for (int i = 0; i < BUFFERSIZE; i++)
{
value = (System.Math.Sin(angle * i) +1 ) * 32767;
dataArray[index++] = System.Convert.ToUInt16(value);
}
}
MccService.WinArrayToBuf(dataArray, OutBuf, 0, BUFFERSIZE);
Console.WriteLine("\nConnect analog output 0 to analog input 0...");
WaitForKey();
RetVal = daq.AOutScan( 0,
0,
BLOCKSIZE,
ref Rate,
Range.Bip10Volts,
OutBuf,
ScanOptions.Background
);
IsError(RetVal);
RetVal = daq.AInScan( FIRSTCHANNEL,
LASTCHANNEL,
BUFFERSIZE,
ref Rate,
Range.Bip10Volts,
buffer,
ScanOptions.ScaleData
);
IsError(RetVal);
double[] theArray = new double[BUFFERSIZE];
RetVal = MccService.ScaledWinBufToArray(buffer, theArray, 0, BUFFERSIZE);
RetVal = daq.StopBackground( FunctionType.AiFunction );
IsError(RetVal);
RetVal = daq.StopBackground( FunctionType.AoFunction );
IsError(RetVal);
MccService.WinBufFreeEx(buffer);
MccService.WinBufFreeEx(OutBuf);
DisplayData( theArray, BUFFERSIZE );
WaitForKey();
}
}
public static int GetBoardNum(string dev)
{
for (int BoardNum = 0; BoardNum < 99; BoardNum++)
{
MccDaq.MccBoard daq = new MccDaq.MccBoard(BoardNum);
if (daq.BoardName.Contains(dev))
{
Console.WriteLine("USB-{0} board number = {1}", dev, BoardNum.ToString());
daq.FlashLED();
return BoardNum;
}
}
return -1;
}
public static void WaitForKey()
{
Console.WriteLine("\nPress <SpaceBar> to continue...");
System.ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey();
} while (cki.Key != ConsoleKey.Spacebar);
}
public static int IsError(ErrorInfo e)
{
if (e.Value != 0)
{
Console.WriteLine(e.Message);
return 1;
}
return 0;
}
public static void DisplayData(double[] datArray, int Count)
{
int i = 0;
for (int row = 0; row < Count / CHANCOUNT; row++)
{
for (int c = 0; c < CHANCOUNT; c++)
Console.Write("{0}\t", datArray[i++].ToString("0.0000").PadLeft(10));
Console.Write("\r\n");
}
}
}
}