instantiate function block at input

Anonymous
2015-11-13
2015-11-16
  • Anonymous - 2015-11-13

    Originally created by: patrick van ieperen

    Hello,
    I am trying to make a hardware abstraction layer by using interfaces.

    It's working out fine, but is it possible to create a instance of a class / function block during runtime.

    In Java and C# i will do like :

    Motorcontroller mc = new Motorcontroller();

        mc.setMotorType(new ServoMotor());
        System.out.println(mc.getMotorType());
    
        mc.setMotorType(new StepperMotor());
        System.out.println(mc.getMotorType());
    
        mc.setMotorType(new DcMotor());
        System.out.println(mc.getMotorType());
    

    Is there a way to use the "new command" in Codesys?

    regards Patrick

     
  • singleton - 2015-11-16

    Hello,
    you can use __NEW() and __DELETE() if your runtime system supports that.
    Check the online help for more information.

     
  • Anonymous - 2015-11-16

    Originally created by: patrick van ieperen

    Hello Singleton,

    thanks for your quick reply.

    if i make a pointer to my intrerface Imotor.
    and then try to assign fb_StepperMotor, this class implements Imotor, i will get an error.

    "cannot convert type POINTER TO fb_stepperMotor to type POINTER TO Imotor

    main program:
    PROGRAM main
    VAR
    ptrITF : POINTER TO Imotor;
    fbSub : fbMain;
    bInit: BOOL := TRUE;
    strNaam : STRING;
    END_VAR

    fbSub();

    IF (bInit) THEN
    ptrITF := __NEW(fb_StepperMotor);
    fbSub.setDynamic(ptr:=ptrITF);
    strNaam := fbSub.getName;
    bInit := FALSE;
    END_IF

    while in Java i use :
    public class Motorcontroller
    {
    ImotorControl mc;

    public Motorcontroller()
    {
    }
    public void setMotorType(ImotorControl imc)
    {
        this.mc = imc;
    }
    

    Regards Patrick

     

Log in to post a comment.