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

Measurement Computing Data Acquisition Knowledgebase

Welcome Guest ( )

USB-1608G CSharp (C#) Digital Ouput

Expand / Collapse
 

USB-1608G CSharp (C#) Digital Ouput


The following example demonstrates how to set individual digital output lines. The program uses the keys 1 - 8 to set the 8 digital IO pins and the Escape key to clear them. 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. 

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 DigitalSingleBitOutput
{
    class Program
    {
        public const string DEVICE = "1608G"; //identify the board using string name

        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;
        }
 
        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            ushort BitVal    = 0;

            int BoardNum    = GetBoardNum( DEVICE );

            System.ConsoleKeyInfo cki = new ConsoleKeyInfo();

            if( BoardNum == -1 )

                Console.WriteLine( "No USB-{0} detected!\n", DEVICE );

            else
            {
                MccDaq.MccBoard daq = new MccDaq.MccBoard( BoardNum );

                Console.WriteLine(  "Digital Single Bit Input Example\n" + 
                                    "\nPress keys 1,2,3,4,5,6,7 & 8 to set bits...<ESC> clears all.\n" +
                                    "\nPress <SpaceBar> to exit loop..." 
                                 );

                RetVal = daq.DConfigPort( DigitalPortType.AuxPort,  DigitalPortDirection.DigitalOut );

                do
                {
                    if( Console.KeyAvailable )
                    {
                        cki = Console.ReadKey();
                        switch( cki.Key )
                        {
                            case ConsoleKey.D1:
                                BitVal += 1;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal )); 
                                break;
                            case ConsoleKey.D2:
                                BitVal += 2;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.D3:
                                BitVal += 4;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.D4:
                                BitVal += 8;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)(BitVal));
                                break;
                            case ConsoleKey.D5:
                                BitVal += 16;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.D6:
                                BitVal += 32;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.D7:
                                BitVal += 64;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.D8:
                                BitVal += 128;
                                RetVal = daq.DOut(DigitalPortType.AuxPort, (short)( BitVal ));
                                break;
                            case ConsoleKey.Escape:
                                RetVal = daq.DOut( DigitalPortType.AuxPort, 0 );
                                break;
                        }
                    }
     
                    System.Threading.Thread.Sleep( 100 );

                } while ( cki.Key != ConsoleKey.Spacebar );

            }
        }
    }
}



Rate this Article:

Attachments


DigitalSingleBitOutput.zip DigitalSingleBitOutput.zip (23.63 KB, 856 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50471

Last Modified:2/6/2014 11:49:36 AM

Article has been viewed 5,613 times.

Options