oopplctwincatstructured-texttwincat-hmi

Passing an instance of a FB into the initialization of a different instance from a different function block


I am new to twincat3 and especially using it with oop. I am trying to pass an instance of FB_EventLogger to the FB_init of an instance of O2_Sensor. Forexample: Main Var EventLogger : FB_EventLogger (I_PersistantEventStrotagr := CSV); O2_sensor : O2_Sensor(EventLogger := EventLogger); end_var

end_Main

O2_Sensor var EventLogger : Reference to FB_EventLogger ; eventinfo : something; end_var

method_addEvent

Eventlogger.AddEvent(eventinfo); end_method_addEvent

end_O2_Sensor

I would like to allow for any instance of the O2_sensor FB to be able to create their own event as this will make the code much better rather than having a seperate function that checks all the different inputs and then raises alarms or sents messages.

The First thing I thought about was to pass the instance of eventlogger fb and use that in the sensor's fb but that does not work. However, it works if I create a method that takes in a reference to an event logger, in other words O2_Sensor.addevent(EventLogger).


Solution

  • I am unable to fully understand your attempt reading your question. Initialization can be very tricky. Here are a few pointers :