0

我正在尝试编写一个与 I/O 卡接口的程序。硬件提供的文档是用 C# 编写的,我以前从未使用过 C#。所以,如果这个问题看起来非常基本或基本,请原谅我。

使用制造商的函数,每个 I/O 引脚至少需要 3 个变量,并且有 55 个引脚。这意味着很多定义的变量。在编写代码时,我发现它非常难看。我在这里和那里重复使用可以很容易地成为功能的片段。但是让它们成为一个函数,我失去了访问大量变量的能力。有一些简单的方法可以做到这一点吗?

这是我目前正在使用的缩短版本。丢失的部分看起来是一样的。我正在使用VS2012。提前致谢。

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

2 回答 2

2

如果您使用结构来保存公共变量,代码将变得更漂亮和更紧凑。例如:

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;
}

然后不是像这样单独设置每个变量:

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

您将只能使用这样的一行代码:

Item swivelCCWA = new Item(0, false, false);
于 2013-04-25T15:30:58.500 回答
0

为每个引脚创建一个包含所有输出设置的字典:

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

然后你可以创建一个函数来设置所有的输出引脚并在另一个字典中检索结果:

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

此时,所有输出引脚都已设置,并且您的第二个字典已填充结果。您可以通过以下方式检索结果:

bool resultOfPin12 = pinResults[12];
于 2013-04-25T15:38:12.833 回答