SliceIO.cs

Demonstrates Slice I/O initilization and function.

/*  SliceIO.cs
 
 Copyright(c) 1998-2006 by Robotic Systems Integration, Inc. All rights reserved.
 This software contains proprietary and confidential information of Robotic 
 Systems Integration, Inc. (RSI) and its suppliers. Except as may be set forth 
 in the license agreement under which this software is supplied, disclosure, 
 reproduction, or use with controls other than those provided by RSI or suppliers
 for RSI is strictly prohibited without the prior express written consent of 
 Robotic Systems Integration.

 Warning!  This is a sample program to assist in the integration of your motion 
 controller with your application.  It may not contain all of the logic and safety
 features that your application requires.

 For any questions regarding this sample code please visit www.roboticsys.com.
 ==================================================================================

 This application is designed to demonstrate Slice I/O functions using Digital/Analog 
 Input/Outputs.  This example has coded Node and Slice assumes as constants which should
 be changed as appropriate for your system.
 
 The application alternates between reference by Node and reference by Segment.  This
 should give you a feel for both options and allow you to choose the style which best
 suits you.

*/

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using RSI.RapidCode.SynqNet.dotNET;
using RSI.RapidCode.SynqNet.dotNET.Enums;

namespace SampleApplications
{
    [TestFixture]
    public class SliceIO
    {
        const int RSI_BOARD = 0;
        const int IO_NODE = 0; // Slice IO is Node 0
        const int ANALOG_OUT_SLICE_ONE = 0;  // Slice/Segment 0 is an Analog Out
        const int ANALOG_OUT_SLICE_TWO = 1;  // Slice/Segment 1 is an Analog Out
        const int DIGITAL_IN_SLICE = 2; // Slice/Segment 2 is a Digital In
        const int DIGITAL_OUT_SLICE = 3; // Slice/Segment 3 is a Digital Out
        const int ANALOG_IN_SLICE = 4; // Slice/Segment 4 is a Analog In


