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

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

この時点で、すべての出力ピンが設定され、2 番目の辞書に結果が入力されます。次の方法で結果を取得できます。

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