USB-TC CSharp (C#) Single Channel Temperature Read


The program listed below demonstrates how to read a thermocouple connected to the USB-TC . The program uses the MccDaq Dot Net API.To access the MccDaq 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 recreate the project use Visual Studio's File->New Project, select Visual C# and select Console Application. Create the new project and add the reference to the MccDaq Dot Net component. The easiest way to do this is to right mouse click the project in the Project Explorer and choose Add Reference. Select MccDaq from the NET list. Of course this assumes that you have installed the InstaCal software.

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 TinExample
{
    class Program
    {
        public const int BLOCKSIZE = 10;
        public const int CHANNEL = 0;
        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);
                WaitForKey();
                return; //exit program
            }
            else
            {
                MccBoard daq = new MccDaq.MccBoard(BoardNum);

                float TempData;
                for (int i = 0; i < BLOCKSIZE; i++)
                {
                    RetVal = daq.TIn(CHANNEL, TempScale.Fahrenheit, out TempData, ThermocoupleOptions.Filter);

                    IsError(RetVal);

                    Console.WriteLine("The temperature is: {0}", TempData.ToString("0.000").PadLeft(10));

                    System.Threading.Thread.Sleep(500); //max rate is 2Hz or 500mS per read.
                }
                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 any to continue...");
            do
            {
                System.Threading.Thread.Sleep(50);
            } while (Console.KeyAvailable);
            ConsoleKeyInfo cki = Console.ReadKey();

        }

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

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

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


    }
}


Posted 7/29/2013 3:27:57 AM by Administrator
https://kb.mccdaq.com/KnowledgebaseArticle50482.aspx