0

I'm trying to write a program to interface with an I/O card. The hardware's provided documentation is in C# and I have never used C# before. So, pardon me if this question seems very basic or elementary.

Using the manufacturer's functions calls for at least 3 variables per I/O pin and there are 55 pins. That means a lot of defined variables. As I am writing my code, I am finding that its very ugly. I am reusing snippets here and there that could easily be made a function. But making them a function, I lose the ability to access the multitude of variables. Is there some simple way to do this?

Here is an shortened version of what I am currently working with. The missing pieces look the same. I'm using VS2012. Thanks in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

/* Tool Changer Controller Program
 * In theory, this program will run the tool changer through a full cycle.
 * This includes:     removing the current tool, 
 *                    placing the current tool in its appropriate location on the carousel,
 *                    moving the carousel to the correct location of the new tool,
 *                    placing the new tool in the spindle so it is ready for use.
 */
namespace Tool_Changer
{

class Program
{
    static void Main(string[] args)
    {
        // Create PoKeys55 device object
        PoKeysDevice_DLL.PoKeysDevice device = new PoKeysDevice_DLL.PoKeysDevice();

        // Define all pins and correlate to correct pin
        // Pin: Pin #, Status: pass/fail returned, State: On/Off status
        // Swivel
        byte SwivelCCWA_Pin = 0; //Set*
        bool SwivelCCWA_Status = false;
        bool SwivelCCWA_State = false;

        byte SwivelCWA_Pin = 2; //Set*
        bool SwivelCWA_Status = false;
        bool SwivelCWA_State = false;

        // Telescope
        byte TeleExtA_Pin = 4; //Set*
        bool TeleExtA_Status = false;
        bool TeleExtA_State = false;

        byte TeleRetA_Pin = 6; //Set*
        bool TeleRetA_Status = false;
        bool TeleRetA_State = false;
        // Grabber
        byte GrabberOpen_Pin = 12; //Set*
        bool GrabberOpen_Status = false;
        bool GrabberOpen_State = false;

        byte ToolPresent_Pin = 13; //Get
        bool ToolPresent_Status = false;
        bool ToolPresent_State = false;

        // Begin Tool Change procedure.
        SwivelCWA_State = false;
        SwivelCCWA_State = true;
        SwivelCWA_Status = device.SetOutput(SwivelCWA_Pin, SwivelCWA_State); // Turn off CW
        SwivelCCWA_Status = device.SetOutput(SwivelCCWA_Pin, SwivelCCWA_State); // Turn on CCW
        Thread.Sleep(1000);

        GrabberOpen_State = true;
        GrabberOpen_Status = device.SetOutput(GrabberOpen_Pin, GrabberOpen_State); // Open Grabber

        TeleRetA_State = false;
        TeleExtA_State = true;
        TeleRetA_Status = device.SetOutput(TeleRetA_Pin, TeleRetA_State); // Turn off retract
        TeleExtA_Status = device.SetOutput(TeleExtA_Pin, TeleExtA_State); // Turn on extend
        Thread.Sleep(1000);
4
  • Make the variables class variables, and then have the functions that use them in a class (your class here is program). Commented Apr 25, 2013 at 15:20
  • 2
    Why not use a dedicated class? You could also probably set this up with a couple of properties as 'property bags' themselves, with collections of variables relating to the same thing for different pins. Commented Apr 25, 2013 at 15:20
  • @GrantThomas - A struct likely would work better in a case like this. Commented Apr 25, 2013 at 15:42
  • @Ramhound I considered that but thought to leave my comment brief, and that that would be the next extension of the idea - though perhaps not will 50+ properties of a struct type, these could be the items' type in the collection. Commented Apr 25, 2013 at 15:44

2 Answers 2

2

The code will become prettier and more compact if you use a struct to hold the common variables. For example:

public struct Item
{
    public Item(byte pin, bool status, bool state)
    {
        Pin = pin;
        Status = status;
        State = state;
    }

    public byte Pin;
    public bool Status;
    public bool State;
}

And then instead of setting each variable individually like this:

byte SwivelCCWA_Pin = 0; //Set*
bool SwivelCCWA_Status = false;
bool SwivelCCWA_State = false;

you will be able to use only one line of code like this:

Item swivelCCWA = new Item(0, false, false);
Sign up to request clarification or add additional context in comments.

2 Comments

How would I use this when I called for example device.SetOutput? SwivelCCWA.Status = device.SetOutput(SwivelCCWA.Pin, SwivelCCWA.State)?
Yes, exactly or even better - you can add a method to the struct that accepts a device and updates the status using the pin and the state. The call will then look like this: swivelCCWA.UpdateStatus(device);
0

Create a dictionary with all output settings per pin:

 Dictionary<int, bool> pinStates = new Dictionary<int, bool> 
 {
     {0, false},
     {2, true},
     {12, false},
     {13, false}
 };

Then you could create a function so set all the output pins and retrieve the results in another dictionary:

Dictionary<int, bool> pinResults = new Dictionary<int, bool>();
foreach(KeyValuePair<int, bool> pinState in pinStates)
{
    pinResults[pinState.Key] = device.SetOutput(pinState.Key, pinState.Value); 
}

At this point, all outputs pins are set, and your second dictionary is filled with the results. You can retrieve the results this way:

bool resultOfPin12 = pinResults[12];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.