Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| daq-and-datalogging:documents:usb-1808-polling-subsystems [2023/06/13 16:18] – created Scandiweb | daq-and-datalogging:documents:usb-1808-polling-subsystems [2023/07/28 01:01] (current) – Arthur Brown | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== USB-1808 Reading Multiple Subsystems With Polling |
| - | This article explains how to use a USB-TEMP-AI module with DAQami to measure a 2-wire thermistor. (A RTD sensor or thermocouple could also be used.) | + | The following example uses the DaqInScan function |
| - | In order to use a USB-TEMP-AI module, with DAQami, a user must first configure it with InstaCal. | + | To access the API, add a reference to the MccDaq object. Adding |
| - | {{https:// | + | To make the code more readable a few convenience functions were added such as IsError, GetBoardNum, |
| - | Verify | + | 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. | ||
| - | {{https://files.digilent.com/ | + | < |
| + | using System; | ||
| + | using MccDaq; | ||
| - | Afterwards, exit InstaCal and then launch DAQami. | ||
| - | In DAQami’s Getting Started window, select ‘New Configuration’ icon followed by the ‘Automatic’ option. | + | //This program uses the DaqInScan function to read 4x AI, 1x DigIn, 1x Encoder simultaneously. It configures |
| + | //buffer with a size of packet times channel count time multipliers. To make the buffer bigger/ | ||
| + | // | ||
| + | //a new half buffer | ||
| + | //routine prints | ||
| - | {{https://files.digilent.com/resources/measurement-computing/Reference Images/pic_3.png? | + | 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 | ||
| + | public const int COUNTER = 2; | ||
| + | public static int rows = halfbuf | ||
| + | public static MccBoard daq; | ||
| + | public static int rate = 3600; | ||
| + | public static int preTrigCount = 0; | ||
| - | Click on the third icon button, in the upper left corner, to switch to the Display Panel. | + | } |
| + | class Program | ||
| + | { | ||
| - | {{https:// | + | static void Main(string[] args) |
| + | | ||
| + | int BoardNum | ||
| + | short daqStatus = 0; | ||
| + | int Count = 0; | ||
| + | int Index = 0; | ||
| + | |||
| + | short[] | ||
| + | ChannelType[] | ||
| + | Range[] | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | Console.WriteLine(" | ||
| + | |||
| + | BoardNum = GetBoardNum(" | ||
| + | |||
| + | if (BoardNum == -1) | ||
| + | | ||
| + | Console.WriteLine(" | ||
| + | WaitForKey(); | ||
| + | return; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Constants.daq = new MccDaq.MccBoard(BoardNum); | ||
| + | |||
| + | Constants.daq.AInputMode(AInputMode.SingleEnded); | ||
| + | |||
| + | CounterMode CMODE = CounterMode.Encoder | CounterMode.EncoderModeX1; | ||
| + | |||
| + | IsError(Constants.daq.CConfigScan( | ||
| + | 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.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(" | ||
| + | |||
| + | 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, | ||
| + | |||
| + | DisplayData(UserBuffer, | ||
| + | ReadLower = false; //flag that controls the next read | ||
| + | } | ||
| + | else if ((Index < Constants.halfbuf) & !ReadLower) | ||
| + | { | ||
| + | |||
| + | //get the upper half | ||
| + | IsError(MccService.WinBufToArray32(buffer, | ||
| + | |||
| + | DisplayData(UserBuffer, | ||
| + | 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; | ||
| + | { | ||
| + | |||
| + | try | ||
| + | { | ||
| + | |||
| + | if (inventory[boardNum].ProductName.Contains(dev)) | ||
| + | { | ||
| + | MccDaq.MccBoard daqBoard = MccDaq.DaqDeviceManager.CreateDaqDevice(boardNum, | ||
| + | Console.WriteLine(" | ||
| + | Console.WriteLine(" | ||
| + | Console.WriteLine(" | ||
| + | return boardNum; | ||
| + | } | ||
| + | } | ||
| + | catch (ULException ule) | ||
| + | { | ||
| + | Console.WriteLine(" | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | return -1; | ||
| + | } | ||
| + | / | ||
| + | |||
| + | public static void WaitForKey() | ||
| + | { | ||
| + | Console.WriteLine(" | ||
| + | do | ||
| + | { // | ||
| + | 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; | ||
| + | { | ||
| + | if (c < 4) | ||
| + | { | ||
| + | IsError(Constants.daq.ToEngUnits32(Range.Bip10Volts, | ||
| + | Console.Write(" | ||
| + | } | ||
| + | else | ||
| + | Console.Write(" | ||
| + | |||
| + | i++; | ||
| + | } | ||
| + | Console.Write(" | ||
| + | } | ||
| + | Console.WriteLine(" | ||
| + | |||
| + | } | ||
| + | |||
| + | / | ||
| + | |||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | **Attachments**\\ | ||
| + | [[ https:// | ||
| - | Finally, in the top center of DAQami, click the green arrow to start an acquisition. | ||