        [Test]
        public void Main()
        {
            try
            {
                Console.WriteLine("In this example Application we test a Slice I/O.\n");


                MotionController controller;
                IO io;

                // Create and initialize RsiController class (PCI board).
                controller = MotionController.CreateFromBoard(RSI_BOARD);

                // Initialize an IO.
                io = controller.IOGet(IO_NODE);


                // Verification //

                // Verification of IO's Existence.
                if (!(io.IOExists())) // If io is not an IO Node
                {
                    Console.WriteLine("The controller did not return an IO Node from Node IO_NODE (default value 0).\n");
                } // End of if.

                // Node based confirmation of IO type and number.
                Console.WriteLine("Node {0}: {1} Digital Ins, {2} Digital Outs, {3} Analog Ins, and {4} Analog Outs.\n",
                    IO_NODE, io.SqNode.DigitalInCountGet(), io.SqNode.DigitalOutCountGet(), io.SqNode.AnalogInCountGet(), io.SqNode.AnalogOutCountGet());

                // Segment based confirmation of IO segment, type, and number.
                int segmentCount = io.SqNode.SegmentCountGet();
                Console.WriteLine("Number of Segments: {0}", segmentCount);

                for (int i = 0; i < segmentCount; i++)
                {
                    Console.WriteLine("Segment {0}: {1} Digital Ins, {2} Digital Outs, {3} Analog Ins, and {4} Analog Outs.",
                        i, io.SegmentDigitalInCountGet(i), io.SegmentDigitalOutCountGet(i), io.SegmentAnalogInCountGet(i), io.SegmentAnalogOutCountGet(i));
                } // End of for loop.


                // Digital Inputs //
                
                // A TSIO 2007 Slice with 8 Digital Inputs is used in this Example.  

                // Store the value of Digital Input Segment 2, Bit 0 in variable singleBit.  Reference by Node.
                bool singleBit = io.DigitalInGet(0);
                // Store the values of Digital Inputs Segment 2, Bits 2-5 in variable multipleBits.  Reference by Segment.
                int multipleBits = io.SegmentDigitalInGet(DIGITAL_IN_SLICE, 2, 4);

                Console.WriteLine("\nDigital Input Initial Values - Bit 0: {0}, Bit 2-5: {1}", singleBit, multipleBits);
                Console.WriteLine(" *0 on Bits 2-5 represent the binary 0 0 0 0 or False (low) for each input.\n");

                // Digital Outputs //

                // A TSIO 4004 Slice with 4 Digital Outputs is used in this Example .

                // Clearing Outputs to 0 to ensure change.  Reference by Segment.
                io.SegmentDigitalOutSet(3, 0, 4, 0);

                // Store the value of Digital Output Segment 4, Bit 0 in variable singleBit.  Reference by Node.
                singleBit = io.DigitalOutGet(0);
                // Store the values of Digital Outputs Segment 4, Bits 2-3 in variable multipleBits.  Reference by Segment.
                multipleBits = io.SegmentDigitalOutGet(DIGITAL_OUT_SLICE, 2, 2);

                Console.WriteLine("Digital Output Initial Values - Bit 0: {0}, Bit 2-3: {1}", singleBit, multipleBits);


                // Modify the Digital Outputs to display Bit 0 as True (high) and Bit 3 as True (high). Reference by Segment.
                io.SegmentDigitalOutSet(DIGITAL_OUT_SLICE, 0, 4, 9);

                // Store the value of Digital Output Segment 4, Bit 0 in variable singleBit.  Reference by Segment.
                singleBit = io.SegmentDigitalOutGet(DIGITAL_OUT_SLICE, 0);
                // Store the values of Digital Outputs Segment 4, Bits 2-3 in variable multipleBits.  Reference by Node.
                multipleBits = io.DigitalOutGet(2, 2);

                Console.WriteLine("  ==Changing Bit 0 and Bit 3 to High.==");

                Console.WriteLine("Digital Output Modified Values - Bit 0: {0}, Bit 2-3: {1}", singleBit, multipleBits);
                Console.WriteLine(" *Bit 2-3 should display as 2 to present the binary 1 0.\n");


                // Analog Inputs //

                // A TSIO 6012 Slice with 4 Analog Inputs is used in this Example.

                // Similar to Digital Inputs, you my reference by Node or by Segment relation.  Reference by Segment, then Node.
                Console.WriteLine("Analog Input Values - Channel 0: {0}, Channelt 1: {1}\n",
                    io.SegmentAnalogInGet(ANALOG_IN_SLICE, 0), io.AnalogInGet(1));


                // Analog Outputs //

                // A TSIO 7004 Slice with 2 Analog Outputs is used as Slice 0 in this Example
                // A TSIO 7002 Slice with 2 Analog Outputs is used as Slice 1 in this Example

                // Clearing Outputs to 0 to ensure change.  Reference by Node.
                for (int i = 0; i < io.SqNode.AnalogOutCountGet(); i++)
                {
                    io.AnalogOutSet(i, 0);
                } // End of for Loop.

                // Reference by Node, then Segment.
                Console.WriteLine("Analog Output Initial Values - Channel 0: {0}, Channel 4: {1}",
                    io.AnalogOutGet(0), io.SegmentAnalogOutGet(ANALOG_OUT_SLICE_TWO, 1));

                // Modify the Analog Outputs to have Channel 0 as 7 and Channel 4 as -3.  Reference by Segment, then Node.
                Console.WriteLine("  ==Changing Channel 0  to 7 and channel 4 to -3.==");
                io.SegmentAnalogOutSet(ANALOG_OUT_SLICE_ONE, 0, 7);
                io.AnalogOutSet(3, -3);

                // Reference by Node, then Segment.
                Console.WriteLine("Analog Output Modified Values - Channel 0: {0}, Channel 4: {1}",
                   io.AnalogOutGet(0), io.SegmentAnalogOutGet(ANALOG_OUT_SLICE_TWO, 1));

                // Insert your application code here.
                String bob = Console.ReadLine();

            }
            catch (RSI.RapidCode.SynqNet.dotNET.RsiError err)
            {
                Console.WriteLine(err.text);
            }
        }  // End of Main.        
    } // End of Class.
}  // End of Namespace.