USB-1608G CSharp (C#) Digital Output
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 ); } } } }
Attachments
DigitalSingleBitOutput.zip