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

C# example demonstrates how to collect thermocouple data at 100...

Expand / Collapse
 

C# example demonstrates how to collect thermocouple data at 100 Samples per second.


The code listing below uses the DaqInScan function to read thermocouple inputs. (module attached at the bottom of this article) This function is typically used to simultaneously read the device subsystems (analog, digital) on devices such as the USB-1616HS and USB-2523. However, if you need to scan multiple thermocouples at a faster rate, it must also be used so as to pair up the channel with its CJC sensor. In this example, we use it to read eight thermocouple inputs (and the associated CJC channels) at 100 samples per second. The binary data returned in the buffer contains both CJC and channel data. It is arranged as CJC, Channel0, CJC Channel1, etc. Care must be taken to position the correct CJC before each channel. The GetTCValues function is used to convert the CJC and channel data to a temperature value. The key to using the function is to enter the position of the first thermocouple/CJC and the number of samples to convert. 

Attached below is the 32-bit code file. To use it, you must create a 32-bit C# Console project and a reference to the MccDaq object must be added. Adding the reference is usually accomplished by right clicking the Project [under the Project Explorer and selecting Add Reference/Extensions.  

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 8 thermocouple channels; the return data 
//in the form of raw counts from both the CJC and the thermocouple; in order to get
//temperature data, as second function named GetTCValues is used
namespace CSharp_USB_1616HS_DaqInScan
{
    static class Constants
    {
        public const int ChanCount = 16; //number of channels ( 8x TC 8x CJC)

        public const int BUFFSIZE = 32 * ChanCount;
        public static int bufferSize = BUFFSIZE;
        public const int HALFBUFF = BUFFSIZE / 2;

        public static MccBoard daq;
        public static int rate = 100;
        public static int preTrigCount = 0;
        public const int TC_Count = 2;


    }
    class Program
    {

        static void Main(string[] args)
        {
            int BoardNum = 0;
            int Count = 0;
            int Index = 0;
            short daqStatus = 0;

            ushort[] ADData = new ushort[Constants.BUFFSIZE]; //raw data
            float[] tempVals = new float[Constants.BUFFSIZE]; //used for temperatures

            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("1616");

            if (BoardNum == -1)
            {
                Console.WriteLine("Device not detected!");
                WaitForKey();
                return;
            }
            else
            {
                Constants.daq = new MccDaq.MccBoard(BoardNum);


                Constants.daq.AInputMode(AInputMode.Differential);
               


                //Channel configuration array definitions
                int i = 0;


                //if mixing voltage an TC, make the TC channels first in the list for easier conversion using GetTCValues. 

                //TC0
                ChanArray[i] = 0;
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 0;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;

                //TC1
                i++;
                ChanArray[i] = 1;
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 1;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;

                //TC2
                ChanArray[i] = 1; //TC2 uses CJC1
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 2;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;

                //TC3
                ChanArray[i] = 2; //TC3 uses CJC2
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 3;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;

                //TC4
                ChanArray[i] = 3; //TC4 uses CJC3
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 4;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;

                //TC5
                ChanArray[i] = 4; //TC5 uses CJC 4
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 5;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;

                //TC6
                ChanArray[i] = 4; //TC6 also uses CJC4
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 6;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;
                i++;


                //TC7
                ChanArray[i] = 5; //TC7 uses CJC5
                ChanTypeArray[i] = ChannelType.CJC;
                GainArray[i] = Range.NotUsed;
                i++;

                ChanArray[i] = 7;
                ChanTypeArray[i] = ChannelType.TC;
                GainArray[i] = Range.NotUsed;


                ScanOptions Options = ScanOptions.ConvertData
                                        | ScanOptions.Continuous
                                        | ScanOptions.Background;

                //allocate buffer
                int bufferSize = Constants.bufferSize;

                IntPtr buffer = MccService.WinBufAllocEx(Constants.bufferSize);

                Console.WriteLine("Actual Rate {0}\n", Constants.rate);

                WaitForKey();

                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();


                bool readLow = true;

                do
                {


                    System.Threading.Thread.Sleep(1);

                    IsError(Constants.daq.GetStatus(out daqStatus, out Count, out Index, FunctionType.DaqiFunction));



                    if ((Index > Constants.HALFBUFF) && readLow)
                    {
                        MccService.WinBufToArray(buffer, ADData, 0, Constants.HALFBUFF);

                        //the buffer contains 32 scans of the 16 channels. Because the TC are first in the list the TC conversion starts at 0 for the lower portion.
                        //Keep in mind that GetTCValues uses scans whereas WinBufToArray uses Scans * Channel Count
                        IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 0, 16, TempScale.Celsius, tempVals));

                        readLow = false;

                        i = 0;
                        for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
                        {
                            Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"));

                            idx += 8;
                        }

                    }

                    else if ((Index < Constants.HALFBUFF) && !readLow)
                    {
                        MccService.WinBufToArray(buffer, ADData, Constants.HALFBUFF, Constants.HALFBUFF);

                        //the buffer contains 32 scans of the 16 channels. Because the TC are first in the list the TC conversion starts at 16 for the upper portion.
                        //Keep in mind that GetTCValues uses scans whereas WinBufToArray uses Scans * Channel Count
                        IsError(Constants.daq.GetTCValues(ChanArray, ChanTypeArray, Constants.ChanCount, buffer, 16, 16, TempScale.Celsius, tempVals));

                        readLow = true;

                        i = 0;
                        for (int idx = 0; idx < Constants.HALFBUFF; idx+=16)
                        {
                            Console.Write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"),
                                                 tempVals[i++].ToString("00.00"));


                        }
                    }



                } while (!Console.KeyAvailable);
                cki = Console.ReadKey();

                //stop background operation
                IsError(Constants.daq.StopBackground(FunctionType.DaqiFunction));
                Constants.daq.TimerOutStop(0);
                //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...\n");
            do
            {   //idle loop
                System.Threading.Thread.Sleep(10);
            } while (!Console.KeyAvailable);
            Console.ReadKey();//get rid of the key press
        }

        /*////////////////////////////////////////////////////////////////////////////////////*/

        public static int IsError(ErrorInfo e)
        {
            if (e.Value != 0)
            {
                Console.WriteLine(e.Message);
                WaitForKey();
                return 1;
            }
            return 0;
        }


        /*////////////////////////////////////////////////////////////////////////////////////*/
        //function to convert to engineering units; if just volts set scale to 1.0 and offset to 0.0
        public static double ScaleData(Range rng, ushort dataval, double scale, double offset)
        {
            float V2 = 0.0f;
            Constants.daq.ToEngUnits(rng, dataval, out V2);
            return V2 * scale + offset;

        }



    }
}



Rate this Article:

Attachments


Program.cs Program.cs (12.65 KB, 14 views)

Add Your Comments


For comments email [email protected].

Details
Article ID: 50861

Last Modified:8/31/2022 8:31:29 AM

Article has been viewed 173 times.

Options