USB-1808 Reading Multiple Subsystems With Event Notification
The following example uses the DaqInScan function to simultaneous read the USB-1808 subsystems. It is configured to read four analog inputs, the four bit digital port, and one encoder channel. 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. An event routine is set so as to read data when a new half buffer is available. The print routine prints only the first 32 samples to prevent it from consuming available time
To access the API, add a reference to the MccDaq object. Adding the reference is usually accomplished by right clicking the Project [under the Project Explorer and selecting Add Reference. The complete project is attached to the article and was created with Visual Studio 2008 using CSharp. Compiling and execution is quick as there is minimal Windows overhead and associated code.
To make the code more readable a few convenience functions were added such as IsError, GetBoardNum, and WaitForKey. The IsError function checks the error number in the ErrorInfo object and if not zero displays the error message. The GetBoardNum function begins reading the Device strings from each number location and as soon as it finds one that contains the identifying string it exits. The WaitForKey does just that - waits for someone to press a key. To view the complete program project download the attachment below.
Disclaimer:
The attached Code or Example is provided As Is. It has not been tested or validated as a product, for use in a deployed application or system, or for use in hazardous environments. You assume all risks for use of the Code or Example.
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 multiplers. To make the buffer bigger/smaller change //change the multipler. The sample rate is 3600 samples per second. The event is set so as to fire 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[] 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); Constants.daq.EnableEvent(EventType.OnDataAvailable, Constants.halfbuf, GetData, buffer); 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"); // Idle loop do { System.Threading.Thread.Sleep(100); } 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 = 0; Console.Clear(); 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); } /*////////////////////////////////////////////////////////////////////////////////////*/ public static void GetData(int bd, MccDaq.EventType et, uint Count, IntPtr pUserData) { int[] UserBuffer = new int[Constants.halfbuf]; //Calculate the index of the latest sample in the buffer. int Index = ((int)Count) % Constants.bufferSize; if (Index >= Constants.halfbuf-1) //check for 50% more data { //get lower half of buffer IsError(MccService.WinBufToArray32(pUserData, UserBuffer, 0, Constants.halfbuf)); DisplayData(UserBuffer, Constants.rows); Console.WriteLine("\nRead lower half of the buffer. Total sample count {0}", Count.ToString("000,000,000")); } else if (Index < Constants.halfbuf-1) { //get the upper half IsError(MccService.WinBufToArray32(pUserData, UserBuffer, Constants.halfbuf, Constants.halfbuf)); DisplayData(UserBuffer, Constants.rows); Console.WriteLine("\nRead upper half of the buffer. Total sample count {0}", Count.ToString("000,000,000")); } } /*////////////////////////////////////////////////////////////////////////////////////*/ } }
Attachments
CSharp_USB_1808_DaqInScanWithEvent.zip