Check for valid interface?

somlioy
2022-01-29
2022-01-29
  • somlioy - 2022-01-29

    Hi

    I have a common interface between various frequency drives (ItfDrive).
    In a function block I have

    VAR_INPUT
        ItfFlushingPump     : ItfDrive;
    END_VAR
    

    How can I check that the interface is valid/has an assignment to an function block which has implemented this interface?
    Is there some similar way as with references using __ISVALIDREF()?

    If no drive is assigned to the function block, and the function block is trying to access any properties or methods of the interface, it will crash ... That's what I want to avoid.

     
  • i-campbell

    i-campbell - 2022-01-29
    IF ItfFlushingPump <> 0 THEN
       //OK
    END_IF
    

    In the open source cobolt library, I have put in a GVL a function block instance g_InvalidMetric. So for most of the methods which return an IMetric, on error, they will return this global metric, rather thn nothing. This means, if you do want to call the methods anyway, it will not give you an exception, only invalid data.
    In this case, you would look at the error message returned, or look at some property of the Interface to know if it is valid.

    Check out the Host Application in the Example Project of cobolt for how this looks.

     

Log in to post a comment.