USB-TC CSharp (C#) Temperature Read Example

The following example demonstrates how to set the API to read temperature using the USB-TC.

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, DisplayData 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 Display data merely displays the data in column form and WaitForKey does just that - waits for someone to press the spacebar.

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; // add reference to project for MccDaq

namespace TinSCanExample
{
    class Program
    {
        public const int BLOCKSIZE = 50;
        public const int CHANCOUNT = 4;
        public const int FIRSTCHANNEL = 0;
        public const int LASTCHANNEL = 3;
        public const int FREQ = 2;
        public const int BUFFERSIZE = BLOCKSIZE * CHANCOUNT;
        public const string DEVICE = "TC";


        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            int BoardNum = 0;

            //locate the USB-TC
            BoardNum = GetBoardNum(DEVICE);

            if (BoardNum == -1)
                Console.WriteLine("No USB-{0} detected!", DEVICE);
            else
            {
                MccBoard daq = new MccDaq.MccBoard(BoardNum);

                float[] TempData = new float[CHANCOUNT];
                for (int i = 0; i < BLOCKSIZE; i++)
                {
                    RetVal = daq.TInScan(FIRSTCHANNEL, LASTCHANNEL, TempScale.Fahrenheit, TempData, ThermocoupleOptions.Filter);

                    IsError(RetVal);

                    DisplayData(TempData);
                }
                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);
                WaitForKey();
                return 1;
            }
            return 0;
        }

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

        public static void DisplayData(float[] datArray)
        {
            for (int c = 0; c < CHANCOUNT; c++)
                Console.Write("{0}F\t", datArray[c].ToString("0.00").PadLeft(10));
            Console.Write("\r\n");
        }

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

    }
}

Attachments
TinScanExample.zip