USB-TC CSharp (C#) Digital Input

The following program continuously acquires data from the USB-TC digital port. The program then breaks down the eight bit value into separate bits to determine the state of each one. The state is displayed as a True or False.

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.

To make the code more readable a few convenience functions were added such as GetBoardNum, and WaitForKey. 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 WaitForKey function wait for the user 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; //added to project references

namespace DigitalInput
{
    class Program
    {
        public const string DEVICE = "TC";


        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            short BitVal = 0;

            //locate device
            int BoardNum = GetBoardNum(DEVICE);

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

                Console.Write("Digital Single Bit Input Example\n");

                //read eight bit port
                RetVal = daq.DConfigPort(DigitalPortType.AuxPort, DigitalPortDirection.DigitalIn);

                WaitForKey();

                Console.Clear();

                do
                {

                    RetVal = daq.DIn(DigitalPortType.AuxPort, out BitVal);

                    Console.Write("D0={0}\n", Convert.ToBoolean(BitVal & 1));
                    Console.Write("D1={0}\n", Convert.ToBoolean(BitVal & 2));
                    Console.Write("D2={0}\n", Convert.ToBoolean(BitVal & 4));
                    Console.Write("D3={0}\n", Convert.ToBoolean(BitVal & 8));
                    Console.Write("D4={0}\n", Convert.ToBoolean(BitVal & 16));
                    Console.Write("D5={0}\n", Convert.ToBoolean(BitVal & 32));
                    Console.Write("D6={0}\n", Convert.ToBoolean(BitVal & 64));
                    Console.Write("D7={0}\n", Convert.ToBoolean(BitVal & 128));
                    Console.WriteLine("\nPress <SpaceBar> to exit loop...");

                    System.Threading.Thread.Sleep(250);

                    Console.Clear();

                } while (!Console.KeyAvailable);

            }
        }
/********** Get Board Number *******************************************************************/

        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}\n", dev, BoardNum.ToString());
                    daq.FlashLED();
                    return BoardNum;
                }
            }
            return -1;
        }
/********** Wait for key input *****************************************************************/
        public static void WaitForKey()
        {
            Console.WriteLine("\nPress <SpaceBar> to continue...");

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

Attachments
DigitalInput.zip