Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Measurement Computing Data Acquisition Knowledgebase

Welcome Guest ( )

C# wrapper for IOtech DaqX dll

Expand / Collapse
 

C# wrapper for IOtech DaqX dll


The example below is an example showing how to read the analog inputs on an IOtech PersonalDaq/3000 device using C#. It uses a wrapper around the DaqX.dll. The complete project is attached as download. This is support was not tested by Measurement Computing and is to merely provide a guide to what is possible.

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 System.Collections.Generic;
using System.Linq;
using System.Text;
using DaqXAPIClass;
namespace DaqXAPIExample_BasicAcquisition_UserBuffer
{
    class Program
    {

        public const uint SCANS = 100;
        public const uint CHANCOUNT = 4;
        public const uint BUFFSIZE = SCANS * CHANCOUNT;
        public const float FREQ = 100.0f;
        public const DaqX.DaqAdcTransferMask TM = DaqX.DaqAdcTransferMask.DatmCycleOn |
                                                    DaqX.DaqAdcTransferMask.DatmUserBuf |
                                                    DaqX.DaqAdcTransferMask.DatmUpdateSingle |
                                                    DaqX.DaqAdcTransferMask.DatmIgnoreOverruns;

        public static void WaitForKey()
        {
            Console.WriteLine("\nPress <SpaceBar> to continue...");
            System.ConsoleKeyInfo cki;

            do
            {
                   cki = Console.ReadKey();
            } while (cki.Key != ConsoleKey.Spacebar);
        }

        static void Main(string[] args)
        {
            System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
            

            DaqX.DaqError err;
            int dh;
            ushort active = 0;
            ushort retCount = 0;
            float actualRate = 0;
            float Range = 10.0f;
            float Resolution = Range / 32768;
            ushort head = 0;
            ushort tail = 0;
            uint i,j;

            //these arrays are use to configure channels to be sampled by ADC
            uint[] myChan = new uint[CHANCOUNT];
            DaqX.DaqAdcGain[] myGain = new DaqX.DaqAdcGain[CHANCOUNT];
            DaqX.DaqAdcFlag[] myFlag = new DaqX.DaqAdcFlag[CHANCOUNT];

            //open the device based on the name provided by either the Daq Configuration utility or 
            //the devices plug-n-play name. For example, PersonalDaq3000 or DaqBoard2K0.
            dh = DaqX.DevOpen("PersonalDaq3000");
            if (dh >= 0)
                Console.WriteLine("Device Connected");
            else
            {
                Console.WriteLine("Error: Device not connected");
                Environment.Exit(-1);
            }


            Console.WriteLine("\nConfiguring acquisition for:\n");
            Console.WriteLine("Sample Rate:        {0}\t", FREQ.ToString());
            Console.WriteLine("Number of Samples:  {0}\t", SCANS.ToString());
            Console.WriteLine("Number of Channels: {0}\t", CHANCOUNT.ToString());

            WaitForKey();

            //Configure per channel setup specifying channel number, gain range, and input attributes
            for (i = 0; i < CHANCOUNT; i++)
            {
                myChan.SetValue(i, i);
                myGain.SetValue(DaqX.DaqAdcGain.DgainX1, i);
                myFlag.SetValue((DaqX.DaqAdcFlag.DafBipolar | DaqX.DaqAdcFlag.DafSigned), i);
            }

            err = DaqX.AdcSetAcq(dh, DaqX.DaqAdcAcqMode.DaamInfinitePost, 0, SCANS);
            err = DaqX.AdcSetScan(dh, myChan, myGain, myFlag, CHANCOUNT);
            err = DaqX.AdcSetTrigger(dh, DaqX.DaqAdcTriggerSource.DatsImmediate, true, 0, 0, 0);
            err = DaqX.AdcSetRate(dh, DaqX.DaqAdcRateMode.DarmFrequency, DaqX.DaqAdcAcqState.DaasPostTrig, FREQ, ref actualRate);
            
            unsafe
            {
                float val = 0.0f;
                ushort[] buffer = new ushort[BUFFSIZE];

                fixed (ushort* bufPtr = buffer)
                {
                    DaqX.AdcTransferSetBufferP(dh, bufPtr, SCANS, TM);
                    err = DaqX.AdcTransferStart(dh);
                    err = DaqX.AdcArm(dh);
                    do
                    {
                        tail = head;
                        do
                        {
                            DaqX.AdcTransferGetStat(dh, ref active, ref retCount);
                            head = (ushort)((retCount * CHANCOUNT) % BUFFSIZE);
                            System.Threading.Thread.Sleep(100);

                        }while (((head - tail) % CHANCOUNT) > 10); //pause for chunk data to be present
                            

                        for (i = tail; i != head; i = (i + CHANCOUNT) % BUFFSIZE)
                        {
                            Console.Write("{0}\t", i.ToString("0000"));
                            for (j = 0; j < CHANCOUNT; j++)
                            {
                                val = (short)buffer[i + j] * Resolution;
                                Console.Write("{0} \t", val.ToString("+0.00000;-0.00000;00.00000"));
                            }
                            Console.Write("\n");
                        }
            
                        if (Console.KeyAvailable)
                        cki = Console.ReadKey();
                    } while (cki.Key != ConsoleKey.Spacebar);

                    DaqX.DisArm(dh);
                    DaqX.DevClose(dh);
                    Environment.Exit(-1);

     
                }//fixed
            }//unsafe
        
        }//main

    }//class
}//namespace



Rate this Article:
Tags:

Attachments


DaqXAPIClass.zip DaqXAPIClass.zip (103.04 KB, 24 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50804

Last Modified:1/30/2020 12:20:06 PM

Article has been viewed 131 times.

Options