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

PCI-QUAD04 CSharp (C#) Read Two Encoders

Expand / Collapse
 

PCI-QUAD04 CSharp (C#) Read Two Encoders


The program listed below continuously acquires data from the PCI-QUAD04 specifically to read to quadrature encoder attached to channels 1 & 2. 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.

To make the code more readable a few convenience functions were added such as IsError, GetBoardNum, 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 a key press

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 = "QUAD04";


        static void Main(string[] args)
        {

            MccDaq.ErrorInfo RetVal;

            int BoardNum = 0;

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

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

                MccDaq.Quadrature Quadrature = MccDaq.Quadrature.X1Quad;
                MccDaq.CountingMode CountingMode = MccDaq.CountingMode.ModuloN;
                MccDaq.DataEncoding DataEncoding = MccDaq.DataEncoding.BinaryEncoding;
                MccDaq.IndexMode IndexMode = MccDaq.IndexMode.IndexDisabled;
                MccDaq.OptionState InvertIndex = MccDaq.OptionState.Disabled;
                MccDaq.FlagPins FlagPins = MccDaq.FlagPins.CarryBorrow;
                MccDaq.OptionState GateEnable = MccDaq.OptionState.Enabled;

                int CounterNum = 1;
                for (CounterNum = 1; CounterNum < 3; CounterNum++)
                {
                    RetVal = daq.C7266Config(CounterNum,
                                             Quadrature,
                                             CountingMode,
                                             DataEncoding,
                                             IndexMode,
                                             InvertIndex,
                                             FlagPins,
                                             GateEnable);

                    IsError(RetVal); //check function return value

                    MccDaq.CounterRegister RegName;
                    int LoadValue = 0; //set the counter to begin reading at zero
                    RegName = (MccDaq.CounterRegister)(MccDaq.CounterRegister.QuadCount1 + CounterNum - 1);
                    RetVal = daq.CLoad32(RegName, LoadValue);
                    IsError(RetVal); //check function return value


                    LoadValue = 100000; //set the counter to roll over to zero at 100000
                    RegName = (MccDaq.CounterRegister)(MccDaq.CounterRegister.QuadPreset1 + CounterNum - 1);

                    RetVal = daq.CLoad32(RegName, LoadValue);
                    IsError(RetVal); //check function return value

                }


                System.ConsoleKeyInfo cki = new System.ConsoleKeyInfo();
                MccDaq.StatusBits SBits = 0;
                int EncoderData;
                Console.WriteLine("\nPress any key to Exit...\n");

                do
                {
                    CounterNum = 1;
                    RetVal = daq.CIn32(CounterNum, out EncoderData);
                    IsError(RetVal);//check function return value
                    daq.CStatus(CounterNum, out SBits);
                    if ((SBits & MccDaq.StatusBits.Updown) != 0)
                        Console.Write("Encoder #1 {0}\tDirection {1}\t", EncoderData.ToString("00000"), "UP   ");
                    else
                        Console.Write("Encoder #1 {0}\tDirection {1}\t", EncoderData.ToString("00000"), "DOWN");


                    CounterNum = 2;
                    RetVal = daq.CIn32(CounterNum, out EncoderData);
                    IsError(RetVal);//check function return value
                    daq.CStatus(CounterNum, out SBits);
                    if ((SBits & MccDaq.StatusBits.Updown) != 0)
                        Console.Write("Encoder #2 {0}\tDirection {1}\r", EncoderData.ToString("00000"), "UP   ");
                    else
                        Console.Write("Encoder #2 {0}\tDirection {1}\r", EncoderData.ToString("00000"), "DOWN");


                } while (!Console.KeyAvailable);
                cki = Console.ReadKey();

            }

        }
        /************************************************************************/
        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("{0} board number = {1}\n", dev, BoardNum.ToString());
                    daq.FlashLED();
                    return BoardNum;
                }
            }
            return -1;
        }
        /************************************************************************/

        public static void WaitForKey()
        {
            Console.WriteLine("\nPress any key to continue...");
            do
            {   //idle loop
                System.Threading.Thread.Sleep(10);
            } while (!Console.KeyAvailable);
        }

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

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

  

    }
}


Rate this Article:

Attachments


QuadEncoderRead.zip QuadEncoderRead.zip (25.42 KB, 988 views)

Add Your Comments


For comments email [email protected].

Details
Article ID: 50480

Last Modified:2/6/2014 11:57:00 AM

Article has been viewed 6,818 times.

Options