Class organization vb.net

I tried my best to find out the classes. Even after reading several websites and books, I still don’t feel like I really get it.

The application I'm working on has a subsystem that will manage several different test devices via the USB interface for the GPIB interface.

So, I have a:

Laptop
   USB - GPIB Interface
        Power Supply (Device Address 5)
        HP34970 Datalogger (Device Address 6)

In my section, I would like to open the GPIB device, send some SCPI commands to turn on the power, send a few more to the HP34970 to switch the relay or measure the voltage.

It seems simple enough, and I can easily get everything to work in one module. However, I think it would be much better to have a separate class for the GPIB interface, power supply and HP34970. If that were the case, I could very easily reuse the code in other projects.

That's where I just can't get a mental model. If I create an instance of the GPIB class and have a way to “open” the channel on the GPIB bus, how do I allow methods in other classes (for example, a power supply) to use the open channel created in the GPIB class? For example, a method in the class of a power supply for setting voltage.

If someone could take a few minutes to post a pseudo code and a short explanation to illustrate how I could / should have organized this, I would really appreciate help!

thank

+3
3

. , , - , , , , - "" ?

- # (, :) , powerupply dataLogger ).

public class DeviceTesting
{
   private string powerSupplyAddress = "DA5";
   private string dataLoggerAddress = "DA6";

   public static void main()
   {

      //bring interface into life
      DeviceInterface interface = GPIBInterface(); //example of inheritance - GPIB could be just one of the many device interfaces

      //the constructor in PowerSupply takes on the responsibility of initializing itself
      PowerSupply ps = new PowerSupply(powerSupplyAddress, interface); //plugged in with interface - has a reference to it
      //you could have multiple types of data loggers - all could inherit from DataLogger, and here so does HP34970
      //This means you can reuse the common code for data loggers, and write only the specifics for each specific device of that kind
      DataLogger myLogger = new HP34970(dataLoggerAddress, interface);


      //now, do whatever you do with the real devices
      ps.SetVoltage(220); //send voltage command directly
      interface.SendLogMessage("Interface is operational");
      interface.ExecuteTest("test1"); //send voltage command through interface
      //etc... you get the idea...
   }
}

, , , ( - , , - ):

public GPIBInterface : DeviceInterface //here we are reusing code that same for all interfaces
{
    //whoever attaches to this interface is responsible for registering itself with it
    public PowerSupply Power{get;set;}
    public DataLogger Logger{get;set;}
    //notice here that it can work with any class that inherits from DataLogger
    public GPIBInterface()
    {

    }

    private override void TestInitialization() //as an example, we write our own testing by overriding some method in base class
    {
        base.TestInitialization(); //make sure that the base class checks it ok - e.g. if it own display is working or similar...

        if (Power.TestConnection() == false)
            throw new DeviceNotWorkingException(ps);
        if (Logger.TestConnection() == false)
            throw new DeviceNotWorkingException(_logger);
    }

    public void SendLogMessage(string message)
    {
        Logger.SendMessage(message);
    }

    public void ExecuteTest(string testName)
    {
        switch(testName)
        {
            case "test1":
                Power.SetVoltage(280);
                Logger.SendMessage("Voltage increased to 280V");
                break;
        }
    }
}

, , , , . , PowerSupply , PowerSupply . , , PowerSupply , ( ).

, , "-" - , - 1, "thing1()" ; 441, . , ...

, Inversion of Control (IoC) - , , powerSupply, , , - , IoC. , , - IoC, .

+1

(, ) , GPIB ?

- , Power Supply GPIB ( #):

public class PowerSupplyGPIBController
{
    // Members
    GPIB gpib;
    PowerSupply powerSupply;

    // Constructor
    public PowerSupplyGPIBController(GPIB myGPIB, PowerSupply myPowerSupply)
    {
        gpib = myGPIB;
        powerSupply = myPowerSupply;
    }

    // Method
    public void SendPowerSupplyVoltage()
    {
        GPIB.Send(myPowerSupply.Voltage);
    }
}

GPIB PowerSupply - /.

+1

, PowerSupply, Datalogger .. GPIB, . , - MyPowersupply.SetVoltage, PowerSupply.SetVoltage GPIB . , , , , . . PowerSupply, Datalogger .. GPIB, .

+1

Source: https://habr.com/ru/post/1762544/


All Articles